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 upgrade
Code language: Bash (bash)
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 nginx
Code language: Bash (bash)
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.
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-server
Code language: Bash (bash)
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
Code 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.”
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 mysql
Code 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.
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-opcache
Code language: Bash (bash)
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/default
Code 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)
Test the modified Nginx configuration file for syntax errors by entering the following command:
sudo nginx -t
Code language: Bash (bash)
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 nginx
Code 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.php
Code 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.
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.