How to Install LEMP on Ubuntu Linux
LEMP is a popular bundle of software for running websites on Ubuntu Linux.
LEMP stands for Linux (your Ubuntu operating system), Nginx (a fast web server), MariaDB (a database like MySQL), and PHP (a scripting language for making web pages interactive).
You need LEMP to run dynamic websites and applications, like WordPress, especially if you want your site to load quickly. This guide focuses on installing LEMP on Ubuntu 22.04 LTS, a stable version of Ubuntu that’s great for hosting.
Setting up LEMP on Ubuntu 22.04 LTS involves installing these four key pieces of software.
Install LEMP on Ubuntu by first updating packages with `sudo apt update` and `sudo apt dist-upgrade`. Then, install Nginx using `sudo apt install nginx` and MariaDB with `sudo apt install mariadb-server`. Finally, configure PHP with `sudo apt install php-fpm`.
How to install Ubuntu Linux
Ubuntu Linux is a popular choice for servers, especially when you plan to install a LEMP stack. Before you begin setting up LEMP, make sure you have Ubuntu Linux installed. Once it’s ready, the very first command you should run is `sudo apt update` to make sure your system has the latest software information.
A Linux machine serves as the initial requirement for LEMP configuration. Users can install Ubuntu Linux by following the detailed steps provided in a separate guide.
Once Ubuntu is installed, run the commands below to update it.
sudo apt update sudo apt dist-upgrade sudo apt autoremove
There are lots of other settings and configurations to apply that apply to Ubuntu. However, the post is only concerned with installing LEMP.
How to install Nginx on Ubuntu Linux
Nginx is the web server software that makes up the ‘N’ in your LEMP setup, and it’s quite straightforward to install on Ubuntu. To get Nginx running, you’ll first refresh your package list with `sudo apt update`, and then install Nginx itself using the command `sudo apt install nginx`.
sudo apt update sudo apt install nginx
After installing Nginx, the commands below can be used to stop, start and enable the Nginx service to always start up when the server starts.
sudo systemctl stop nginx sudo systemctl start nginx sudo systemctl enable nginx
To see if Nginx is installed, open a web browser and browse to the server hostname or IP address.
http://localhost

If you see the above Nginx welcome page, it means Nginx is installed and functioning.
How to install MariaDB on Ubuntu Linux
MariaDB acts as the database server, the ‘M’ in your LEMP stack, and installing it on Ubuntu is a simple process. First, update your software list by running `sudo apt update`. Then, you can install the MariaDB server with the command `sudo apt install mariadb-server`.
For this post, we will install MariaDB instead of MySQL.
To install MariaDB, run the commands below.
sudo apt update sudo apt install mariadb-server
After installing, you can run the commands below to view the MariaDB service status.
sudo systemctl status mariadb
After running the command above, it should output similar lines.
mariadb.service - MariaDB 10.3.31 database server
Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2021-09-15 16:40:20 CDT; 22s ago
Docs: man:mysqld(8)
https://mariadb.com/kb/en/library/systemd/
Main PID: 3007 (mysqld)
Status: "Taking your SQL requests now..."
Tasks: 31 (limit: 4651)
Memory: 65.6M
CGroup: /system.slice/mariadb.service
└─3007 /usr/sbin/mysqld
Sep 15 16:40:20 ubuntu2004 /etc/mysql/debian-start[3045]: mysql
Sep 15 16:40:20 ubuntu2004 /etc/mysql/debian-start[3045]: performance_schemaMariaDB and MySQL have a script allowing you to perform security operations.
Run the commands below to invoke the script and perform some recommended tasks to secure the database.
sudo mysql_secure_installation
MariaDB and MySQL servers have the root user set to use the auth_socket authentication method by default.
The auth_socket plugin authenticates users that connect from the local host through the Unix socket file. You can’t authenticate as a root by providing a password.
Simply run the command below to log on to MariaDB and MySQL servers as root. You don’t need a password since it’s using the auth_socket method.
To log on to MariaDB, run the commands below.
sudo mysql
The server console should come up.
Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 8 Server version: 8.0.26-0ubuntu0.20.04.2 (Ubuntu) Copyright (c) 2000, 2021, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or 'h' for help. Type 'c' to clear the current input statement. mysql>
How to install PHP on Ubuntu Linux
PHP is the ‘P’ in LEMP, and to make it work with Nginx, you need to install PHP-FPM on Ubuntu. Unlike Apache, Nginx doesn’t handle PHP files directly. PHP-FPM acts as a special manager that helps Nginx process PHP code correctly.
Nginx doesn’t have built-in support for processing PHP files and is not tightly integrated as Apache. To add PHP support for Nginx, you must install and use PHP-FPM (FastCGI process manager) to handle the PHP files.
If you’re using the Nginx web server, the commands below are used to install PHP.
sudo apt update sudo apt install php-fpm
Because PHP isn’t tightly integrated with Nginx, if you make changes to PHP, you must restart or reload PHP and Nginx separately for the changes to apply.
sudo systemctl restart php-fpm sudo systemctl restart nginx
server {
location ~ .php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.0-fpm.sock;
}
}To install the latest PHP versions unavailable in the Ubuntu repository, run the commands below to install a third-party PPA repository that includes multiple versions of PHP.
At the time of this writing, the latest version of PHP is 8.0.
sudo apt install software-properties-common sudo add-apt-repository ppa:ondrej/php
After adding the repository above, you can then install another PHP version.
sudo apt install php8.0 php8.0-common php8.0-cli php8.0-gd php8.0-curl php8.0-mysql
That should do it!
For more on LEMP, read individual posts on the LEMP components
Conclusion:
This guide explains installing the LEMP stack on Ubuntu Linux. Users can report errors or add information using the comment form below the guide.
Was this guide helpful?
About the Author
Richard
Tech Writer, IT Professional
Richard, a writer for Geek Rewind, is a tech enthusiast who loves breaking down complex IT topics into simple, easy-to-understand ideas. With years of hands-on experience in system administration and enterprise IT operations, he’s developed a knack for offering practical tips and solutions. Richard aims to make technology more accessible and actionable. He's deeply committed to the Geek Rewind community, always ready to answer questions and engage in discussions.
[…] How to install LEMP on Ubuntu Linux […]
[…] ready to connect to our Google Cloud server and install any CMS based on LAMP or LEMP by tying software and packages installed in our previous […]