LEMP (Nginx, MariaDB and PHP), installation on Ubuntu 20.04

about LEMP

In the next article we are going to take a look at how we can install LEMP (Nginx, MariaDB and PHP) on Ubuntu 20.04. This software stack is widely used for hosting small to large websites / blogs. If you are one of those who prefers to use the Apache server for these tasks, you may want to take a look at LAMP.

The LEMP software stack is a group of software elements that can be used to deliver dynamic web pages and web applications. This acronym describes a Linux operating systema Nginx web server. Backend data is stored with MariaDB y dynamic processing is managed through PHP. In the following lines we will see how to install all this locally, using a computer with Ubuntu 20.04.

Installing the LEMP software stack on Ubuntu 20.04

At the time of writing, if the instructions below are followed, We will be able to install the EMP packages (Nginx v1.19, PHP v7.4, MariaDB v10.3) in Ubuntu 20.04.

Install Nginx from repository

Nginx provides a repository for the Ubuntu operating system. The official Nginx repository includes version v1.19.

nginx version

To start installing Nginx from the repository, we are going to open a terminal (Ctrl + Alt + T) and update the list of available software with the command:

sudo apt update

The next thing we will do is install the some packages:

install certificates and curl

sudo apt install curl gnupg2 ca-certificates lsb-release

Next, in the same terminal we will add the necessary key and the repository to be able to install Nginx:

add the repository for nginx

curl -fsSL https://nginx.org/keys/nginx_signing.key | sudo apt-key add -

echo "deb [arch=amd64] http://nginx.org/packages/mainline/ubuntu focal nginx" | sudo tee /etc/apt/sources.list.d/nginx.list

Once the repository has been correctly added, we can proceed to the install Nginx package with the commands:

install nginx for lemp

sudo apt update; sudo apt install nginx

Once the installation is finished, we will start the Nginx service with the command:

sudo systemctl start nginx

Once the server is started, we can open a web browser and visit the IP address of our server. In this case, as I am doing it locally, it will be the IP of the computer on which I just installed it. You should see the default Nginx page, confirming that the server has been installed and is working properly.

nginx server running in web browser

The default document root of Nginx in Ubuntu 20.04 can be found in the folder / Usr / share / nginx / html and its configuration files in / etc / nginx /.

nginx files

Install MariaDB

mariadb LEMP version

The next step to follow will be to install the MariaDB server using the following command. By default, Ubuntu 20.04 includes MariaDB v10.3.

mariadb installation for LEMP

sudo apt install mariadb-server mariadb-client

Next, we will have to set root password and secure MariaDB instance using mysql_secure_installation command. To all the questions you ask us, there will be no more to answer 'y'. Although it is always good to read them.

sudo mysql_secure_installation

Install PHP-FPM

At this point we will to install PHP-FPM (PHP-FastCGI Process Manager) to display dynamic content written in PHP.

php version for LEMP

To install PHP-FPM what we will do is use the following command. By default, as of today Ubuntu 20.04 includes PHP-FPM v7.4.

install php-fpm for LEMP

sudo apt install php-fpm php-mysql php-cli

PHP-FPM listens on socket /run/php/php7.4-fpm.sock default. To make it use the TCP connection, we are going to edit the following file:

sudo vim /etc/php/7.4/fpm/pool.d/www.conf

Once in the file, we will change listening parameter:

listen = /run/php/php7.4-fpm.sock

By the following:

configuration www.conf PHP

listen = 127.0.0.1:9000

Once the changes have been made, we only have to save the file and close. The next thing we will do is restart PHP-FPM with the command:

sudo systemctl restart php7.4-fpm.service

Testing LEMP

As a test, we are going to create a name-based virtual host on the Nginx server to test our installation of the LEMP stack. The following names and addresses are only an example, that each user adapts them to their needs.

  • Domain name: site.betweenonesandzeroes.local
  • Root of the document: /www/site.entreunosyceros.local

We will start by creating a virtual host configuration file for our domain in directory /etc/nginx/conf.d/:

sudo vim /etc/nginx/conf.d/site.entreunosyceros.local.conf

virtual host configuration LEMP test

Inside the file, we will add the following content:

server {
server_name site.entreunosyceros.local;
root /www/site.entreunosyceros.local;

location / {
index index.html index.htm index.php;
}

location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}

Once the content is pasted, we save and close. Now let's create the root directory to place the PHP files:

sudo mkdir -p /www/site.entreunosyceros.local

The following will be change ownership of root directory:

sudo chown -R www-data:www-data /www/site.entreunosyceros.local/

For test PHP-FPM support, we will place a .php file in the root of the virtual host document with the command:

create a test file for LEMP

echo "<?php phpinfo(); ?>" | sudo tee /www/site.entreunosyceros.local/index.php

We continue restarting Nginx:

sudo systemctl restart nginx

Now let's create a host entry for the domain (in this example site.entreunosyceros.local) in the / etc / hosts file, in case our environment does not have a DNS server for name resolution.

sudo vim /etc/hosts

Inside the file, we will add a host entry as shown below.

local hosts file

10.0.2.15 site.entreunosyceros.local site

We save the changes and close the file. The next thing we will do is open the web browser and write the name of used in the address bar:

local site web browser php info

In the previous capture, You can see in the server API line that PHP works on our server through FPM / FastCGI.

And with this we can conclude the local installation of LEMP on Ubuntu 20.04.


Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.