Install LEMP Stack on Ubuntu 20.04

LEMP is an acronym for Linux, NGINX (pronounced as Engine X,) MySQL, and PHP. These are all popular open-source tools typically used in web development. LEMP stack is commonly used to host dynamic websites. Most content management systems such as WordPress, Drupal, and Joomla create dynamic web pages.

When a request comes, the server will run the PHP code to put together the HTML version and query the backend database to get content to insert into HTML.

In this guide, we will cover the steps to install the LEMP stack on Ubuntu 20.04.

Prerequisites

This takes care of the first portion of the LEMP stack, i.e., Linux.

Install NGINX Web Server

For the second portion of the LEMP stack, we would need to set up NGINX for serving web resources to clients.

It is easy to install NGINX on Ubuntu 20.04 with the following command.

$ sudo apt install nginx

After the installation completes successfully, you can check the status of NGINX by running the next command.

$ sudo systemctl status nginx
Check NGINX status
Check NGINX status

You should see a message confirming that NGINX is active (running.) Otherwise, run the next command to start NGINX.

$ sudo systemctl start nginx

Now, you may open a web browser and enter your server's ip address. You should see NGINX's default page.

Note: You can retrieve your server's ip address by running the command below.

$ curl ifconfig.me

Or simply enter localhost in your web browser if you're connected locally.

NGINX default web page
NGINX default web page

Change the Default NGINX Document Root

The "Welcome to nginx" web page above was served from the default document root i.e., /var/www/html based on the configuration directives in the default server block file. The default NGINX server block file is /etc/nginx/sites-available/default. Rather than use this default configuration, we can create ours.

Here's how.

First of all, create a directory under /var/www which will serve as the new default NGINX document root. I have named mine cloudindevs according to the first portion of my registered domain name. You can name yours in a similar manner.

$ sudo mkdir /var/www/cloudindevs

Next, ensure that the NGINX service account (www-data) has access to the document root directory.

Note: it's not mandatory to use the www-data. You can create any meaningful username and define user in /etc/nginx/nginx.conf file.

To change ownership of the new document root to www-data, type:

$ sudo chown www-data:www-data /var/www/cloudindevs 

To assign permissions, run the following chmod command:

$ sudo chmod -R 755 /var/www/cloudindevs

After that, you may create a new NGINX server block file by copying the existing one as follows.

$ sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/cloudindevs.com

Next, edit the new NGINX server block file and change the root directive to point to your new document root.

$ sudo nano /etc/nginx/sites-available/cloudindevs.com
Change NGINX root directory
Change NGINX root directory

The next step is to activate the new server block file by sym-linking it as follows.

$ sudo ln -s /etc/nginx/sites-available/cloudindevs.com /etc/nginx/sites-enabled

You may now disable the former default NGINX server block file by unlinking it with the command below.

$ sudo unlink /etc/nginx/sites-enabled/default

Test your NGINX configuration with the next command.

$ sudo nginx -t 

To properly test this change, copy the sample html code below.

<head>
<title>Successfully changed NGINX default document root!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx default document root was successfully changed.</p>
</body>
</html>

Create a new index page in the new document root directory with the following command.

$ sudo nano /var/www/cloudindevs/index.html

Paste the html code in the text editor, save and close the file.

Restart NGINX with the command below.

$ sudo systemctl restart nginx

Finally, open a web browser and enter the IP address of your server.

Once you see the message in the image below, you are good to go.

NGINX new default web page
NGINX new default web page

Install MySQL Database Management System

The third portion of the LEMP stack is MySQL -- a commonly used open-source database management system for storing and managing application data. MariaDB is a good replacement for MySQL in the LEMP stack as it offers improved performance. But in this article, we are using the traditional MySQL.

You can install MySQL on Ubuntu 20.04 with the following command.

$ sudo apt install mysql-server

Once installed, you can check the version of MySQL with the next command.

$ mysql --version

Configure MySQL

To properly configure MySQL, it is recommended that you run the mysql_secure_installation script as follows.

$ sudo mysql_secure_installation

In a nutshell, the script will prompt you to:

  • Enable/Disable VALIDATE PASSWORD component. This pertains to password complexity requirement
  • Set a new password for the MySQL root user
  • Remove anonymous users
  • Disallow root login remotely
  • Remove test database
  • Reload the privilege tables to apply the changes

Once the mysql_secure_installation script completes with a success message, you may login to MySQL.

Login to MySQL

To login to MySQL, run the command below and enter your MySQL root password when prompted.

$ sudo mysql -u root -p

You should see the mysql> prompt which confirms that you now have a working MySQL server.

Login to MySQL
Login to MySQL

Type quit and press enter to log out of MySQL.

mysql> quit

Install PHP

Lastly, installing Hypertext Preprocessor (PHP) completes the LEMP stack. PHP handles the dynamic processing of web content and interacts with MySQL.

In this section, we are going to install PHP-FPM (Fast-CGI Process Manager) and configure it for use by the NGINX web server.

Install PHP-FPM

Run the command below to install PHP-FPM and related components on Ubuntu 20.04.

$ sudo apt install php-fpm php-mysql

Once installed, check the version of PHP with the command below.

$ php -v

Configure NGINX to use PHP-FPM

Edit your NGINX server block configuration file as follows.

$ sudo nano /etc/nginx/sites-available/cloudindevs.com 

Press the down arrow on your keyboard until you get to the line beginning with index. Now, add index.php to the list as shown in the image below.

Add index.php to NGINX index list
Add index.php to NGINX index list

Next, press the down arrow key again until you get to the section titled, "pass PHP scripts to FASTCGI server."

Uncomment the following lines.

location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}

Now, because I want my NGINX server to be accessible through my registered domain name, I have made a few additional changes to the server block file.

Below is a snippet of the server block file after these changes are made. If you would like to do the same, you may copy the configuration details below and just replace cloudindevs accordingly.

server {
        listen 80;
        root /var/www/cloudindevs;
        index index.php index.html index.htm;
        server_name cloudindevs.com www.cloudindevs.com;

        location / {
                
        try_files $uri $uri/ =404;
        
        }

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
           
        }

Save changes and close the server block configuration file.

Note: By default, PHP-FPM pool configuration is stored in /etc/php/7.4/fpm/pool.d/www.conf file. Here, you can define user and group for php-fpm.

Test PHP-FPM configuration

Recall that we added index.php to the list of index files in the NGINX server block configuration file. Hence, we need to create the index.php file in the new default document root as follows.

$ sudo nano /var/www/cloudindevs/index.php

Copy the PHP code below and paste it in the text editor.

<?php
phpinfo();
?>

Save changes and close the file.

Restart NGINX with:

$ sudo systemctl restart nginx

Open a web browser and enter your server's IP address or localhost if you are locally connected. You should see the following page which confirms that PHP-FPM is working correctly.

Test PHP-FPM configuration with NGINX
Test PHP-FPM configuration with NGINX

For security reasons, you may remove the index.php file as follows, to avoid revealing too much information about your server to the outside world.

$ sudo rm /var/www/cloudindevs/index.php

Conclusion

Following the steps in this guide, you should now have a working LEMP platform on Ubuntu 20.04. Should you need clarification regarding this guide, do let us know in the comments section below.

Leave a Comment