How To Install LEMP stack on Ubuntu 20.04

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.

How To Install LEMP Stack on Ubuntu 20.04

Before you start to install the LEMP stack on 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, let’s start to install the Nginx web server on Ubuntu 20.04.

Install Nginx on Ubuntu 20.04

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
Output
Available applications:
  Nginx Full
  Nginx HTTP
  Nginx HTTPS
  OpenSSH

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:

Output
Status: active
To                         Action      From
OpenSSH                    ALLOW       Anywhere
Nginx HTTP                 ALLOW       Anywhere
OpenSSH (v6)               ALLOW       Anywhere (v6)
Nginx HTTP (v6)            ALLOW       Anywhere (v6)

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 pointed 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.

Nginx default landing page-LEMP stack

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.

Install MySQL on Ubuntu 20.04

Here, 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 that 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:

Output
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.26-0ubuntu0.20.04.3 (Ubuntu)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

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.

Install PHP on 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 dependency with the following command:

sudo apt install php-fpm php-mysql

Now you need to configure Nginx to use the PHP components.

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:

Output
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

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:

landing page of your domain

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.

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
PHP info page-LEMP stack on Ubuntu 20.04

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.

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 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:

Output
+--------------------+
| Database           |
+--------------------+
| orca_database   |
| information_schema |
+--------------------+
2 rows in set (0.000 sec)

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:

Output
+---------+--------------------------+
| item_id | content                  |
+---------+--------------------------+
|       1 | My first important item  |
|       2 | My second important item |
|       3 | and this is one more thing  |
+---------+--------------------------+
3 rows in set (0.000 sec)

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:

To do list PHP on Ubuntu 20.04

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 the LEMP stack on Ubuntu 20.04.

May this article about How to install LAMP stack on Ubuntu 20.04 be useful for you.

You Maybe Interested in these articles:

How To Install LEMP Stack on Debian 11

How to Secure Nginx with Let’s Encrypt on AlmaLinux 8

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Stay informed and not overwhelmed, subscribe now!