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

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

This guide shows you step-by-step the installation process of 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 Nginx Web Server, MySQL / MariaDB Database, and PHP Scripting Language on a Linux operating system.

So if you plan to create PHP-based applications or websites or intend to use CMS systems such as WordPress or Joomla, you will almost certainly use the LEMP stack.

This guide demonstrates installing a LEMP stack (Nginx, MariaDB, PHP) on an Ubuntu 22.04 LTS server. 

All that is required to get the LEMP stack up and running is to follow the steps shown, and in no time, you will have a reliable platform to serve your web applications. So without further ado, let’s get started.

Step 1: Update the Packages List

First, we will use the apt command to update the list of available packages on our system. 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 prevent difficulties.

sudo apt update
sudo apt upgrade
Updating Ubuntu 22.04 Packages List

Make sure the system is fully updated. If packages are waiting to be updated, our advice is to apply them before moving on.

Step 2: Installing the Nginx Web Server

The web server enables you to serve content such as web pages to website visitors. 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 nginx
Installing Nginx Web Server

After complete installation, the Nginx web server will be up and running 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: Installing MariaDB Server

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

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

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

sudo apt install mariadb-server
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_installation

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 the MariaDB Installation

To check if the database server is functioning normally, let’s log into it and run a simple query.

To log in, type the command shown below.

sudo mysql

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

select version();

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

Testing MariaDB Installation

Step 4: Installing PHP Scripting Language

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 need to install and use PHP-FPM to execute PHP files.

So, to install PHP-FPM and several 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-opcache
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: Configuring 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/default

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;
}
Configuring Nginx to Execute PHP Files

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

sudo nginx -t
Testing Nginx's Configuration

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

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

sudo systemctl restart nginx

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

Step 6: Test your LEMP Installation

You have completed the installation of Nginx, MariaDB, and PHP on the 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.php

Now, you can access test.php from a web browser, using your site’s domain or server’s IP address followed by /test.php. A web page with complete information about your server 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, in Ubuntu 22.04 LTS. PHP-based web apps can now run on your server.

There are several next steps you could take from here. For example, we recommend that you read our guide on setting SSL certificates on the Nginx server to ensure that 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.

Leave a Reply

Your email address will not be published.