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.
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:
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:
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:
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.
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 /.
Install MariaDB
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.
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.
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.
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:
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
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:
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.
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:
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.