How To Install LEMP stack (Linux, Nginx, MySQL, PHP) on Ubuntu System

Harish Kumar · · 2652 Views
How To Install LEMP stack (Linux, Nginx, MySQL, PHP) on Ubuntu System

The term LEMP is an acronym of the four open-source projects:

  1. L - Linux operating system

  2. E - Nginx [engine x]

  3. M - MySQL or MariaDB 

  4. P - PHP programming language

In this post, you will learn how to install Nginx, create Nginx server blocks (virtual host), install and secure MySQL, and install PHP.


The first thing you should make sure you have a regular, non-root user account on your server with sudo privileges

Step 1. Installing Nginx

Nginx is available in Ubuntu repositories. So, update the packages index and install Nginx by using the following commands on the terminal:

sudo apt update
sudo apt install nginx

Step 2. Installing MySQL

Now the web server is ready. The next step is to install MySQL (a database management system). Install MySQL by using following command:

sudo apt install mysql-server

When the installation is finished, run the mysql_secure_installation command to improve the security of the MySQL installation:

sudo mysql_secure_installation

It will ask to set the root secret password, delete the anonymous user, and remove the test database. You should answer "Y" (yes) to all questions.

Step 3. Installing PHP

Since Nginx doesn't contain native PHP handling like some other web servers, you will need to install php-fpm, which stands "fastCGI process manager”. We will tell Nginx to pass PHP requests to this software for processing.

Install the php-fpm module along with an additional helper package using following commands:

sudo apt install php-fpm php-opcache php-cli php-gd php-curl php-mysql

Step 4: Configuring Nginx to Use the PHP Processor (create virtual hosts)

Now all of the necessary LEMP stack parts installed. Yet still, need to make a couple of configuration changes so as to tell Nginx to utilize the PHP processor for dynamic content.

This is done on the server block level (server blocks are like Apache's virtual hosts). To do this, navigate to /etc/nginx/sites-available/ directory.

Let's say our domain name is So, here I will create the new server block configuration file is named, although you can name it whatever you’d like:

sudo vi /etc/nginx/sites-available/

Add the following snippet, which was taken and somewhat modified from the default server block configuration file to the new server block configuration file:

In the /etc/nginx/sites-available/  :

server {
        listen 80;
        root /var/www/html;
        index index.php index.html 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;

        location ~ /\.ht {
                deny all;

This is what every one of these directives and location blocks does:

  1. listen - Defines what port Nginx should listen on. In this case, that is port 80, which is the default port for HTTP.

  2. root -  Defines the website root path.

  3. index -  prioritize serving files named index.php

  4. server_name - server’s domain name or public IP address.

  5. location / - It has a try_files directive, which checks for the existence of files matching a URI request. If Nginx cannot find it, then it will return a 404 error.

  6. location ~ \.php$ - In this location block, it is pointing Nginx to the fastcgi-php.conf configuration file and the php7.4-fpm.sock file to handle PHP processing.

  7. location ~ /\.ht - This location block tells the Nginx to do not server .htaccess files by using the deny all directive.

After adding this snippet, save and close the file.

The next step is to create a symbolic link from /etc/nginx/sites-available/ to the /etc/nginx/sites-enabled/ to enable this server block.

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

Test your new configuration file for syntax errors by following command:

sudo nginx -t

If any errors are reported, go back and recheck your configuration file before continuing.

When you are ready, reload Nginx for the changes to take effect:

sudo systemctl reload nginx

This concludes the installation and configuration of your LEMP stack.


Please login or create new account to add your comment.

1 comment
gurpreet ·

Good explaination

You may also like:

What is the difference between classes vs enums in PHP 8.1?

One of the common questions that I have been asked is what is the difference between classes and enums. Class(es) and enum(s) share many similarities but differ in some aspects. (...)
Harish Kumar

How to use the enumerations(Enums) of PHP 8.1 in Laravel?

The release of PHP 8.1 brings native enumerations to PHP. There is no more requirement for custom solutions in your Laravel projects since the Laravel v8.69 release has you back. (...)
Harish Kumar

What is Enumerations(Enums) in PHP 8.1? Enums in PHP v8.1 Explained in detail with examples.

PHP 8.1 added many new features, and Enumerations (Enum) is our favourite new feature. Enumerations (or enums for short) allow us to define a new structure similar to a class however (...)
Harish Kumar

Web App Development Technologies

If you have been planning to create robust websites or web apps, you should be aware of the various technologies required to make it happen. Over time, these technologies have (...)
Narola Infotech

How To Install NVM (Node Version Manager) on Ubuntu System?

This tutorial will assist you with installing NVM on the Ubuntu machine. Additionally, allow you to install different node versions and other useful examples.
Harish Kumar

Install phpMyAdmin Manually with Nginx server on Ubuntu

In this guide, I will show you how to install and configure phpMyAdmin with Nginx, MySQL, and PHP8.0 (LEMP) on an Ubuntu system. phpMyAdmin is a free and open-source database (...)
Harish Kumar