Install LEMP Stack on Ubuntu 22.04

Last Updated:

Welcome to our tutorial on how to install LEMP stack on Ubuntu 22.04.  LEMP Stack is a group of open source tools commonly used for developing and deploying web applications. It consists of the Linux Operating System (Ubuntu 22.04 in this case), lightweight and powerful Nginx (Engine-X) web server, MySQL/MariaDB RDBMS and the server-side scripting language, PHP.

Install and Setup LEMP Stack on Ubuntu 22.04

Run system update

apt update
apt upgrade

Install Nginx Web Server on Ubuntu 22.04

Nginx is available on the default Ubuntu 22.04 repos. It can simply be installed by executing the command below;

apt install nginx

Running Apache

Once the installation is done, Nginx is started and enabled to run on system boot.

To check the status;

systemctl status nginx

● nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2022-05-02 10:33:14 UTC; 5min ago
       Docs: man:nginx(8)
    Process: 4544 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
    Process: 4545 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
   Main PID: 4649 (nginx)
      Tasks: 3 (limit: 2241)
     Memory: 6.0M
        CPU: 78ms
     CGroup: /system.slice/nginx.service
             ├─4649 "nginx: master process /usr/sbin/nginx -g daemon on; master_process on;"
             ├─4652 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""
             └─4653 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""

May 02 10:33:14 jellyfish systemd[1]: Starting A high performance web server and a reverse proxy server...
May 02 10:33:14 jellyfish systemd[1]: Started A high performance web server and a reverse proxy server.

Check if it is enabled to run on system boot.

systemctl is-enabled nginx

Allow Web Server External Access on Firewall

You can allow external access to Nginx using the ports or the Nginx application name. For example, to allow access to both Nginx HTTP and HTTPs traffic;

ufw allow 80,443/tcp

Similarly, you can use Nginx app, Nginx Full, to allow both HTTP and HTTPs;

ufw allow "Nginx Full"

For HTTP or HTPPS, simple use Nginx HTTP or Nginx HTTPS respectively as the app names.

Verify access to Nginx from browser by using the address, http://Server.IP_or_hostname. You should land on Nginx HTTP server test page which basically shows that nginx web server is successfully installed and working.

Install and Setup LEMP Stack on Ubuntu 22.04

Install MySQL/MariaDB Database Server on Ubuntu 22.04

You can choose to use MySQL or MariaDB for your RDMS. In this tutorial, we are using MySQL 8 as our database system.

apt install mysql-server mysql-client

When installed, MariaDB is started and enabled to run on system boot.

systemctl status mysql

● mysql.service - MySQL Community Server
     Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2022-05-02 10:44:28 UTC; 1min 23s ago
    Process: 5498 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exited, status=0/SUCCESS)
   Main PID: 5506 (mysqld)
     Status: "Server is operational"
      Tasks: 37 (limit: 2241)
     Memory: 358.2M
        CPU: 1.208s
     CGroup: /system.slice/mysql.service
             └─5506 /usr/sbin/mysqld

May 02 10:44:26 jellyfish systemd[1]: Starting MySQL Community Server...
May 02 10:44:28 jellyfish systemd[1]: Started MySQL Community Server.

Securing MariaDB

MariaDB ships with some initial security script called mysql_secure_installation. When run, this script removes all test databases, disables remote root account login and remove test user accounts as part of the initial security checks.


You can now login and create a database and database user as required by whatever application you want to run.

Install PHP and Other Required PHP Modules on Ubuntu 22.04

Next, install PHP on Ubuntu 22.04.

By default, Ubuntu 22.04 ships with PHP 8.x in its default repos. Hence, installation of PHP 8.x is as simple as running the command below;

apt install php php-fpm php-mysql

This will install alongside itself other default PHP modules.

To install Other versions of PHP, check the link below;

Install PHP 7.1/7.2/7.3/7.4 on Ubuntu 22.04

Install Other Required PHP Modules

If any other required modules needs to be installed, you can simply install it by running the command, apt install php-extension, where extension is the name of the PHP module. For example;

apt install php-{bcmath,bz2,imap,intl,mbstring,soap,sybase,xml,zip}

Configure Nginx for PHP Processing

For Nginx to process PHP pages, you need to include index.php as a value for the index parameter. Since we are using the default configuration for testing, edit the it as follows;

vim /etc/nginx/sites-enabled/default
        root /var/www/html;

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

Also, you need to configure Nginx to pass PHP scripts to FastCGI server. Uncomment the line location ~ \.php$ and configure it as shown below

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

Save and exit the file,

Test Nginx configuration syntax.

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

Restart Nginx and FastCGI process manager, if there is no configuration syntax.

systemctl restart nginx php8.1-fpm

Testing Nginx PHP Processing on Ubuntu 22.04

You can test Nginx PHP processing to confirm that is working as required as well check the version and installed modules using the simple PHP info script.

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

Next, navigate to the browser and enter the address, http://<server-IP-or-hostname>/index.php

nginx php ubuntu 22.04

And there you go. Your LEMP stack is installed on Ubuntu 22.04. Ensure that all the required php modules for the specific application you want to deploy are met.

Once you have verified Nginx PHP processing, remove the PHP info file;

rm -rf /var/www/html/index.php

That marks the end of our guide on how to install and setup LEMP Stack on Ubuntu 22.04.

Other Related Tutorials

Install MySQL 8 on Rocky Linux 8

Configure Guacamole MySQL Database Authentication


We're passionate about sharing our knowledge and experiences with you through our blog. If you appreciate our efforts, consider buying us a virtual coffee. Your support keeps us motivated and enables us to continually improve, ensuring that we can provide you with the best content possible. Thank you for being a coffee-fueled champion of our work!

Photo of author
I am the Co-founder of, Linux and the whole FOSS enthusiast, Linux System Admin and a Blue Teamer who loves to share technological tips and hacks with others as a way of sharing knowledge as: "In vain have you acquired knowledge if you have not imparted it to others".

Leave a Comment