How to install LEMP stack on Ubuntu 18.04

How to install LEMP stack on Ubuntu 18.04

Install LEMP stack on Ubuntu

LEMP stack stands for Linux, Nginx, MariaDB, and PHP. Here, in the LAMP stack representing Linux, Apache, MariaDB, and PHP, not all components are tightly coupled. Therefore, we are installing the LEMP stack by replacing Apache with Nginx. This tutorial outlines how to install the LEMP stack on Ubuntu 18.04.


Before installing LEMP on Ubuntu 18.04. The server requires a non-root user account with sudo privileges.

1. Install NGINX

To install Nginx, first update the local package index and enter to access the latest package list

sudo apt update

Enter to install Nginx

sudo apt install nginx

You can check the status of Nginx by typing:

systemctl status nginx

The output looks like this:

    ● nginx.service - A high performance web server and a reverse proxy server
    Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
    Active: active (running) since Fri 2018-07-01 16:08:19 UTC; 1 days ago
    Docs: man:nginx(8)
    Main PID: 2369 (nginx)
    Tasks: 2 (limit: 1153)
    CGroup: /system.slice/nginx.service
        ├─2369 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
        └─2380 nginx: worker process

2. Install MariaDB

To install MariaDB and the related MySQL php extension, enter the following command:

sudo apt install mariadb-server php-mysql

To set up a secure installation, enter the following command: You will be prompted for a password that was not previously set, but only press the Enter key.

sudo mysql_secure_installation

3. Install PHP

By default, Nginx does not support native PHP processing. Therefore, to install PHP, you need to install the php-fpm (“fastCGI process manager”) package. You can now install php-fpm by typing the following command:

sudo apt install php-fpm

You can check if it is installed correctly with the following command

systemctl status php7.2-fpm

4. Set up the Nginx configuration file

Create directory inside var/www/html The name (you can use a domain name).

sudo mkdir -p /var/www/html/

Now you need to delete the provided default configuration file. To delete the default Nginx configuration file, enter:

sudo rm -f /etc/nginx/sites-enabled/default

Website configuration files are stored internally /etc/nginx/sites-available You need to create a configuration file named in this directory (you can use a domain name). Next, replace with your domain name and enter the following code in that file:


server { 
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html/; 
    index index.php; 

    location / { 
        try_files $uri $uri/ =404; 

    location ~* .php$ { 
        fastcgi_pass unix:/run/php/php7.2-fpm.sock; 
        include fastcgi_params; 
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
        fastcgi_param SCRIPT_NAME $fastcgi_script_name; 

Create a link for the above configuration file in the / etc / nginx / sites-enabled / directory by typing:

sudo ln -s /etc/nginx/sites-available/ /etc/nginx/sites-enabled/

5. Testing the LEMP stack

Configure a DNS record for the domain so that it reaches the server.

Now that you have made changes to the Nginx configuration directory, you need to restart PHP and reload the Nginx configuration file. Restart PHP and reload Nginx by entering the following command:

sudo systemctl restart php7.2-fpm
sudo nginx -s reload

You can check the status of Nginx by typing:

sudo nginx -t

Create an index.php file inside /var/www/html/ Go to the directory and enter the following code in the file:


Index Page


'; // Define PHP variables for the MySQL connection. $servername = "localhost"; $username = "test_user"; $password = "password"; // Creating a MySQL connection. $conn = mysqli_connect($servername, $username, $password); // Show if the connection fails or is successful. if (!$conn) { exit('

Your connection has failed.

' . mysqli_connect_error()); } else { echo '

You have connected successfully.

'; } ?>


In this tutorial, you learned how to install the LEMP stack on Ubuntu 18.04. If you have any questions about this, don’t forget to comment below.

Detailed tutorial

  • How to install Nginx on Ubuntu 18.04.
  • Install PHP 7.2 on Ubuntu 7.2.
  • How to install MariaDB on Ubuntu 18.04.