This post shows students and new users the steps to install and use the LEMP stack on Ubuntu Linux.

LEMP is an acronym for Linux (Ubuntu), me Nginx [engine x], SUBWAYariaDB or SUBWAYySQL and PHP Scripting Language. It is a group of open source software and building blocks for many of the web applications and most content management systems (CMS) in use today.

Many of the popular content management systems in use today use some combinations of this open source framework. From WordPress to Drupal to Joomla and many others, everyone is using the LEMP stack to power their applications.

If you are developing PHP-based applications or websites, you will probably also use the LEMP stack. Next, we will show you how to get the stack up and running on Ubuntu Linux.

If you are a student or a new user learning Linux, the easiest place to start learning is on Ubuntu Linux. Ubuntu is the modern open source Linux operating system for desktops, servers, and other devices.

To start installing the LEMP stack on Ubuntu Linux, follow the steps below:

How to install Ubuntu Linux

L in LEMP it means Linux, in this case Ubuntu. This post chooses Ubuntu Linux because it is easy to use and basically for beginners. Whether you are a new student or new user, Ubuntu Linux is a great place to start with Linux.

Your first task to configure LEMP is a Linux machine. If you haven’t installed Ubuntu before, read this post to learn how to install it.

Once Ubuntu is installed, run the following commands to update it.

sudo apt update
sudo apt dist-upgrade
sudo apt autoremove

There are many other settings and configurations to apply that apply to Ubuntu, however the post only refers to the LEMP installation.

How to install Nginx on Ubuntu Linux

Nginx (engine-x) represents the me in LEMP and is the most popular open source web server and a key component of the LEMP stack. Nginx is available in the Ubuntu repositories. Update the package index and install Nginx with the following commands.

sudo apt update
sudo apt install nginx

After installing Nginx, the following commands can be used to stop, start and enable The Nginx service is always started 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 look for the server’s hostname or IP address.

http: // localhost

If you see the Nginx welcome page above, it means that Nginx is installed and working.

How to install MariaDB on Ubuntu Linux

the SUBWAY in LEMP it represents the MariaDB or MySQL database server. Both MySQL and MariaDB are an open source, multi-threaded relational database management system and a key component of the LEMP stack.

For this post, we will install MariaDB instead of MySQL.

To install MariaDB, run the following commands.

sudo apt update
sudo apt install mariadb-server

After installation, you can run the following commands to view the status of the MariaDB service.

sudo systemctl status mariadb

After running the above command, it should generate lines similar to the ones shown below.

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)
   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_schema

Both MariaDB and MySQL come with a script that allows you to perform some security operations.

Run the following commands to invoke the script and perform some recommended tasks to protect the database.

sudo mysql_secure_installation

Both MariaDB and MySQL servers come with the root user configured to use the auth_socket default authentication method.

the auth_socket The plugin authenticates users connecting from the local host through the Unix socket file. This means that you cannot authenticate as root by providing a password.

To log in to the MariaDB and MySQL servers as root, just run the following command. You don’t need a password as it uses the auth_socket method.

To log in to MariaDB, run the following commands.

sudo mysql

The server console should appear.

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

Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.


How to install PHP on Ubuntu Linux

the P in LEMP it means PHP. PHP supports many types of web servers, including Nginx, Apache, and a few others.

Nginx does not have built-in support for processing PHP files and is not tightly integrated like Apache. To add PHP support for Nginx, you will need to install and use PHP-FPM (fastCGI process manager) to handle PHP files.

If you are using the Nginx web server, the following commands are used to install PHP.

sudo apt update
sudo apt install php-fpm

Because PHP is not tightly integrated with Nginx, if you make changes to PHP, you must restart or reload PHP and Nginx separately for the changes to take effect.

sudo systemctl restart php-fpm
sudo systemctl restart nginx

Also, to allow Nginx to read PHP files, you must add these lines to the Nginx server block. Remember to use the installed PHP version and reference it in the highlighted line shown below.

server {

    location ~ .php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php8.0-fpm.sock;

To install the latest versions of PHP that are not available in the Ubuntu repository, run the following commands to install a third-party PPA repository that includes multiple versions of PHP.

At the time of 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 above repository, you can install another version of PHP.

sudo apt install php8.0 php8.0-common php8.0-cli php8.0-gd php8.0-curl php8.0-mysql

You should do that!

To learn more about LEMP, read individual posts on LEMP components


In this tutorial we have seen how to install the LEMP stack on Ubuntu Linux. If you find any errors above or have something to add, please use the comment form below.

Write A Comment