
The LEMP is a short name that describes a Linux operating system, with an Nginx (pronounced like “Engine-X”) web server. The backend data is stored in the MySQL or MariaDB database and the dynamic processing is handled by PHP. This tutorial describes how to install a LEMP stack on an Ubuntu 20.04 server.
We will show you how to install Nginx, install and secure MySQL, and install PHP.
Install LEMP Stack on Ubuntu
You will need non-root user having sudo privileges to be able to install packages.
Step 1 – Installing Nginx
Nginx is a high performance HTTP server. It’s available in default Ubuntu repositories. We’ll use the apt
package manager to obtain this package.
Update the packages index and install Nginx by running the following commands:
sudo apt update
sudo apt install nginx
Once the installation is finished, the Nginx web server will be start automatically on your Ubuntu 20.04 server.
If you have the ufw
firewall enabled, you will need to allow connections to Nginx. To list ufw
profiles, run:
sudo ufw app list
Available applications: Nginx Full Nginx HTTP Nginx HTTPS OpenSSH
Since we have not configured SSL certificate, we need allow only HTTP traffic on port 80
:
sudo ufw allow 'Nginx HTTP'
Verify the change by running:
sudo ufw status
It will show that HTTP
traffic is now allowed:
Status: active
To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
Nginx HTTP ALLOW Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)
Nginx HTTP (v6) ALLOW Anywhere (v6)
To verify installation, open your web browser and enter your SERVER_IP_OR_DOMAIN
as url and it should open nginx default page as given below.
http://SERVER_IP_OR_DOMAIN

Step 2 – Installing MySQL
Now we need to install the database system to store and manage data for website. MySQL is a popular database management system. By default, MySQL 8.0 is available in Ubuntu repositories. Run the below command to install:
sudo apt install mysql-server
Once the installation finished, it’s recommended that you run a MySQL in-built security script.
sudo mysql_secure_installation
You will be asked to set the root password, remove the anonymous user, restrict root user access to the local machine, and remove the test database. You should answer Y
(yes) to all questions.
Step 3 – Installing PHP
You have installed Nginx and MySQL. Now we will install PHP to serve dynamic content for the web server.
PHP 7.4 packages are available under the default repositories on Ubuntu 20.04 LTS. We will install php-fpm module along with an additional helper package, php-mysql. php-mysql will allow PHP to communicate with your database backend. Execute below command:
sudo apt install php-fpm php-mysql
You now have your PHP components installed. Next, we’ll configure Nginx.
Step 4 – Configuring Nginx to Process PHP Pages
At this point, you have installed all the components of LEMP. Edit the Nginx server block configuration file and add the following lines so that Nginx can process PHP files:
server {
# other code
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php4.0-fpm.sock;
}
}
Save and close the file. Restart the Nginx service for the changes to take effect:
sudo systemctl restart nginx
Step 5 – Test PHP with Nginx
Now your LEMP components are installed and ready to test it. Create a test PHP file called info.php
in your document root:
sudo nano /var/www/html/info.php
Add following code lines to this file:
<?php
phpinfo();
Save and close the file. Open this page using your server’s public ip address followed by /info.php
as following:
http://SERVER_IP_OR_DOMAIN/info.php
You will see a web page containing detailed information about your server:

That’s it.
Conclusion
This tutorial shown you how to install LEMP (Nginx, MySql, PHP) stack on Ubuntu 20.04 Focal Fossa server.
If you have any questions or feedback, leave a comment below.
Leave a Reply