Skip to content
Related Articles

Related Articles

How to Install and Deploy LEMP in Linux?

View Discussion
Improve Article
Save Article
  • Last Updated : 27 Jun, 2022
View Discussion
Improve Article
Save Article

LEMP is a bundle or stack of software that, is used to run dynamic websites based on PHP. The LEMP stack includes a Linux Operating system, Nginx (Pronounced as Engine X) web server, MySQL database and PHP. Nginx is a web server that handles web requests and serves web content. MySQL database is used to store data and PHP is used to process dynamic content on the server itself before delivering the web content to the user. It is a server-side language. 

In this article, we will see how the LEMP stack can be set up on a Ubuntu/Debian-based system. You will need a user which has sudo permissions to follow along with this article.

Installation of LEMP in Linux

First, we will update and upgrade our packages. Run the following command

sudo apt update
sudo apt upgrade -y

Now, let’s begin by installing our web server.

Step 1: Install Nginx

To install Nginx, run the following command

sudo apt install nginx -y

This will install Nginx. To check if it is running, run the following command

sudo systemctl status nginx


Nginx uses port 80 to listen for incoming web requests, so if you have a firewall, you need to open port 80. Some cloud service providers have an external firewall, like Microsoft Azure, so in that case, You have to manage the firewall settings from there. We have used ufw to manage the firewall, so let’s configure the firewall rule using that. Run the following command

sudo ufw allow 80

If you wish to encrypt your traffic with an SSL certificate, you will need to open port 443 as well, which you can do similarly with the following command

sudo ufw allow 443

Now, to check if Nginx is handling web requests, open any web browser, and type in the IP address of your server. If you are setting up a LEMP stack on a local machine, type localhost instead. You should see the following default home page of Nginx.


Now, we are sure that our Nginx web server is working properly. This default web page is stored in the location /var/www/html and it is this directory that Nginx uses by default to serve websites. You can see the contents of this directory

thatNginx default directory

This index.nginx-debian.html file is the default file that gets served whenever we make a web request to our Nginx web server. You can change this later, or even change the default directory, which we will see how to do later in this article.

Step 2: Installing MySQL Database

MySQL is an open-source relational database management system, and it is used to manage databases for different web applications in the LEMP stack. To install it, run the following command

sudo apt install mysql-server -y

Now that the MySQL server is installed, by default, it is not very secure. Luckily there is a script that makes securing our MySQL server easy. Execute that script by typing

sudo mysql_secure_installation


At first, it will ask you if you want to install a VALIDATE PASSWORD COMPONENT. This plugin validates the passwords for users against a level of strength that you choose. Failing to meet that password strength level will cause rejection of the password. It is safe to not install this validation plugin, just use a strong password on your own. 

After this step, you will be prompted to enter and confirm a password for the root user of MySQL. This is the database root user and not the system’s root user. 

Next, you will be asked for several things, which enhance the security of our MySQL server, just type y and hit enter at every prompt and you will be done with MySQL installation.


Now, to log in to your MySQL server, type in

sudo mysql

This will log you in as the root user. You will notice that you were not asked for any password, but didn’t we just set up a password for the root users in the previous step? The thing is, there are several authentication methods in MySQL to authenticate a user against the server. Our root user uses the auth_socket plugin for authentication instead of a password. This allows users with sudo privileges to log into the MySQL server using their system password. This ensures that system users who have sudo rights are the only ones who are capable of accessing the MySQL server. 


You can see in the screenshot above that the root user uses the auth_socket plugin for authentication. You can change it to other plugins if you like to do so. 

Step 3: Installing PHP

Nginx delivers the web content to the client, and MySQL stores their data. Now we need PHP to generate dynamic web pages. It is used to process content on the server before it gets delivered. To install it, run the following command

sudo apt install php-fpm php-mysql

PHP-fpm, which nginx uses to process PHP files, and the PHP-MySQL module lets PHP connect with the MySQL database. All the other PHP packages that are required for processing PHP files will be installed additionally along with these two modules as dependencies.


 Php is installed, now we need to configure our Nginx to work with PHP.

Step 4: Configuring Nginx

To configure Nginx, we need to edit its configuration file which is located at /etc/nginx/sites-available/ location. 


As you can see, there is a file named default, we need to edit this file. Now, Instead of un-commenting the required lines one by one and editing them, it would be easy if you just delete this default file, create a new one, and copy-paste the required configuration lines. To delete and create a new file, run the following commands

sudo rm /etc/nginx/sites-available/default
sudo touch /etc/nginx/sites-available/default

Open this newly created file with a nano text editor. Type in

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

Copy the following text content and paste it in the default configuration file by pressing Ctrl+Shift+V

server {
    listen 80;
    server_name your_domain www.your_domain;
    root /var/www/html;

    index index.html index.php index.htm index.nginx-debian.html;

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

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

Let’s understand this configuration code. 

  • listen 80 – This specifies the port which Nginx uses to listen for incoming web requests.
  • server_name – This defines the server block’s name. Right now there is one server block, but multiple server blocks can be created for hosting multiple web-apps. In this, we have specified the domain name of the website for which we wish to apply this configuration to. So, any web request made from this domain name will be served based on this server block configuration. If you do not use domain name, you can specify the IP address as well, or if you are configuring LEMP stack on your personal computer, just type an _ (underscore) and leave it at that.
  • root – this defines the root directory from where the Nginx will serve the web files. It is this directory where you have to store the source files of your web-application.
  • Next, there is an array of index words. This specifies the priority of filenames that Nginx will display by default. The leftmost word has more significance and will be prioritized over its right neighbor. So, if the root directory has an index.html file, it will be displayed by default, and if it also has a index.php file, then it will not be used. If there is a index.php file alone, then it will be displayed by default. By the word “default”, I mean the page which gets displayed whenever a user enters the domain name or IP address of the website in the web browser.
  • location / – This block of code checks for the URL and looks for the files which the user has requested. If a user requests for a file that Nginx cannot find, it will give a 404 error.
  • location ~ \.php – This block includes the fastcgi configuration file fastcgi-php.conf and specifies the php-fpm socket which is used to process PHP files.

Now Press Ctrl+X then Y and then Enter to save and exit. This will save the changes that you have made to the file. Now, we will check if the configuration file is correct syntax wise. To do that, run the following command

sudo nginx -t

You should see the following output.


Now, if you have made some mistake in the configuration file, and you need to recheck the file for those errors. After fixing the errors and rechecking the configuration file with the above command, we need to reload our Nginx webserver to apply these changes in the configuration file. Run the following command to do so.

sudo systemctl reload nginx

Now, with all done, let’s try to run a PHP file on our LEMP stack to see if it works.

Step 5: Testing a PHP file

Create a small PHP script in the root directory of Nginx and open it with nano text editor. Run the following commands to do so.

sudo touch /var/www/html/test.php
sudo nano /var/www/html/test.php

Add the following contents to this PHP file.


This PHP script has a function phpinfo() which displays information about the current installed PHP. Now open your web browser, and type in 


You should be greeted by something like 


This shows that our LEMP stack can process PHP files. Remove this test.php file, because it displays sensitive information about our PHP environment and we do not want anyone to know any of the listed information. Remove it by running the following command.

sudo rm /var/www/html/test.php

With this done, we have successfully set up the LEMP stack on our Linux machine.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!