In this guide we show you how to install the LEMP stack (NGINX, MariaDB and PHP 7.4) on Ubuntu 20.04. A software stack is a collection of software tools bundled together. LEMP stands for Linux, NGINX(Engine-X), MariaDB/MySQL and PHP, all of which are open source and free to use. It is the most common software stack that supports dynamic websites and web applications. Linux is an operating system; NGINX is a web server; MariaDB/MySQL is a database server and PHP is a server-side scripting language responsible for creating dynamic web pages.
Updating software packages
Before we install the LEMP stack, it is recommended to update the repository and software packages by running the following commands:
apt update && apt upgrade
We did not have an update and we will continue immediately on.
NGINX web server installation
NGINX is a high performance web server that is very popular these days. It can also be used as a reverse proxy and caching server. Enter the following command to install the NGINX web server:
apt install nginx

After installing it, we can enable NGINX to start automatically at boot time by running the following command:
systemctl enable nginx
Then start NGINX with this command:
systemctl start nginx
Now check its status:
systemctl status nginx

“Enabled” means that autostart at boot time is enabled and we can see that NGINX is running. You can also see how much RAM NGINX is using from the output. If the above command does not exit immediately after running. You need to press “q” to exit.
Check the NGINX version:
nginx -v
We have the installed version:
root@firstbyte:~# nginx -v
nginx version: nginx/1.18.0 (Ubuntu)
Now enter the IP address of your server in the address bar of your browser. You should see a “Welcome to nginx!” web page, which means the NGINX web server is working correctly.

Finally, we need to create “www-data” (the NGINX user) the owner of the web folder. By default, it is owned by the root user.
chown www-data:www-data /usr/share/nginx/html -R
Now we can safely upload the necessary files and they will be visible to the world.
Installing the MariaDB Database Server
MariaDB is a replacement for MySQL. It is developed by former members of the MySQL team who are concerned that Oracle might turn MySQL into a closed source product. Enter the following command to install MariaDB:
apt install mariadb-server mariadb-client

Confirm that we want to install this software and wait a couple of minutes for the installation to complete:
After installation, the MariaDB server should be started automatically. Use systemctl to check its status:
systemctl status mariadb

If it is not running, start it with this command:
systemctl start mariadb
To allow MariaDB to start automatically during OS boot, use the command:
systemctl enable mariadb
Now run the post-install security script:
mysql_secure_installation
When it asks you to enter the MariaDB root password, press the Enter key as the root password has not been set yet. Then enter “Y” to set the root password for the MariaDB server.
You can then press Enter to answer any remaining questions, which will remove the anonymous user, disable remote root login, and delete the test database. This step is a basic requirement for securing the MariaDB database.

By default, the MariaDB package in Ubuntu uses a unix_socket to authenticate the user on login, which basically means that you can use the OS username and password to login to the MariaDB console. So you can run the following command to login without providing the MariaDB root password.
mariadb -u root
mariadb -u root
To exit the MariaDB console, run the command:
exit;
Check the MariaDB server version information:
mariadb --version

As you can see, we have installed MariaDB, version 10.3.38.
Installing PHP
PHP 7.4 is the latest stable version of PHP for Ubuntu 20.04 and has a slight performance advantage over PHP 7.3. Enter the following command to install PHP 7.4 and some common PHP modules:
apt install php7.4 php7.4-fpm php7.4-mysql php-common php7.4-cli php7.4-common php7.4-json php7.4-opcache php7.4-readline php7.4-mbstring php7.4-xml php7.4-gd php7.4-curl
PHP extensions are usually required for content management systems (CMS) such as WordPress. For example, if php7.4-xml is missing from your installation, then some pages of your WordPress site may be empty, and you may find an error in the NGINX error logs, such as:
PHP message: PHP Fatal error: Uncaught Error: Call to undefined function xml_parser_create()
Installing these PHP extensions will ensure that your CMS runs smoothly. Now run php7.4-fpm:
systemctl start php7.4-fpm
It’s also a good idea to enable autorun at boot time:
systemctl enable php7.4-fpm
We can then check its status:

If the above command does not exit immediately after running. You need to press “q” to exit.
How to create an NGINX server block
The NGINX server block is similar to the virtual host in Apache. We won’t use the default server block because it’s not suitable for running PHP code and if we change it it will turn into a mess. So remove the default symlink in the sites-enabled folder by running the following command: (It’s still available as /etc/nginx/sites-available/default.)
rm /etc/nginx/sites-enabled/default
Then use the “nano” command line text editor to create a new server block file in the /etc/nginx/conf.d/ folder:
nano /etc/nginx/conf.d/default.conf
Paste the following text into the file. The following snippet will make NGINX listen on port 80:
server { listen 80; listen [::]:80; server_name _; root /usr/share/nginx/html/; index index.php index.html index.htm index.nginx-debian.html; location / { try_files $uri $uri/ /index.php; } location ~ \.php$ { fastcgi_pass unix:/run/php/php7.4-fpm.sock; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; include snippets/fastcgi-php.conf; } # A long browser cache lifetime can speed up repeat visits to your page location ~* \.(jpg|jpeg|gif|png|webp|svg|woff|woff2|ttf|css|js|ico|xml)$ { access_log off; log_not_found off; expires 360d; } # disable access to hidden files location ~ /\.ht { access_log off; log_not_found off; deny all; } }
Save the file by pressing CTRL+X and then Enter.

Then test the NGINX configurations:
nginx -t

If the test was successful, reload NGINX:
systemctl restart nginx
Test PHP
To test PHP-FPM with the NGINX web server, we need to create an info.php file in the root folder where we intend to load the site files:
nano /usr/share/nginx/html/info.php
Paste the following PHP code into the file:
<?php phpinfo(); ?>
And save the file by pressing CTRL+X and then press Enter.
Now go to the info page using the link:
http://IP-ADDRESS/info.php

You did it! You have successfully installed NGINX, MariaDB and PHP 7.4 on Ubuntu 20.04. For the security of your server, you should delete the info.php file now so that attackers don’t see it.
rm /usr/share/nginx/html/info.php
Great! Now no one but you or your team will know exactly what your web server and PHP configuration is.
Conclusion
We hope that it was very useful for you to teach how you can install LEMP on your server with Ubuntu 20.04 OS.