How to Install LEMP Stack on Ubuntu 20.04

The LEMP stack is one of the most popular kits as the basis for a web site. It consists of a Linux server, Nginx web server, MySQL database management systems and PHP. We’re going to take a look at the process of installing a LEMP stack in Ubuntu 20.04. All commands must be run as root or add sudo before them.

Installing Nginx

First, let’s update the package manager cache and install Nginx.

apt update
apt install nginx

If the firewall is enabled, you need to allow access to the server on port 80:

ufw allow in "Nginx HTTP"


Rules updated
Rules updated (v6)

You can now check if the Nginx web server is running by entering your server’s public IP address into your browser’s address bar. You will see the following page:

Screenshot 1. Nginx welcome page.

Installing MySQL

To install MySQL server enter:

apt install mysql-server

Once installed, run the script and follow the instructions to configure the recommended MySQL security settings. The most secure setting will be achieved with affirmative answers and the strictest password complexity check.


Installing PHP

Install the following packages to get PHP running on the server.

apt install php-fpm php-mysql

Configuring Nginx for PHP Processing

In this tutorial, we’ll show you how to configure PHP processing using the default virtual host as an example. When configuring Nginx to work with multiple domain names, this setting will also apply. Nginx virtual host basic configuration guide.
Open the default virtual host configuration file.

nano /etc/nginx/sites-available/default

Add the next code to the end of the server section:

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

Save and close the file and check the configuration for errors:

nginx -t

Restart Nginx:

systemctl restart nginx

Testing PHP on Nginx Server

We will now create a .php file on our server and check if PHP is running. Create a file:

nano /var/www/html/info.php

Paste the following content here:

<!--?php phpinfo(); ?-->

Now enter your server’s public IP address into your browser’s address bar with /info.php at the end:


If you see the following content, PHP works fine with Nginx.

Screenshot 2. PHP testing.