How to Install Nginx, MariaDB, and PHP (LEMP) on Ubuntu 22.04 LTS

This guide explains step-by-step instructions on installing the LEMP stack (Nginx, MariaDB, and PHP) in Ubuntu 22.04 LTS.

LEMP is an acronym for a group of free and open-source software often used to serve web applications. It represents the configuration of the Nginx Web Server, MySQL / MariaDB Database, and PHP Scripting Language on a Linux operating system.

If you plan to create PHP-based applications or use CMS systems such as WordPress or Joomla, you will likely use the LEMP stack.

This guide demonstrates installing a LEMP stack (Nginx, MariaDB, PHP) on an Ubuntu 22.04 LTS (Jammy Jellyfish).ย However, if you are looking for one forย Ubuntu 24.04 LTSย (Noble Numbat), you canย find it here.

Step 1: Update Software Repositories

First, we willย use the APT commandย to update our system’s list of available packages.ย Thisย ensures that only the most recent versions of the packages are used throughout the installation of the LEMP stack on our Ubuntu 22.04 LTS system and prevents difficulties.

sudo apt update
sudo apt upgradeCode language: Bash (bash)
Updating Ubuntu 22.04 Packages List

Make sure the system is fully updated. If packages need to be updated, we advise you to apply them before proceeding.

Step 2: Install Nginx Web Server on Ubuntu 22.04 LTS

Theย web serverย enables you to serve visitors’ content, such as web pages. This step takes care of the second requirement in the LEMP stack, the Nginx web server.ย 

Use the following commands to install the latest Nginx version on your Ubuntu 22.04 system. When prompted, enter Y to confirm that you want to install it.

sudo apt install nginxCode language: Bash (bash)
Installing Nginx Web Server

After complete installation, the Nginx web server will run on your Ubuntu 22.04 server. But first, let’s see if it works as expected.

Open a web browser on your system and type the serverโ€™s IP address in the address bar.

Nginx Default Webpage

The default Nginx page will welcome you.

Step 3: Install MariaDB Server on Ubuntu 22.04 LTS

Now that you have a working web server, you need to install the database system to store and manage data for your website. In this tutorial, we’ll install MariaDB instead of MySQL.

MariaDB is an open-source RDBMS (Relational Database Management System) that is backward compatible and a binary drop-inย replacement for MySQL. Compared to MySQL,ย MariaDB provides improved performance with faster replication speeds, tighter security measures, and additional storage engines.

To install the MariaDB database, run the commands below, and when prompted, confirm installation by typingย “Y” and hittingย “Enter.”

sudo apt install mariadb-serverCode language: Bash (bash)
Installing MariaDB Database

Step 3.1: Securing the MariaDB Server

Next, we’ll use a script (mysql_secure_installation) provided by theย “mariadb-server”ย package toย restrict access to the serverย and remove unused accounts because the default setup makes your MariaDB installation unsafe.

sudo mysql_secure_installationCode language: Bash (bash)

After running the above command, you willย be promptedย to enter the MariaDB root password. Just leave the root password empty and press theย Enterย key. For the rest, typeย “Y” and hitย “Enter.”

Securing the MariaDB Server

Great! You have secured the MariaDB server in the LEMP stack on Ubuntu 22.04 LTS.

We clarify that the password specified above for the MariaDB root accounts is only for remote users. To log in from the host we installed it on, you do not need to enter a password and will not be asked for one.

Step 3.2: Testing MariaDB Installation

Let’s log into it and run a simple query to check if the database server is functioning normally.

Typeย the command shown below.

sudo mysqlCode language: Bash (bash)

The server console should come up. Then, run a simple query:

select version();Code language: Bash (bash)

In response to your query, the MariaDB server should return its version. Finally, use the quit command to exit the MariaDB shell and return to the system terminal.

Testing MariaDB Installation

Step 4: Install PHP on Ubuntu 22.04 LTS

The last step to have a complete LEMP stack installed on our Ubuntu 22.04 LTS system is to install PHP. To add PHP support to Nginx, you mustย install and use PHP-FPMย to execute PHP files.

So, to install PHP-FPM and several of the most widely used PHP modules, type the command below, and when prompted, enterย Y to confirm that you want to install it.

sudo apt install php-fpm php-mysql php-gd php-cli php-curl php-mbstring php-zip php-opcacheCode language: Bash (bash)
Installing PHP 8 on Ubuntu 22.04 LTS

The command above will install the latest, up-to-date versions of PHP 8.1 on your Ubuntu 22.04 system.

Step 5: Configure Nginx to Execute PHP Files

Now that we’ve installed all of the LEMP components on our Ubuntu 22.04 system, we need to edit the default Nginx virtual host configuration file.

sudo vim /etc/nginx/sites-enabled/defaultCode language: Bash (bash)

Add the following lines to the default server block to allow Nginx to process PHP files:

location ~ \.php$ {
     include snippets/fastcgi-php.conf;
     fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
}Code language: Bash (bash)
Configuring Nginx to Execute PHP Files

Test the modified Nginx configuration file for syntax errors by entering the following command:

sudo nginx -tCode language: Bash (bash)
Testing Nginx's Configuration

If you get the above result, all is well, and we only have one final step left. However, if any errorsย are reported, recheck your file before continuing.

When you are ready, restart Nginx to make the changes take effect.

sudo systemctl restart nginxCode language: Bash (bash)

If you want to learn how to create and edit Nginx server blocks for your virtual hosts, we recommend our excellent guide on “How to Create Nginx Virtual Host (Server Block).”

Step 6: Test your Ubuntu 22.04 LEMP Installation

You have completed the installation of Nginx, MariaDB, and PHP on Ubuntu 22.04, so your LEMP stack should now be fully operational.

Finally, let’s create a test PHP file to verify that PHP-FPM works and is integrated with Nginx. In the default server block above, our site is being served from /var/www/html, so weโ€™ll create a test file there:

echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/test.phpCode language: Bash (bash)

Now, you can accessย “test.php”ย from aย web browser by using your site’s domain or server’s IP address followed byย “/test.php.” A page with complete information about your PHP installation will appear.

Nginx, MariaDB, and PHP (LEMP) Successfully Installed on Ubuntu 22.04 LTS

Congratulations! Your LEMP stack is successfully installed and fully functional.

Conclusion

This guide showed you how to install the LEMP stack (Nginx, MariaDB, and PHP) on Ubuntu 22.04 LTS. PHP-based web apps can now run on your server.

You could take several next steps from here. For example, we recommend reading our guide onย setting SSL certificates on the Nginx serverย to ensure your website provides content over a secure SSL (HTTPS) connection.

Thanks for your time! We hope you find this guide useful. Your feedback and comments are most welcome.

Bobby Borisov

Bobby Borisov

Bobby, an editor-in-chief at Linuxiac, is a Linux professional with over 20 years of experience. With a strong focus on Linux and open-source software, he has worked as a Senior Linux System Administrator, Software Developer, and DevOps Engineer for small and large multinational companies.

Think You're an Ubuntu Expert? Let's Find Out!

Put your knowledge to the test in our lightning-fast Ubuntu quiz!
Ten questions to challenge yourself to see if you're a Linux legend or just a penguin in the making.

1 / 10

Ubuntu is an ancient African word that means:

2 / 10

Who is the Ubuntu's founder?

3 / 10

What year was the first official Ubuntu release?

4 / 10

What does the Ubuntu logo symbolize?

5 / 10

What package format does Ubuntu use for installing software?

6 / 10

When are Ubuntu's LTS versions released?

7 / 10

What is Unity?

8 / 10

What are Ubuntu versions named after?

9 / 10

What's Ubuntu Core?

10 / 10

Which Ubuntu version is Snap introduced?

The average score is 68%