How to Install LEMP on Ubuntu 16.04

How to Install LEMP on Ubuntu 16.04

We’ll show you, how to install LEMP on Ubuntu 16.04. LEMP stack (Linux, Nginx, MySQL, PHP) should not be confused with the popular LAMP stack (Linux, Apache, MySQL, PHP) . This tutorial is for the LEMP (Nginx) stack and it’s commonly used on Ubuntu 16.04 servers. Installing LEMP on Ubuntu 16.04, should not take more then 10 min. Let’s start with the installation of LEMP on Ubuntu 16.04:

1. 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.

2. Install Nginx on Ubuntu 16.04

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

3. Start 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.

4. Install PHP-FPM

Run the following commands to install the necessary PHP packages:

apt-get install php-fpm php-mysql

5. Configure PHP-FPM

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

cgi.fix_pathinfo=0

Save the changes and restart PHP-FPM:

systemctl restart php7.0-fpm

6. Configure Nginx Web Server

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 127.0.0.1:9000
        #
        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

7. Restart Nginx Web Server

If there are no errors, restart the web server

systemctl restart nginx

8. 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.

Need a fast and easy fix?
✔ Unlimited Managed Support
✔ Supports Your Software
✔ 2 CPU Cores
✔ 2 GB RAM
✔ 50 GB PCIe4 NVMe Disk
✔ 1854 GeekBench Score
✔ Unmetered Data Transfer
NVME 2 VPS

Now just $43 .99
/mo

GET YOUR VPS

9. Secure MySQl Server

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

mysql_secure_installation

and follow the recommendations.

10. Start MySql Server

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


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

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

Leave a Comment