How to Install LEMP Stack on Ubuntu 20.04


In this article, we will have explained the necessary steps to install and configure the LEMP Stack on Ubuntu 20.04 LTS. Before continuing with this tutorial, make sure you are logged in as a user with sudo privileges. All the commands in this tutorial should be run as a non-root user.

LEMP is a variation of the ubiquitous LAMP stack used for developing and deploying web applications. Traditionally, LAMP consists of Linux, Apache, MySQL, and PHP. Due to its modular nature, the components can easily be swapped out. With LEMP, Apache is replaced with the lightweight yet powerful Nginx.

Install LEMP Stack on Ubuntu 20.04

Step 1. First, before you start installing any package on your Ubuntu server, we always recommend making sure that all system packages are updated.

Step 2. Install Nginx.

Nginx is available on the default Ubuntu 20.04 repos. Run the following command to install Nginx Web server:

Once installed, check to see if the Nginx service is running:

Verify access to Nginx from the browser by using the address, http://Your-Server.IP_or_hostname. You should land on the Nginx HTTP server test page which basically shows that the Nginx web server is successfully installed and working.

Step 3. Install MariaDB Database Server.

Run the following command to install MariaDB on Ubuntu 20.04:

Once completing installation MariaDB will start automatically. Check MariaDB version by typing:

Then, issue the mysql_secure_installation command to improve the security of the MariaDB installation:

Install LEMP Stack on Ubuntu 20.04

Step 4. Install PHP in Ubuntu 20.04.

The final component of your LEMP Stack on Ubuntu 20.04 is PHP. PHP7.4 is included in Ubuntu 20.04 repository, to install PHP and PHP-FPM, run the following command which will also install some required additional packages:

Step 5. Configure Nginx for PHP.

The next step you that need to complete is to modify the Nginx configuration file:

Paste the following content:

To enable the server configuration that we just created, run the following command:

Restart PHP and reload the NGINX configuration:

Step 6. Configure Firewall.

It is recommended that you enable the ufw firewall and add a rule for Nginx:

Step 7. Testing PHP

Now you should create info.php file to test PHP to do so type following:

Then, open the following link in your web browser and we will be able to see all the information about PHP and its other configurations: http://your-domain/phpinfo.php (replacing your IP address with the one above).

That’s all you need to do to install LEMP Stack on Ubuntu 20.04 Focal Fossa. I hope you find this quick tip helpful. If you have questions or suggestions, feel free to leave a comment below.