How to optimize performance of the LEMP stack on Ubuntu Server

In the previous article, I introduced you to how to install LEMP (Linux, Nginx, MySQL, PHP) on Ubuntu Server, today, I would like to introduce you to some tips to optimize the LEMP server for it to work properly and more effective.

  1. Make Browsers Cache Static Files On nginx
    This trick forces the browser to cache static files (images, CSS and Javascript) and reload them instead of sending requests to the server for loading these static files.
    Edit virtual host using below command
    sudo nano /etc/nginx/sites-available/defaultChange the setting as below

    [...]
    server {
            [...]
            location ~*  \.(jpg|jpeg|png|gif|ico|css|js)$  {
                  expires 365d;
            }
            [...]
    }
    [...]
  2.  Enable Gzip Compression
    Edit nginx.conf
    sudo nano /etc/nginx/nginx.conf
    On gzip section, change the setting as below# enable gzip compression
    gzip on;
    gzip_disable “msie6”;
    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_buffers 16 8k;
    gzip_http_version 1.1;
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
    # end gzip configuration
  3. Optimize MySQL table automatically every weekOpen crontab (crontab -e) and add

    @weekly mysqlcheck -o –user=root –password=<your password here> -A