How To Install Linux, Nginx, MySQL, PHP (LEMP Stack) in Ubuntu 18.04


LEMP stack stands for Linux, Nginx, MariaDB, and PHP stack and is widely used for hosting websites/blogs.

Here, we will see how to install LEMP Stack on Ubuntu 18.04 with PHP support (via PHP-FPM) and database (MariaDB) support.

Install LEMP Stack

Install Linux

Here is the tutorial on Step by Step installation of Ubuntu 18.04 and Upgrading Ubuntu 16.04 & Ubuntu 17.10 to Ubuntu 18.04.

Proceed to the installation of EMP (Nginx version 1.17.2, PHP version 7.2, MariaDB version 10.1.40) on Ubuntu 18.04.

Install Nginx

Nginx is a free, open-source, high-performance HTTP web server and is known for its stability, simple configuration, and low resource consumption.

Update repository index.

sudo apt update

Install below packages.

sudo apt install -y wget gnupg2 ca-certificates

Download the Nginx repository signing key from the official website.


Add the Nginx public key into the system.

sudo apt-key add nginx_signing.key

Add Nginx repository to your system.

echo "deb [arch=amd64] bionic 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 the below URL.


You should see the Nginx default page, and this confirms you that the Nginx is successfully installed on the server.

Nginx's Default Page
Nginx’s Default Page

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

Auto-start Nginx at system startup.

sudo systemctl enable nginx

Install MariaDB Server

Install the MariaDB server by issuing the following command.

sudo apt install -y mariadb-server mariadb-client

Next, make the MariaDB server secure by using the mysql_secure_installation command.

Install PHP-FPM

Install PHP through PHP-FPM (PHP-FastCGI Process Manager), an alternative PHP FastCGI implementation. It provides additional features useful for sites bigger in size.

By default, Ubuntu 18.04 ships PHP-FPM v7.2. You can also install PHP-FPM v7.3 by following the steps in the below URL.

READ: How To Install PHP 7.3 On Ubuntu 18.04

Install php-fpm by using the following command.

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

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

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

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

Then, change the listen parameter.


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


listen =

Restart the PHP-FPM process and enable it to start automatically on system boot.

sudo systemctl restart php7.2-fpm

sudo systemctl enable php7.2-fpm

Test LEMP Stack

Let’s create name-based virtual host on Nginx server for the following details.

Domain Name: web.itzgeek.local
Document Root: /usr/share/nginx/html/web.itzgeek.local

First, create a virtual host configuration file for our domain under /etc/nginx/conf.d/ directory.

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

Add the following content.

server {
   server_name web.itzgeek.local;
   root /usr/share/nginx/html/web.itzgeek.local;

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

   location ~ \.php$ {
      include        /etc/nginx/fastcgi_params;
      fastcgi_index  index.php;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

Create the root directory for our virtual host.

sudo mkdir -p /usr/share/nginx/html/web.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 /usr/share/nginx/html/web.itzgeek.local/index.php

Update the permission of the file.

sudo chown -R www-data:www-data /usr/share/nginx/html/web.itzgeek.local/

Restart the services.

sudo systemctl restart nginx

sudo systemctl restart php7.2-fpm

Create a host entry for your domain (web.itzgeek.local) in the /etc/hosts file in case your environment doesn’t have DNS server for name resolution.

nano /etc/hosts

Add a host entry something look like below.        web.itzgeek.local web

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


The page will look like below:

PHP Informations
PHP Informations

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

If you scroll the page further down, you would see the support of MariaDB.

MariaDB Extension Details
MariaDB Extension Details


That’s All. I hope you have learned how to install LEMP stack on Ubuntu 18.04. Consider installing Let’s Encrypt SSL certificate for you site to improve the security. Please share your feedback in the comments section.

You might also like