May 18, 2022 3 min read

How To Install The LEMP Stack On Ubuntu Linux

L for the Linux Operating System, E for Nginx(Engine X) Web Server, M for MySQL, and P for PHP. In this tutorial, we will cover how to install the LEMP stack on Ubuntu Linux.

How To Install  The LEMP Stack On Ubuntu Linux
LEMP Stack
Table of Contents

The LEMP stack is a group of open-source software that is usually installed together to enable a server to host websites and web applications. The acronym represents L for the Linux Operating System, E for Nginx(Engine X) Web Server, M for MySQL, and P for PHP. In this tutorial, we will cover how to install the LEMP stack on Ubuntu Linux.

Update Software Packages

Before we start the installation it is a good idea to update software packages with the following command:

sudo apt update

Install Nginx Web Server

Next, we will install Nginx Web Server:

sudo apt install nginx

Once the installation is completed the Nginx service should be automatically started. Check Nginx’s status with systemctl:

sudo systemctl status nginx

If it is a case that Nginx is not running then run the following command:

sudo systemctl start nginx

Also, configure Nginx to start automatically when your server or instance boots up:

sudo systemctl enable nginx

Ensure that HTTP traffic is allowed through the firewall :

sudo ufw allow http

You can then type in your server IP address into a browser on your local network to see the default Nginx Web Server page.

Nginx Default Web Page

Install MySQL(MariaDB)

Once the webserver is up and running we will install MySQL(MariaDB) next. MariaDB is a community-developed fork of the MySQL relational database management system. Run the following command to start installing MariaDB:

sudo apt install mariadb-server mariadb-client

Once the installation is completed you should check the status to see if MariaDB is active with the following command:

sudo systemctl status mariadb

Sample output:

mariadb.service - MariaDB 10.1.34 database server
   Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled)
   Active: active (running) since Sat 2018-09-08 11:13:27 UTC; 21s ago
     Docs: man:mysqld(8)
           http://mariadb.com/kb/en/library/systemd/
 Main PID: 3473 (mysqld)
   Status: "Taking your SQL requests now..."
    Tasks: 27 (limit: 505)
   CGroup: /system.slice/mariadb.service
           └─3473 /usr/sbin/mysqld

If it is not running you can start the MariaDB server with the following command:

sudo systemctl start mariadb

The next step is to enable MariaDB to start automatically at system boot:

sudo systemctl enable mariadb

Once the MariaDB service is up and running, the next step is to start the MySQL installation script:

sudo mysql_secure_installation

When it prompts you for the MariaDB root password you can go ahead and press enter and then enter y to set the root password for the MariaDB server. Next, you can press enter through the remaining steps which will remove the anonymous user, disable remote root login, and remove the test database.

Install PHP

The last step is to install PHP on your Ubuntu Linux server or instance:

sudo apt-get install php-fpm php-mysql

Once PHP is installed create a script info.php under the webroot directory:

sudo vi /var/www/html/info.php

The script will create a blank file and then we will add the following PHP code inside the file:

<?php
phpinfo();
?>

Once all the above steps are completed view the info.php page in the web browser using the server IP:

http://<server_ip_address>/info.php

The page will look similar to the one below:

PHP Info Web Page

Conclusion

In conclusion, Nginx is one if not the most widely used and popular opensource web servers and reverse proxies utilized by major companies and organizations worldwide that require high performance and scalability. If you enjoyed this article consider signing up for our newsletter and don't forget to share it with people that would find it useful. Leave a comment below with a tutorial you would like us to cover.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to Opensource Geeks.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.