How to Install LEMP Stack (Nginx, MariaDB, PHP7.4) on Ubuntu 20.04

Updating the system

We first update the system to make sure that all our installed packages are upto date. Your Ubuntu system can be updated easily with the following command.

sudo apt update

sudo apt upgrade

Install Nginx

We will start by installing the Nginx web server. To complete the installation, use the following command.

apt install nginx

Output:

root@vps:~# apt install nginx
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
fontconfig-config fonts-dejavu-core libfontconfig1 libgd3 libjbig0
libjpeg-turbo8 libjpeg8 libnginx-mod-http-image-filter
libnginx-mod-http-xslt-filter libnginx-mod-mail libnginx-mod-stream libtiff5
libwebp6 libxpm4 nginx-common nginx-core
Suggested packages:
libgd-tools fcgiwrap nginx-doc ssl-cert

Once the installation is complete, enable Nginx (to start automatically upon system boot), start the web server and verify the status using the commands below.

systemctl start nginx

systemctl enable nginx

systemctl status nginx

Output:

root@vps:~# systemctl status nginx
● nginx.service - A high performance web server and a reverse proxy server
 Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset:>
 Active: active (running) since Tue 2020-04-28 16:46:36 UTC; 3min 13s ago
   Docs: man:nginx(8)
 Main PID: 1630 (nginx)
  Tasks: 3 (limit: 1074)
 Memory: 5.2M
 CGroup: /system.slice/nginx.service
         ├─1630 nginx: master process /usr/sbin/nginx -g daemon on; master_>
         ├─1631 nginx: worker process
         └─1632 nginx: worker process

Apr 28 16:46:35 vps.server.com systemd[1]: Starting A high performance web serv>
Apr 28 16:46:36 vps.server.com systemd[1]: Started A high performance web serve>

To check Nginx version.

nginx -v

Output:

[root@vps ~]# nginx -v
nginx version: nginx/1.17.10 (Ubuntu)

Verify that the web server is running and accessible by accessing your server’s IP address.

From your browser,

http://IP_address

image

we need to make user nginx as the owner of web directory. By default it’s owned by the root user.

chown www-data:www-data /usr/share/nginx/html -R

Install MariaDB Server

MariaDB is a popular database server. The installation is simple and requires just a few steps as shown.

apt install mariadb-server mariadb-client

Output:

root@vps:~# apt install mariadb-server mariadb-client
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
galera-3 libcgi-fast-perl libcgi-pm-perl libconfig-inifiles-perl
libdbd-mysql-perl libdbi-perl libencode-locale-perl libfcgi-perl
libhtml-parser-perl libhtml-tagset-perl libhtml-template-perl
libhttp-date-perl libhttp-message-perl libio-html-perl
liblwp-mediatypes-perl libmysqlclient21 libsnappy1v5 libterm-readkey-perl
libtimedate-perl liburi-perl mariadb-client-10.3 mariadb-client-core-10.3
mariadb-common mariadb-server-10.3 mariadb-server-core-10.3 mysql-common

Once the installation is complete, enable MariaDB (to start automatically upon system boot), start the mariaDB and verify the status using the commands below.

systemctl start mariadb

systemctl enable mariadb

systemctl status mariadb

Output:

root@vps:~# systemctl status mariadb
● mariadb.service - MariaDB 10.3.22 database server
 Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled)
 Active: active (running) since Tue 2020-04-28 16:56:50 UTC; 1min 21s ago
   Docs: man:mysqld(8)
         https://mariadb.com/kb/en/library/systemd/
  Main PID: 2580 (mysqld)
 Status: "Taking your SQL requests now..."
  Tasks: 31 (limit: 1074)
 Memory: 64.7M
 CGroup: /system.slice/mariadb.service
         └─2580 /usr/sbin/mysqld

Finally, you will want to secure your MariaDB installation by issuing the following command.

mysql_secure_installation

Output:

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
 ... Success!

Once secured, you can connect to MySQL and review the existing databases on your database server by using the following command.

mysql -e "SHOW DATABASES;" -p

Output:

root@vps:~# mysql -e "SHOW DATABASES;" -p
Enter password:
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+

Install PHP 7.4

To Install PHP 7.4, use the command.

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

To start php7.4-fpm.

systemctl start php7.4-fpm

To auto-start on boot php7.4-fpm.

systemctl enable php7.4-fpm

To Check status.

systemctl status php7.4-fpm

Output:

root@vps:~# systemctl status php7.4-fpm
● php7.4-fpm.service - The PHP 7.4 FastCGI Process Manager
 Loaded: loaded (/lib/systemd/system/php7.4-fpm.service; enabled; vendor preset: enabled)
 Active: active (running) since Tue 2020-04-28 17:04:26 UTC; 2min 26s ago
   Docs: man:php-fpm7.4(8)
Main PID: 13828 (php-fpm7.4)
 Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec"
  Tasks: 3 (limit: 1074)
 Memory: 9.4M
 CGroup: /system.slice/php7.4-fpm.service
         ├─13828 php-fpm: master process (/etc/php/7.4/fpm/php-fpm.conf)
         ├─13829 php-fpm: pool www
         └─13830 php-fpm: pool www

Setting Up Server Blocks

You need to remove the file default, located in /etc/nginx/sites-enabled.

rm /etc/nginx/sites-enabled/default

Create new server block file under /etc/nginx/conf.d/ directory.

nano /etc/nginx/conf.d/default.conf

Add following text into the file.

  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;
  }
}

Next, test to make sure that there are no syntax errors in any of your Nginx files.

nginx -t

If there aren’t any problems, restart Nginx to enable your changes.

systemctl reload nginx

Test PHP

To test PHP-FPM with Nginx Web server, we need to create a info.php file in the webroot directory.

nano /usr/share/nginx/html/info.php

Add the following PHP code into the file.

<?php phpinfo(); ?>

Now again, access http://localhost/info.php or http://yourserver-ip-address/info.php. You should see a page similar to below one.

image

Done!