How to Install LEMP on Ubuntu 16.04

lemp ubuntu 16.04

The following article describes how to install a LEMP stack on an Ubuntu 16.04 VPS.

LEMP (Linux, Nginx, MySQL, PHP) should not be confused with the popular LAMP (Linux, Apache, MySQL, PHP) stack. This tutorial is for the LEMP (Nginx) stack and it’s commonly used on Ubuntu 16.04 servers.

Update the server

Make sure that all installed packages on your Ubuntu 16.04 VPS are up to date.

apt-get update && apt-get upgrade

You can also configure automatic updates.

Install Nginx

Before installing the Nginx web server, make sure that there is no other web server like Apache installed on the VPS. If Apache web server is running, stop it:

systemctl stop apache2

and remove it from your VPS:

apt-get remove --purge apache2

Now, run the following command to install Nginx:

apt-get install nginx

Once it is installed, start the web server and enable it to start on boot:

systemctl start nginx
systemctl enable nginx

You can check if Nginx is successfully started using the following command:

systemctl status nginx

The output of the command above should be similar to this:

● nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
   Active: active (running)
 Main PID: 5926 (nginx)
   CGroup: /system.slice/nginx.service
           ├─5926 nginx: master process /usr/sbin/nginx -g daemon on; master_process on
           ├─5927 nginx: worker process
           └─5928 nginx: worker process

You can also verify if Nginx is running, by accessing http://YOUR_IP in your favorite web browser.

Install PHP-FPM

Run the following commands to install the necessary PHP packages:

apt-get install php-fpm php-mysql

Once the installation is completed, edit the PHP configuration file and modify the cgi.fix_pathinfo parameter value from 1 to 0.

To find the location of the php.ini file run the following command:

php --ini |grep Loaded
Loaded Configuration File:         /etc/php/7.0/cli/php.ini

We need to edit the /etc/php/7.0/cli/php.ini file and make the changes.

vi /etc/php/7.0/cli/php.ini


Save the changes and restart PHP-FPM:

systemctl restart php7.0-fpm

Next step is to configure Nginx to use PHP. We will modify the default Nginx server block and add/uncomment the location ~\.php$ block.

vi /etc/nginx/sites-available/default

server {
        listen 80 default_server;
       root /var/www/html;

        # Add index.php to the list if you are using PHP
        index index.php index.html index.htm index.nginx-debian.html;

        server_name _;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;

        # pass the PHP scripts to FastCGI server listening on
        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/run/php/php7.0-fpm.sock;

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        location ~ /\.ht {
                deny all;

Save the changes and test the new configuration

nginx -t 

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

If there are no errors, restart the web server

systemctl restart nginx

Install MySQL server

Install MySQL database server by executing the following command:

apt-get install mysql-server

During the installation, you will be prompted to enter a password for the MySQL root user. Make sure to use a strong password (combination of letters and numbers and minimum 10 characters long) for your the root user.

Once the MySQL server installation is completed, it is recommended to secure the installation by running the security script provided by MySQL:


and follow the recommendations.

Start MySQL and enable it to start on boot

systemctl start mysql
systemctl enable mysql

And that’s it. You have successfully installed the LEMP stack on your Ubuntu 16.04 server.

Further steps

After you’ve installed the LEMP stack, you can follow our guide on how to secure it: How to secure your LEMP stack

You can also get optimized LEMP hosting from us and we’ll install, configure and optimize a LEMP stack on your VPS, free of charge.

Of course, you don’t have to do any of this if you use one of our Linux VPS Hosting services, in which case you can simply ask our expert Linux admins to install the LEMP stack for you. They are available 24×7 and will take care of your request immediately.

PS. If you liked this post please share it with your friends on the social networks using the buttons below or simply leave a reply in the comments section. Thanks.

How to install and configure AutoMySQLBackup
Install Node.js and create your first Express application
Install and run DokuWiki the most versatile Wiki on a CentOS 7 VPS

Free Migration to RoseHosting. Award Winning Linux VPS Hosting with Fully Managed Support. Coupon RHBLOG50 for 50% offOrder Now