How to Install LEMP Stack on Ubuntu 16.04/18.04/20.04 - TechBeginner

Welcome to, Tech Beginner provides technical blog for Online Linux Tutorials, Ubuntu Tutorials, Redhat Tutorials and CentOS 7, Configuration of server like Web Server, Application server, Database server, Window server, networking, html, css, java script, php, wordpress, opencart, magento, joomla and etc.

How to Install LEMP Stack on Ubuntu 16.04/18.04/20.04

Share This

Nginx is a free, open-source, tip top HTTP laborer. Nginx is known for its fast, security, rich rundown of abilities, clear arrangement, and low resource use. This instructional exercise shows how you can present Nginx on a Ubuntu 20.04 LTS laborer with PHP 7.4 assistance (through PHP-FPM) and MySQL support (LEMP = Linux + Nginx (explained "engine x") + MySQL + PHP).

In this post, we will show how to introduce a LEMP stack on a Ubuntu 16.04/18.04/20.04 worker. The Ubuntu working framework deals with the main necessity. We will portray how to get the remainder of the segments going.

Step 1: Update and Upgrade the System

In our first step, We need to update the ubuntu repository and packages by using the following command.

sudo apt-get update 

sudo apt-get upgrade -y


Step 2: Installing Nginx

Now its time to install nginx web server on ubuntu machine by using the given command.

sudo apt-get install nginx -y

sudo systemctl status nginx


You should get output like this.


 After installation of nginx web server, We need to open the port 80 and 443 by using the command.

sudo ufw status
sudo ufw allow 80
sudo ufw allow 443

Step 3: Testing Nginx Test Page


Now its time to test the nginx web server by using the given URLs.

http://localhost or http://ip_address



Step 4: Installing MQL Server Database

You need to install now mql-server on ubuntu machine by following the command.

sudo apt-get install mysql-server
sudo systemctl status mysql

After that you need to take root access to setup the mysql password and other required setting.

sudo -i


Now you will be asking for this.

After this process, You need to select the strong password option.

Its time update your password and after that you will be asked for confirm password.

After this you need to remove the anonymous user from the mysql server, Type Y to confirm that.

In the next step, You will be asked for the disallow remote login, Choose that option carefully, in my i am going to disallow the remote user, Its will deny the workbench like app to connect with mysql server.

Now you will be asked for the remove test database from the mysql server, Due to security purpose, We must have remove test database by pressing the Y button.

In the last step, You need to reload the privileges tables by pressing Y button.

Here our mysql server installation and setup is completed, We can go ahead on next required installation.

Step 4: Installing PHP

We presently have Nginx introduced to serve our pages and MySQL introduced to store and deal with our information. Notwithstanding, we actually don't have whatever can produce dynamic substance. We can utilize PHP for this.


Here are step are the same, Executive the given command on by one.

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

After installation of the PHP packages, We need to do one config change with php-fpm param on line number 773, Use the given command.

sudo vim /etc/php/7.2/fpm/php.ini

We will change both of these conditions by uncommenting the line and setting it to "0" like this:




Save and exit from the Vim text editor and restart the php-ftm service.

sudo systemctl restart php7.2-fpm
sudo systemctl status php7.2-fpm

Step 5: PHP Processor with Nginx

Now you need to update the by default virtual-host configuration with PHP Processor as well as you can configure with your own domain virtual-host configuration.

sudo vim /etc/nginx/sites-available/default


You should get by default virtual host like this.


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

        # SSL configuration
        # listen 443 ssl default_server;
        # listen [::]:443 ssl default_server;
        # Note: You should disable gzip for SSL traffic.
        # See:
        # Read up on ssl_ciphers to ensure a secure configuration.
        # See:
        # Self signed certs generated by the ssl-cert package
        # Don't use them in a production server!
        # include snippets/snakeoil.conf;

        root /var/www/html;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;

        server_name _;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;

        # pass PHP scripts to FastCGI server
        #location ~ \.php$ {
        #       include snippets/fastcgi-php.conf;
        #       # With php-fpm (or other unix sockets):
        #       fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        #       # With php-cgi (or other tcp sockets):
        #       fastcgi_pass;

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #location ~ /\.ht {
        #       deny all;

Now you need to replace your default virtualhost code with this one carefully.


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

    root /var/www/html;
    index index.php index.html index.htm index.nginx-debian.html;

    server_name localhost;

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

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.2-fpm.sock;

    location ~ /\.ht {
        deny all;

Save and exit from the text editor and verify the nginx configuration test with given command.

sudo nginx -t

After this to get new changes in nginx web server, You need to reload to nginx server and get nginx service status by using the following command.

sudo systemctl reload nginx


Step 6: Testing PHP with Nginx

To test PHP is running or not with nginx web server, We need to create a php file called info.php So after that we can execute the info.php in the browser, Use the following step for the same.

Executive command from the root account.

echo "<?php phpinfo();" > /var/www/html/info.php

After that, You need to open your browser with http://localhost/info.php or http://IP_Address/info.php


Once the testing has been completed So then you need to remove this info.php file by using the command.

rm /var/www/html/info.php



You should now have a LEMP stack designed on your Ubuntu 16.04/18.04/20.04 machine. This gives you a truly adaptable establishment for serving web substance to your website visitors.

No comments:

Post a Comment