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

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

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

The LEMP software stack is a collection of software tools used for serving dynamic web content and web applications written in PHP. It consists of a Linux-based operating system, an Nginx web server (pronounced "Engine-X"), MySQL for database management, and PHP for dynamic content processing.

This guide presents the process of LEMP stack installation on an Ubuntu 22.04 server. Ubuntu serves as the foundation for the Linux segment of the stack, and we will provide detailed instructions for configuring the remaining components.

Installing Nginx on Ubuntu 22.04

The first step is to log in to your ubuntu server with ssh. Then installing nginx is very straightforward. Install nginx and allow firewall as shown

sudo apt update
sudo apt install nginx
sudo ufw allow 'Nginx HTTP'

Navigate to the nginx and modify as shown bellow by appending sites-enabled.

user www-data;
worker_processes auto;
pid /run/;
include /etc/nginx/modules-enabled/*.conf;
include /usr/local/etc/nginx/sites-enabled/*;

The next step is to create the folder where we are going to put or code using the following commands

sudo mkdir -p /var/www/
sudo chown -R www-data:www-data /var/www/
sudo chown -R $user1:$user1 /var/www/
sudo chmod -R 755 /var/www/

create a new file on sites-available where you are going to set up the server name and configguration path to the code that is linked to the server name and add the following

server {

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


        location / {
               # try_files $uri $uri/ =404;
                try_files $uri $uri/ /index.php;

        location ~ \.php$ {
                fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
                include snippets/fastcgi-php.conf;
                fastcgi_param REQUEST_URI $request_uri;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

olocation ~ /\.ht {
                deny all;

Here’s what each of these directives and location blocks do:

  • listen — Defines what port Nginx will listen on. In this case, it will listen on port 80, the default port for HTTP.
  • root — Defines the document root where the files served by this website are stored.
  • index — Defines in which order Nginx will prioritize index files for this website. It is a common practice to list index.html files with higher precedence than index.php files to allow for quickly setting up a maintenance landing page in PHP applications. You can adjust these settings to better suit your application needs.
  • server_name — Defines which domain names and/or IP addresses this server block should respond for. Point this directive to your server’s domain name or public IP address.
  • location / — The first location block includes a try_files directive, which checks for the existence of files or directories matching a URL request. If Nginx cannot find the appropriate resource, it will return a 404 error.
  • location ~ \.php$ — This location block handles the actual PHP processing by pointing Nginx to the fastcgi-php.conf configuration file and the php8.1-fpm.sock file, which declares what socket is associated with php8.1-fpm.
  • location ~ /\.ht — The last location block deals with .htaccess files, which Nginx does not process. By adding the deny all directive, if any .htaccess files happen to find their way into the document root, they will not be served to visitors.

Activate your configuration by linking to the configuration file from Nginx’s sites-enabled directory:

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

You can go ahead and restart nginx. 

sudo systemctl restart nginx

For now if you access the web page with the domain name we added, you'll find that it is using http instead of https. You can install a certificate. Fo now we'll use the free certificate provider called certbot.

Install certbot:

sudo apt install certbot python3-certbot-nginx

To enable certbot on the domain, use the following command

sudo certbot --nginx -d

Installing MSQL on Ubuntu 22.04

By default, MySQL server is included in the Ubuntu default repository so you just need to install it using the following command.

sudo apt install mysql-server -y

By default, the MySQL installation is not secured. For security purposes, it is recommended to set a MySQL root password and secure the MySQL installation.

First, log in to the MySQL console with the following command.


Once you are logged in, set a MySQL root password using the following command

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

Now exit from MySQL shell and rin the mysql_secure_installation 



This is to ewmove anonymouse users, disallow root login remotely and remove the test database as shown.

Enter password for user root:
Press y|Y for Yes, any other key for No: n
Change the password for root ? ((Press y|Y for Yes, any other key for No) : n
Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : Y
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y

You can proceed to create a User in MySQL

mysql -u root -p

After successful login, create a user and set the passowrd

CREATE USER 'user1'@'localhost' IDENTIFIED BY 'user1password';

Next you need to grant the user all priviledges to the databases. You can also specify which set of permissions you want to give the user


Next flush the privileges to save and exit


If you want to import a database to mysql, you can use the following command

mysqlimport -u book_admin -p books_production ~/backup/database/books.sql

You can also export from MySQL with:

mysqldump -u my_username -p database_name > output_file_path

Installing PHP on Ubuntu 22.04

To install PHP on nginx, you'll need to use the PHP-FPM library. You can install with the following command

sudo apt-get install php8.1-fpm -y

In this instance, we have installed version 8.1 of the PHP and PHP-FPM packages.

A common mistake is to install the PHP, not PHP-FPM package. The problem with this approach is that unlike PHP-FPM, the PHP package installs the Apache HTTP server and its httpd process, which conflicts with Nginx.

Why does a basic PHP install require Apache?
PHP requires one of three dependencies to exist on a machine:


A simple PHP install uses the libapache2-mod-php module by default, which requires installation of the full Apache HTTP server software suite. To avoid this, install either the php-cgi or the php-fpm module for Nginx.

By default, your page is being renderred by the default nginx file 


You can disable this when you want to use a custom domain by deleating it together with the symlink

sudo cd /etc/nginx/sites-enabled
sudo rm -f default
sudo cd /etc/nginx/sites-available
sudo rm -f default
sudo nginx -t && nginx -s reload

Your site is officially deploy 

What's Your Reaction?