How To Install Nginx, MariaDB 10.5 & PHP8-FPM (LEMP Stack) on Ubuntu 20.04 LTS

Screenshot From 2021 01 14 13 11 42

Installing and configuring the LEMP Stack on Ubuntu 20.04 LTS (Focal Fossa) is quite easy. Simply take your time for a moment, or move your focus to learning mode. Well, for testing purpose, I strongly recomend you to using cheap cloud server or budget VPS as you can buy it at several cloud server providers like Vultr, Digital Ocean, or Linode. So you can deploy and destroy your server whenever you want and this of course will reduce your costs for servers needs. So you won’t spend too much money for renting a server.

However, you can also buy the cloud server on another recommended cloud server providers, including Google Cloud Platform (GCP), Amazon Web Services (AWS) or OVH. It’s highly-recommended to using GCP, AWS, OVH for production server environment, not development purposes.

Ok, I will use $5 droplet from Digital Ocean, and deploy it with Ubuntu 20.04 LTS (Focal Fossa) x86_64. But, PLEASE NOTE that we will build LEMP Stack with CLI-Based Interface only. So there is only command line access to manage the server, and you don’t get any Graphical User Interface (GUI) at all.

Let’s follow this simple tutorial to build the latests LEMP Stack on your virtual machine.


  • Cloud Server Running Ubuntu 20.04 LTS
  • Server IPv4 Address
  • Full-Root Access
  • gNome Terminal for Linux Users
  • PuTTy SSH Client for Windows/Mac

1.) Update Your System Firts

Since this is freshly deploying cloud server, so we need to run system update firts in order to get update and security patch from Canonical as Ubuntu OS vendor.

$ apt update
$ apt upgrade

2.) Install Nginx Web Server

After you run system update on your machine, you should get the newest software packages, including Kernel Linux. So let’s start build Nginx web server and we can simply install it from Ubuntu official repository.

$ apt install nginx

3.) Build NGINX Extras

Unlike regular Nginx web server, NGINX Extras come with several dynamic Nginx module packages which allow you to expand the base functionality of Nginx web server. So, you can build Nginx from simple web server to modern LEMP Stack with more package modules and latest technologies like FastCGI-Cache, LUA module or Brotli compression.

For Nginx Extras, you can install it using the following command.

$ apt install nginx-extras

Let’s check your Nginx version

$ nginx -V
nginx version: nginx/1.18.0 (Ubuntu)
built with OpenSSL 1.1.1f 31 Mar 2020
TLS SNI support enabled
configure arguments: --with-cc-opt='-g -O2 -fdebug-prefix-map=/build/nginx-5J5hor/nginx-1.18.0=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -fPIC' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/ --modules-path=/usr/lib/nginx/modules --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-compat --with-pcre-jit --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_v2_module --with-http_dav_module --with-http_slice_module --with-threads --with-http_addition_module --with-http_flv_module --with-http_geoip_module=dynamic --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_mp4_module --with-http_perl_module=dynamic --with-http_random_index_module --with-http_secure_link_module --with-http_sub_module --with-http_xslt_module=dynamic --with-mail=dynamic --with-mail_ssl_module --with-stream=dynamic --with-stream_ssl_module --with-stream_ssl_preread_module --add-dynamic-module=/build/nginx-5J5hor/nginx-1.18.0/debian/modules/http-headers-more-filter --add-dynamic-module=/build/nginx-5J5hor/nginx-1.18.0/debian/modules/http-auth-pam --add-dynamic-module=/build/nginx-5J5hor/nginx-1.18.0/debian/modules/http-cache-purge --add-dynamic-module=/build/nginx-5J5hor/nginx-1.18.0/debian/modules/http-dav-ext --add-dynamic-module=/build/nginx-5J5hor/nginx-1.18.0/debian/modules/http-ndk --add-dynamic-module=/build/nginx-5J5hor/nginx-1.18.0/debian/modules/http-echo --add-dynamic-module=/build/nginx-5J5hor/nginx-1.18.0/debian/modules/http-fancyindex --add-dynamic-module=/build/nginx-5J5hor/nginx-1.18.0/debian/modules/nchan --add-dynamic-module=/build/nginx-5J5hor/nginx-1.18.0/debian/modules/http-lua --add-dynamic-module=/build/nginx-5J5hor/nginx-1.18.0/debian/modules/rtmp --add-dynamic-module=/build/nginx-5J5hor/nginx-1.18.0/debian/modules/http-uploadprogress --add-dynamic-module=/build/nginx-5J5hor/nginx-1.18.0/debian/modules/http-upstream-fair --add-dynamic-module=/build/nginx-5J5hor/nginx-1.18.0/debian/modules/http-subs-filter --add-dynamic-module=/build/nginx-5J5hor/nginx-1.18.0/debian/modules/http-geoip2

4.) Install and Configure MariaDB 10.5 Database Server

After having Nginx web server installed on your machine, you can move to the next step to build the latest stable release of MariaDB 10.5 database server.

Firts of all, you must adding MariaDB 10.5 repository on your Ubuntu system. Simply run the following commands.

$ apt install software-properties-common
$ apt-key adv --fetch-keys ''
$ add-apt-repository 'deb [arch=amd64,arm64,ppc64el] focal main'

After you install software dependencies and mariadb signing key, you can moving forward to install MariaDB 10.5 database server.

$ apt update
$ apt install mariadb-server

5.) Securing Your MariaDB Installation

On the Step-4 above we don’t actually setup root password for MariaDB/MySQL. Instead, the system give you temporary password for MariaDB/MySQL. So we need to restart MariaDB in order to get temporary MariaDB root password and then you must securing your MariaDB installation.

$ systemctl restart mysql.service

Simply run this command to securing your MariaDB installation

$ mysql_secure_installation
root@ubuntu-s-1vcpu-1gb-nyc3-01:~# mysql_secure_installation


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
haven't set the root password yet, you should just press enter here.

Enter current password for root (enter for none): [simply press ENTER]
OK, successfully used password, moving on...

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

You already have your root account protected, so you can safely answer 'n'.

Switch to unix_socket authentication [Y/n] n
... skipping.

You already have your root account protected, so you can safely answer 'n'.

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

By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y
... Success!

Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y
... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y
... Success!

Cleaning up...

All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

Verifying Your MariaDB Version

$ mysql -V
mysql Ver 15.1 Distrib 10.5.8-MariaDB, for debian-linux-gnu (x86_64) using readline 5.2

6.) Let’s Build PHP 8.0 with FastCGI Process Manager (FPM)

Ubuntu 20.04 LTS (Focal Fossa) is shipped with PHP 7.4 as the official default PHP version. So, in order to ge the latest PHP 8.0, we must add personal package archieve or PPA from Ondřej Surý. But, we need to install both python3-software-properties and software-properties-common if the system doesn’t have it previously.

$ apt install python3-software-properties -y
$ apt install software-properties-common -y

Let’s adding the Ondřej PPA repository to your Ubuntu machine using the following command.

$ LC_ALL=C.UTF-8 add-apt-repository ppa:ondrej/php

Simply press “ENTER” after you run the above command.

And then let’s run update command again on your machine.

$ apt update

After that, you can install PHP 8.0 along with its extensions, including PHP-FPM, Memcached, Redis, Mbstring, GMP, and any other PHP extensions.

$ apt install php8.0 php-apcu php8.0-common php8.0-mysql php8.0-xmlrpc php8.0-curl php8.0-gd php8.0-cgi php8.0-cli php8.0-fpm php-pear php8.0-dev php8.0-imap php8.0-mbstring php8.0-xml php8.0-tidy php8.0-sqlite3 php8.0-xsl php8.0-gmp php8.0-zip php8.0-soap php-redis memcached php-memcache

Verify your PHP Version

$ php -v
root@ubuntu-s-1vcpu-1gb-nyc3-01:~# php -v
PHP 8.0.1 (cli) (built: Jan 13 2021 08:22:35) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.1, Copyright (c) Zend Technologies
with Zend OPcache v8.0.1, Copyright (c), by Zend Technologies

From this step, we’ve actually finished in configuring LEMP Stack on Ubuntu 20.04 machine. However, it’s still not completed yet since you don’t setting up corret server block or virtual host for your domain to run with Nginx web service. Also, you need to correct PHP-FPM pool directives inside /etc/php/8.0/fpm/pool.d/, so Nginx can communicate properly with PHP8.0-FPM.

You can read my previous articles to creating Nginx server block for your domains and edit PHP-FPM default pool directivies. The following articles may help you to build Nginx server block and correct pool directives for PHP8.0-FPM.

* Tutorial Creating Nginx ServerBlock for New Site
* Setting Up Nginx with FastCGI-Cache Module on Ubuntu 20.04 LTS (Focal Fossa)


Setting up and configuring LEMP Stack (Linux, Nginx, MariaDB 10.5 & PHP8.0-FPM) on Ubuntu 20.04 LTS is quite easy, but you need relax time to doing this simple task.