How to install LEMP on Ubuntu 18.04

In today's article, find out how to install LEMP on Ubuntu 18.04, as well as what LEMP actually stands for.

Updated: 05 Mar, 23 by linda Y 10 Min

List of content you will read in this article:

Once you have an idea for a complex website or a web application, before you even start working on its design, you should decide how the backend will look like. If you are new to the world of web hosting, here are some basic software components you need to host a web app:

  • Server operating system
  • Web server software
  • Database
  • Scripting language for dynamic content

When combined all together, it is called a web hosting stack. While there are many possible stacks out there, some are more popular than others, such as LEMP. In today’s article we will discuss the different components that make up this stack, followed by a detailed step-by-step tutorial on how to install it on Ubuntu.

LEMP is an acronym for Linux, Nginx, MySQL, and PHP. LEMP stack, just like the LAMP stack (with Apache instead of Nginx), is used for the development and deployment of web applications. Nginx in LEMP provides modular event-driven architecture handling requests using asynchronous events. This feature enables it to have high performance even under high loads. MySQL is used to store the website’s data whereas PHP is for processing the dynamic content of the sites.

This is all that you need in order to install the LEMP stack on your Ubuntu VPS/Server.

  • Linux VPS running Ubuntu version 18.04
  • Username and password for your VPS Server.

This tutorial is split into distinctive parts, each with its own step numbers. We strongly recommend following the order outlined in this article. Also, please note that the commands used to complete each step are written after each one.

Part 1: Connect to your server via SSH

All VPS plans allow SSH access. You should therefore connect to your Ubuntu server with the public IP address that was allocated to you. If you are running Windows as your local computer, consider downloading the PuTTY SSH client. You can also initiate an SSH connection from Mac or Linux using the inbuilt SSH client Terminal.

Click here for more info on how to connect to a Linux VPS.

Part 2: Install Nginx server

Step 1: Update your software packages.

$ sudo apt update

Step 2: Install Nginx

$ sudo apt install nginx

Step 3: Once you have installed it, the Nginx service should start automatically and will be enabled to start at boot time, you can check if it’s up and running using the following code.

$ sudo systemctl status nginx

Step 4: If you have a firewall enabled and running on your system, you should open ports 80 (HTTP) and 443(HTTPS) to allow client requests to the Nginx web server, and reload the firewall rules.

$ sudo ufw allow 80/tcp

$ sudo ufw allow 443/tcp

$ sudo ufw reload

Step 5: Next, test if the Nginx package was successfully installed and is working fine. Type your server's IP address in your web browser. If you see the Nginx default web page, it means your installation is working fine.

Part 3: Install MySQL

Now that you have a web server, you need to install MySQL (a database management system) to store and manage the data for your site.

Step 1: Update your software packages.

$ sudo apt update

Step 2: Install MySQL

$ sudo apt install mysql-server

Step 3: Press “y” and “Enter” when prompted to install the MySQL package.

Step 4: Once the package installer has finished, we can check to see if the MySQL service is running.

$ sudo service mysql status

If it is running, you will see a green active status like the one shown below.

mysql.service - MySQL Community ServerLoaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled (Active: active (running) since Wed 2018-05-09 21:10:24 UTC; 16s agoMain PID: 30545 (mysqld)Tasks: 27 (limit: 1153)CGroup: /system.slice/mysql.service30545 /usr/sbin/mysqld --daemonize --pid-file=/run/mysqld/mysqld.pid 

You may need to press “q” to exit the service status.

Part 4: Configure MySQL Security

Step 1: You should now run mysql_secure_installation to configure security for your MySQL server.

$ sudo mysql_secure_installation

Step 2: If you have a root password, you may be prompted to enter it here, otherwise you will be asked to create one.

Step 3: You will be asked if you want to set up the Validate Password Plugin. It’s not really necessary unless you want to enforce strict password policies. Press “n” and “Enter” here if you don’t want to set up the validate password plugin.

"VALIDATE PASSWORD" plugin can be used to test passwords and improve security. It checks the strength of the passwords and allows the users to set only those passwords which are secure enough, meaning you cannot use blank passwords. 

Step 4: You will be prompted with: Remove anonymous users? Press “y” and “Enter” to do so.

By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL without having to have a user account created for them. This is intended only for testing and to make the installation go a bit smoother. You should remove them before moving into a production environment.

Step 5: The next prompt you will encounter is: Disallow root login remotely? Press “y” and “Enter” to disallow root login remotely.

Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess the root password from the network.

Step 6: The following prompt will look like this: Remove test database and access to it? Press “y” and “Enter” to remove the test database.

By default, MySQL comes with a database named 'test' that anyone can access. This is also intended only for testing and should be removed before moving into a production environment.

Step 6: Lastly, you will see: Reload privilege tables now? Press “y” and “Enter” to reload the privilege tables.

Reloading the privilege tables will ensure that all changes made so far will take effect immediately.

Step 7: As a test, you can log into the MySQL server and run the version command.

$ sudo mysqladmin –p –u root version

Step 8: Enter the MySQL root password you created earlier and you should see the following:

$ sudo mysqladmin –p –u root versionmysqladmin  Ver 8.42 Distrib 5.7.22, for Linux on x86_64Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Server version          5.7.22-0ubuntu18.04.1Protocol version        10Connection              Localhost via UNIX socketUNIX socket             /var/run/mysqld/mysqld.sockUptime:                 4 min 28 secThreads: 1  Questions: 15  Slow queries: 0  Opens: 113  Flush tables: 1  Open tables: 106  Queries per second avg: 0.055

You have now successfully installed and configured MySQL for Ubuntu 18.04! Continue to the next part to install PHP.

Part 5: Install PHP on Ubuntu 18.04

PHP is a popular server-side scripting language used to generate dynamic content on websites. You can install PHP, PHP-FPM, and other modules for web development using the following command (the default version in the Ubuntu repos is PHP 7.2).

Step 1: Install PHP and required modules

$ sudo apt install php php-fpm php-common php-mysql php-gd php-cli

Step 2: After PHP installation, the PHP7.2-FPM service should also start automatically, you can verify the service is running using the following command.

$ sudo systemctl status php7.2-fpm

Step 3: Next, configure PHP-FPM properly to serve PHP-based web applications or sites, in the configuration file /etc/php/7.2/fpm/php.ini by opening it with an editor, for this example, we are using vim.

$ sudo vim /etc/php/7.2/fpm/php.ini

Step 4: In the opened file, search for the cgi.fix_pathinfo=1 and change it to the following.

cgi.fix_pathinfo=0

Step 5: Then configure PHP-FPM to process PHP scripts in Nginx default server block configuration file (/etc/nginx/sites-available/default) by opening it with a text editor.

$ sudo vim /etc/nginx/sites-available/default

Step 6: Uncomment the configuration section below to pass PHP scripts to FastCGI server.

location ~ \.php$ {            include snippets/fastcgi-php.conf;               fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;}

Step 7: After making changes, restart the php7.2-fpm and Nginx services to apply the recent changes.

$ sudo systemctl restart php7.2-fpm

$ sudo systemctl restart nginx

Step 8: Now you can test your PHP setup by creating a simple info.php page in your web server document root, with this single command.

$ echo "" | sudo tee /var/www/html/info.php

Step 9: Next, open a web browser, and enter any of the following URL to view the PHP information page.

http://domain_name/info.phpORhttp://SERVER_IP/info.php

 

And that’s it. The LEMP stack should now be installed on your Linux server. LEMP can also be installed on other Linux distributions too such as Debian. Here is a complete guide on installing LEMP on Debian.

We hope that by following all the steps outlined in this blog, you successfully installed the LEMP stack on your Linux server and learned a little about the stack itself. If you have any questions or suggestions, please leave them in the comment section down below. You could also post your request if you wish to see tutorials for the installation of the LEMP stack on other operating systems, or the installation of other stacks on Ubuntu.

linda Y

linda Y

My name is Linda, I have Master degree in Information Technology Engineering. I have some experiences in working with Windows and Linux VPS and I have been working for 2 years on Virtualization and Hosting.