Good content takes time and effort to come up with.

Please consider supporting us by just disabling your AD BLOCKER and reloading this page again.

How To Install LINUX, NGINX, MYSQL, PHP LEMP Stack In Ubuntu | StackCoder

Install Linux, NGINX, MYSQL, PHP (LEMP Stack) on Ubuntu

Share On     Share On WhatsApp     Share On LinkedIn

Hello friends in this post I am gonna show you how to setup LEMP in Ubuntu server.

LEMP stands for L - LINUX, E - NGINX, M - MYSQL, P - PHP (Basically PHP-FPM for NGINX server)


Before moving ahead with this article I request you guys to setup up Ubuntu server in Digital Ocean, Linode, or any other cloud platform. If you don't have an account then click on the link and get the free credits to play around :)

If you're still using password logins or root SSH logins. Then I highly recommend you to check on How To Do Basic Server Security Setup For Ubuntu / Linux

Login to your server and follow along with the following steps.

Step 1 - Install NGINX webserver

NGINX is similar to you that of Apache webserver which handles all your server incoming requests on port 80

sudo apt-get update
sudo apt-get install nginx

To cross verify whether the installation is successful or not you can simply open your server ip_address in your browser as follows


You will see a NGINX successfully installed page. Which loads from /var/www/html/ folder

Step 2 - Install the MYSQL database & secure it with a password

MYSQL is a relational database that is used to store the dynamic data and fetch the data which will come from login, register, and various other types of forms

sudo apt-get install mysql-server

Now the MYSQL database is installed. As earlier now MYSQL won't prompt for a password. So to securely install the MYSQL server use the following commands

sudo mysql_secure_installation

You might be prompted to VALIDATE PASSWORD PLUGIN please select NO. Else, it will create a lot of fuss for the passwords at a later point.

By chance, if you select YES the option then you will get the following prompt

There are three levels of a password validation policy. Select difficulty based on your requirement.

LOW  Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 

Next, add the root user password and confirm the password.

New password:

Re-enter new password:

For best security practices it's a good idea to press YES for the subsequent prompts. These prompts include remove anonymous users, remove the test databases, disable remote root logins and flush privileges to save changes into effect.

By default, you will be root a user with auth_socket enabled connection. This means while connecting to the database no need for any passwords. At some point later this might cause security issues. So it's better to add the password authentication for all requesting connections.

Let me show you the same. First, let's login to MYSQL

sudo mysql

Next, run the following to see the details of the root user

SELECT user,authentication_string,plugin,host FROM mysql.user;

Once you run the above command you can see that root user doesn't have the authentication_string ie password.

| user       | authentication_string           | plugin        | host   |
| root       |                      | auth_socket      | localhost |

To set the password run the following command, make sure to change the your_password with the valid required password

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_password';

Run the following command so that the changes make will take an effect.


Now again if you re-run the above command to check if the password is set or not then it will set with mysql_native_password. No worries if your not able to see them authentication_string as it will be encrypted by MYSQL 

| user       | authentication_string           | plugin        | host   |
| root       | *549966GJ45794839F92C1571D6D78F | mysql_native_password | localhost |

Now you can safely exit from MYSQL prompt. If you try to log in as sudo mysql then it won't allow. You need to add your_password to do so.

Step 3 – Install PHP and its packages

You are doing a great job. Now you have installed NGINX webserver and MYSQL its time to set up your PHP so that your PHP scripts run.

NGINX unlike APACHE doesn't have any native code to instruct that PHP scripts need to be processed. Now, NGINX has to pass the PHP processing requests to PHP-FPM (FAST CGI PROCESS MANAGER)

Now install all the PHP packages in the Ubuntu server with the following command. No need to add a specific version like the way I will do in the following command. But make sure that your local development and now installing PHP version matches else if you use composer in the future you will have to face some problems.

sudo apt install php7.4-fpm php7.4-mysql php7.4-curl php7.4-zip php7.4-json

If your installing LARAVEL then better to run the following command to install all packages at once

sudo apt-get install php7.4-fpm php7.4-cli php7.4-common php7.4-curl php7.4-gd php7.4-gmp php7.4-json php7.4-mbstring php7.4-mysql php7.4-xml php7.4-zip composer git zip unzip

You can add composer, zip, unzip, git in the same command instead of adding later :)

Step 4 - Configure PHP with NGINX webserver

Once the PHP , MYSQL & NGINX installation is done then your left with configuring NGINX with PHP-FPM. As we had discussed earlier that NGINX by itself cannot process the PHP scripts you need to do this process.

First, you need to set up NGINX for your site. Basically, you will find all NGINX available configurations in the following path


To check if any of these configurations are enabled or not then you need to check in the following path.

NOTE: These are only symbolic links which will be pointing to your /etc/nginx/sites-available directory


Make sure to replace with your domain name in the following configuration.

sudo nano /etc/nginx/sites-available/

Now add the following configuration to the file. In the following, if you don't have a domain name still then just add the ip_address of your domain name.

server {
        listen 80;
        root /var/www/html;
        index index.php index.html;
        #Make sure to change your domain name in the following line too.

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

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

        location ~ /\.ht {
                deny all;

Now you have added NGINX configuration lets activate it by the following command

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

Activating your domain name is nothing but adding the symbolic link in your /etc/nginx/sites-enabled/ folder. Which will be read by the NGINX main configuration file nginx.conf which resides in /etc/nginx/nginx.conf

As a default, NGINX will have default configuration which will be activated we have to unlink from /etc/nginx/sites-enabled/default with the following command

sudo unlink /etc/nginx/sites-enabled/default

NOTE: Remember here we just unlinking the file. Still, your main file exists in /etc/nginx/sites-available the folder which you can activate at any given point of time with the following command

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

Step 5- Test & restart your NGINX configuration

This is a very important step. Don't forget to restart you NGINX server. Else the configuration that you have added won't work.

First, check if the NGINX added code to the configuration file is proper or not by the following command

sudo nginx -t

You must be able to see similar output

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Now reload the NGINX server configuration file with the following

sudo service nginx reload

Step 6 - Finally check if the configuration is working

Now you have install LEMP and it's time to check if it paid your efforts. First, create testing.php file inside /var/www/html/ like /var/www/html/testing.php

sudo nano /var/www/hml/testing.php

And add the following code

echo 'LEMP installtion completed.';

Now browse URL or http://ip_address/testing.php from browser and you must be able to see the following output

LEMP installation completed.

Congrats on setting up your LEMP stack.


Now you know how to install the LEMP stack. Hope this article helped you. I have written other articles on NGINX kindly check them too

Author Image

Channaveer Hakari

I am a full-stack developer working at WifiDabba India Pvt Ltd. I started this blog so that I can share my knowledge and enhance my skills with constant learning.

Never stop learning. If you stop learning, you stop growing