Install LEMP Stack on Ubuntu 20.04 with Easy Steps
In this article, we want to teach you How To Install the LEMP stack on Ubuntu 20.04. LEMP stack stands for Linux, Nginx, MySQL, and PHP. It is a group of software that can be used to serve dynamic web pages and web applications written in PHP. The LEMP stack is a popular alternative to the LAMP stack, replacing Apache with the more lightweight and performant Nginx web server. Let’s dive into setting up a LEMP stack.
Now you can proceed to the guide steps below on the Orcacore website to complete LEMP Stack Ubuntu 20.04 Setup.
Before installing the LEMP Stack Ubuntu 20.04, you need some requirements.
You need to log in to your server as a non-root user with Sudo privileges and set up a basic firewall. To do this you can check our article about the Initial server setup with Ubuntu 20.04.
When you are done with this, follow the steps below to complete the LEMP Stack Ubuntu 20.04 Setup.
1. Install Nginx For LEMP Stack Ubuntu 20.04
The first step in completing the LEMP Stack Ubuntu 20.04 Setup is to install Nginx. Nginx is available in the Ubuntu default repository. first, update the APT packages with the following command:
sudo apt update
Then, install Nginx on Ubuntu 20.04 with the following command:
sudo apt install nginx
We assumed that you have enabled the UFW firewall with the requirements. you need to allow connections to Nginx.
Check the available applications through the UFW with the following command:
sudo ufw app list

In this article, you only need to allow Nginx to HTTP traffic on port 80 with the following command:
sudo ufw allow 'Nginx HTTP'
Now you can verify the changes with the following command:
sudo ufw status
Your output should be similar to this:

Now you can test that your Nginx web server is up and running by typing your domain name or your server’s public IP address in your web browser.
If you don’t have a domain name that points to your domain name, you can use your server’s public IP address.
To get your IP address you can use the following command:
hostname -I
Or you can use the curl tool to get it:
curl -4 icanhazip.com
Now you can access Nginx’s default landing page by typing the IP address that you have got in your web browser:
http://your_domain_or_IP
If you see this page, means that your web server is successfully installed and HTTP traffic is enabled for it.

At this point, you are done with installing Nginx the first part of the LEMP stack on Ubuntu 20.04. Let’s start to install MySQL on Ubuntu 20.04.
2. Install MySQL For LEMP Stack Ubuntu 20.04
For the second step of LEMP Stack Ubuntu 20.04, you need to install MySQL the database system to store and manage data for your site with the following command:
sudo apt install mysql-server
For more security, it’s recommended to run a security script that comes with the pre-installation of MySQL:
sudo mysql_secure_installation
You will be asked some questions. The first is to configure VALIDATE PASSWORD PLUGIN. you can leave it blank safely. but if you choose to set a password for it choose a strong one.
Next, is to set a password for the MySQL root user. from there, you can press Y for the rest of the questions to accept the defaults.
When you are finished, log in to your MySQL console with the following command:
sudo mysql
In your output you will see:

Then you can exit from the MySQL console with the following command:
mysql> exit
At this point, you are done with installing MySQL the second part of the LEMP stack on Ubuntu 20.04. Let’s start to install PHP on Ubuntu 20.04.
3. Install PHP For LEMP Stack Ubuntu 20.04
You have Nginx installed to serve your content and MySQL installed to store and manage your data. Now you can install PHP to process code and generate dynamic content for the webserver.
You can install PHP and its dependencies with the following command:
sudo apt install php-fpm php-mysql
Now you need to configure Nginx to use the PHP components.
4. Configure Nginx to use PHP components
You can create Nginx server blocks that are similar to Apache virtual hosts. In this article, we use nginx.orcacore.com as our domain. Remember to replace it with your own domain in the commands.
First, create the root web directory for your domain with the following command:
sudo mkdir /var/www/nginx.orcacore.com
Then, assign ownership of the directory with the $USER environment variable with the following command:
sudo chown -R $USER:$USER /var/www/nginx.orcacore.com
Next, open a new configuration file in the Nginx’s sites-available directory with your favorite text editor, here we use vi:
sudo vi /etc/nginx/sites-available/nginx.orcacore.com
Then, add the following bare-bones configuration to the file:
server {
listen 80;
server_name nginx.orcacore.com www.nginx.orcacore.com;
root /var/www/nginx.orcacore.com;
index index.html index.htm index.php;
location / {
try_files $uri $uri/ =404;
}
location ~ .php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
location ~ /.ht {
deny all;
}
}
When you are finished, save and close the file.
Now you need to activate your configuration by linking to the config file from Nginx’s sites-enabled directory with the following command:
sudo ln -s /etc/nginx/sites-available/nginx.orcacore.com /etc/nginx/sites-enabled/
You can test your configuration for no syntax error with the following command:
sudo nginx -t
In your output you should see:

If you see errors, open the file again and check for typos and missing characters.
To apply these changes reload Nginx:
sudo systemctl reload nginx
Now you need to create a file in the webroot named index.html. so that you can test your new server blocks:
vi /var/www/nginx.orcacore.com/index.html
Then, add the following content to your file:
<html>
<head>
<title>nginx.orcacore.com website</title>
</head>
<body>
<h1>Hello World!</h1>
<p>This is the landing page of <strong>nginx.orcacore.com</strong>.</p>
</body>
</html>
Now type your domain name or IP address in your web browser:
http://your_domain_or_IP
You will see a page like this:

If you see this page, means that your server blocks work correctly.
Now your LEMP stack is fully configured on Ubuntu 20.04.
In the next step, you need to create a PHP script to test that Nginx is able to handle PHP files.
5. How To Test PHP with Nginx on Ubuntu 20.04?
The LEMP stack should now be completely set up on Ubuntu 20.04. you can test it to validate that Nginx is able to hand .php files.
Create a file in the webroot directory named info.php with the following command:
vi /var/www/nginx.orcacore.com/info.php
Add the following PHP code to the file:
<?php
phpinfo();
Save and close the file, when you are finished.
you can access the PHP information page by visiting the domain name or public IP address you’ve set up in your Nginx configuration file, followed by info.php:
http://your_domain_or_IP/info.php

After checking the information about your PHP server, it’s recommended to remove it for more security.
sudo rm /var/www/nginx.orcacore.com/info.php
At this point, when you are completely done with installing the LEMP stack on Ubuntu 20.04 and testing your PHP server, you can test the database connection from PHP.
6. Test Database Connection from PHP
If you want to test whether PHP is able to connect to MySQL and execute database queries, you can create a test table with dummy data and query for its contents from a PHP script.
Here we create a database named orca_database
and a user named olivia_user
. you can replace it with your own values.
Create a Test Database on Mysql
Open the MySQL console with the following command:
sudo mysql
Then, run the following command to create your database:
mysql> CREATE DATABASE orca_database;
Now you can create a new user and give them full privileges on the custom database you’ve just created with the following command:
Remember to choose a strong password for your user.
mysql> CREATE USER 'olivia_user'@'%' IDENTIFIED WITH mysql_native_password BY 'password';
Here you need to give this user permissions over orca_database
with the following command:
mysql> GRANT ALL ON orca_database.* TO 'olivia_user'@'%';
Exit the MySQL shell:
mysql> exit
To test that the user has the correct permission, open the MySQL console again with the custom user credentials:
mysql -u olivia_user -p
After you log into the MySQL console, confirm that you have access to the orca_database
with the following command:
mysql> SHOW DATABASES;
Your output should be similar to this:

Next, you need to create a test table named todo_list
. From the MySQL console, run the following command:
mysql> CREATE TABLE orca_database.todo_list (
item_id INT AUTO_INCREMENT,
content VARCHAR(255),
PRIMARY KEY(item_id)
);
Insert a few rows of content in the test table. You might want to repeat the next command a few times with different values.
mysql> INSERT INTO orca_database.todo_list (content) VALUES ("My first important item");
Now verify that the data was successfully saved with the following command:
mysql> SELECT * FROM orca_database.todo_list;
In your output you will see something like this:

Then, exit from the MySQL shell:
mysql> exit
Create a PHP file for testing Mysql
Next, you need to create a new PHP file in your custom web root directory with your favorite text editor:
vi /var/www/nginx.orcacore.com/todo_list.php
Copy the following content with your own values into the file:
<?php
$user = "olivia_user";
$password = "password";
$database = "orca_database";
$table = "todo_list";
try {
$db = new PDO("mysql:host=localhost;dbname=$database", $user, $password);
echo "<h2>TODO</h2><ol>";
foreach($db->query("SELECT content FROM $table") as $row) {
echo "<li>" . $row['content'] . "</li>";
}
echo "</ol>";
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
Save and close the file when you are finished.
Now you can access this page by typing your domain name or IP address in your web browser followed by todo_list.php:
http://your_domain_or_IP/todo_list.php
You should see a page like this with the content you’ve inserted in your test table:

That means your PHP environment is ready to connect and interact with your MySQL server.
Conclusion
At this point, you learn what is LEMP stack and you can easily install it on your server and use it. Hope you enjoy this article about How To Install LEMP stack on Ubuntu 20.04. The LEMP stack provides a robust and efficient platform for deploying web applications.
May this article about How to install LAMP stack on Ubuntu 20.04 be useful for you.
You may also interested in these articles:
How To Install LEMP Stack on Debian 11
How to Secure Nginx with Let’s Encrypt on AlmaLinux 8
Alternative Solutions for Installing LEMP Stack on Ubuntu 20.04
While the above method outlines a manual installation of the LEMP stack, alternative approaches exist that can streamline the process. Here are two different ways to achieve the same goal, offering increased speed and convenience:
1. Using Tasksel
Tasksel is a Debian/Ubuntu tool that simplifies the installation of multiple related packages as a co-ordinated "task". While not explicitly a "LEMP stack installer," it can be leveraged to install a LAMP stack, which can then easily be modified to become a LEMP stack by removing Apache and installing Nginx.
- Explanation: Tasksel automates the installation process by handling dependencies and configurations for a pre-defined set of packages. It’s a more user-friendly approach for beginners who prefer a guided experience.
-
Steps:
- Install Tasksel:
sudo apt update sudo apt install tasksel
- Run Tasksel and select "LAMP server":
sudo tasksel
(Navigate using arrow keys, select with spacebar, and press Enter to install.)
- Remove Apache:
sudo apt remove apache2
- Install Nginx
sudo apt install nginx
- Follow the steps for configuring Nginx to use PHP components from the main article.
2. Using Docker Compose
Docker Compose defines and manages multi-container Docker applications. You can create a docker-compose.yml
file to define the Nginx, MySQL, and PHP-FPM services, along with their configurations, and then start the entire stack with a single command.
- Explanation: Docker provides a consistent and isolated environment for your application, ensuring that it runs the same way regardless of the underlying infrastructure. Docker Compose simplifies the management of multi-container applications. This approach is beneficial for development and deployment as it encapsulates the entire stack within containers.
- Code Example (
docker-compose.yml
):
version: "3.8"
services:
nginx:
image: nginx:latest
ports:
- "80:80"
volumes:
- ./nginx/conf.d:/etc/nginx/conf.d
- ./app:/var/www/html
depends_on:
- php
php:
image: php:7.4-fpm
volumes:
- ./app:/var/www/html
db:
image: mysql:5.7
environment:
MYSQL_ROOT_PASSWORD: your_root_password
MYSQL_DATABASE: orca_database
MYSQL_USER: olivia_user
MYSQL_PASSWORD: password
ports:
- "3306:3306"
volumes:
- db_data:/var/lib/mysql
volumes:
db_data:
-
Steps:
- Install Docker and Docker Compose: Follow the official Docker documentation for installing Docker and Docker Compose on Ubuntu 20.04.
- Create the
docker-compose.yml
file with the above content. - Create the necessary directories:
mkdir nginx mkdir nginx/conf.d mkdir app
- Create an Nginx configuration file (
nginx/conf.d/default.conf
):
server {
listen 80;
index index.php index.html;
root /var/www/html;
location ~ .php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
5. Create a PHP file (e.g., `app/index.php`) to test the setup.
6. Start the stack:
```bash
docker-compose up -d
```
These alternative solutions offer different levels of automation and control. Tasksel provides a simpler, guided installation, while Docker Compose offers greater flexibility and portability through containerization. The best approach depends on your specific needs and comfort level with these tools.