How To Install Linux, Nginx, MariaDB, PHP (LEMP Stack) in Ubuntu 20.04

THIS DOCUMENT IS ALSO AVAILABLE FOR

LEMP stack is a technology stack, stands for Linux, Nginx, MariaDB, and PHP. It is widely used for hosting websites/blogs ranging from smaller to large scale.

Here, we will see how to install LEMP Stack on Ubuntu 20.04.

Install LEMP Stack

Install Linux

Here is the tutorial about Step by Step installation of Ubuntu 20.04 and Upgrading Ubuntu 18.04 or Ubuntu 19.10 to Ubuntu 20.04.

Proceed to the installation of EMP (Nginx v1.17.10, PHP v7.4, MariaDB v10.3) packages on Ubuntu 20.04.

Install Nginx

Nginx is a free, high-performance web server for serving static web content. It is known for its stability, simple configuration, and low resource consumption.

Install Nginx From Ubuntu repository

Install Nginx package using the following command. By default, Ubuntu 20.04 ships Nginx v1.17.10.

sudo apt update

sudo apt install -y nginx

Open up a web browser and visit your server IP address.

http://your-ip-add-ress

You should see the Nginx’s default page, and this confirms that the Nginx web server is successfully installed and working.

Nginx’s Default Page
Nginx’s Default Page

Nginx’s default document root on Ubuntu 20.04 is /var/www/html and its configuration files are found under /etc/nginx/ directory.

Install Nginx From Nginx repository

Nginx provides repository for Ubuntu operating system. Nginx’s official repository ships v1.17.11.

Install Nginx from Nginx’s repository with the below commands.

sudo apt update

sudo apt install -y curl gnupg2 ca-certificates lsb-release

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

Install Nginx package using the following command.

sudo apt update

sudo apt install -y nginx

Start the Nginx service after the installation.

sudo systemctl start nginx

Open up a web browser and visit your server IP address.

http://your-ip-add-ress

You should see the Nginx’s default page, and this confirms that the Nginx web server is successfully installed and working.

Nginx’s Default Page
Nginx’s Default Page

Nginx’s default document root on Ubuntu 20.04 is /usr/share/nginx/html and its configuration files are found under /etc/nginx/ directory.

Install MariaDB Server

Install the MariaDB server by using the following command. By default, Ubuntu 20.04 ships MariaDB v10.3.

READ: How To Install MariaDB On Ubuntu 20.04 - Official Repository

sudo apt install -y mariadb-server mariadb-client

You can also install MySQL 8.0 for LEMP stack.

Next, set the MariaDB root password and secure the MariaDB instance using the mysql_secure_installation command.

sudo 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  << Set MariaDB root Password
New password: xxx << Enter Password
Re-enter new password: xxx << Re-Enter 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 << Remove Anonymous User
 ... 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 << Disallow root login remotely
 ... 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 << Remote test database for security
 - 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 << Reload Tables
 ... 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!

Install PHP-FPM

Now, Install PHP-FPM (PHP-FastCGI Process Manager) for displaying dynamic content written in PHP.

Install PHP-FPM by using the following command. By default, Ubuntu 20.04 ships PHP-FPM v7.4.

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

PHP-FPM listens on the socket /run/php/php7.4-fpm.sock by default.

To make PHP-FPM to use TCP connection, edit the below file.

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

Then, change the listen parameter.

FROM:

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

TO:

listen = 127.0.0.1:9000

Restart the PHP-FPM process.

sudo systemctl restart php7.4-fpm

Test LEMP Stack

Let’s create a name-based virtual host on the Nginx server to test our LEMP stack installation.

Domain Name: site.itzgeek.local

Document Root: /www/site.itzgeek.local

Create a virtual host configuration file for our domain in /etc/nginx/conf.d/ directory.

sudo nano /etc/nginx/conf.d/site.itzgeek.local.conf

Add the following content.

server {
   server_name site.itzgeek.local;
   root /www/site.itzgeek.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;
   }
}

Create the root directory to place the PHP files.

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

For testing the PHP-FPM support, place a .php file onto the document root of the created virtual host.

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

Change the ownership of the root directory.

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

Restart the Nginx service.

sudo systemctl restart nginx

On your server or desktop machine, create a host entry for your domain (site.itzgeek.local) in the /etc/hosts file in case your environment doesn’t have a DNS server for name resolution.

sudo nano /etc/hosts

Add a host entry look like below.

192.168.0.10        site.itzgeek.local site

Open up your web browser and type your domain name in the web address.

http://site.itzgeek.local

The page will look like below:

PHP Information
PHP Information

From the above screenshot, PHP is working via FPM/FastCGI, as shown in the Server API line.

If you scroll the page further down, you can view all the supported PHP extensions details.

MySQL Extension
MySQL Extension

Conclusion

That’s All. I hope you have learned how to install LEMP stack on Ubuntu 20.04. Please share your feedback in the comments section.

Prev Post
Next Post
comments powered by Disqus