How to Install LEMP Stack on Ubuntu 18.04

Introduction

A LEMP stack is an open so urce software stack for building dynamic websites and web applications, and this guide will be showing abot LEMP Stack on Ubuntu system. Basically, LEMP is an acronym of its own components, Linux, Nginx pronounced as ‘Engine-X’, MySQL, and PHP. Each component has it’s own functionally, Linux as the operating system, Nginx as a HTTP web server, MySQL to store all site data, and dynamic content managed by PHP.

A LEMP stack becomes one of the most popular software stacks for web applications technology. The LEMP stack has been used for the most PHP web application frameworks such as Laravel, YII Framework, CodeIgniter etc. And most content management system such as WordPress, Magento, Drupal, and Joomla uses it too.

In this guide, we’re going to show you how to install the LEMP Stack on Ubuntu 18.04 Bionic beaver.

Requirements

In order to complete this guide, ensure you have the latest version of Ubuntu Server 18.04 with basic setup and configuration of the ufw firewall and sudo root privileges. The basic initial setup ubuntu server 18.04 can be done on this link.

Step 1 – Install Nginx Web Server

Nginx HTTP web server is one of the most popular web servers that can be used to serve the dynamic content and HTTP Reverse Proxy and load balancer. Widely used by large websites such as Github.com, Twitch.com, WordPress.com etc. In short, Nginx web server is a great choice to host your dynamic websites and web applications, especially when you’re on high loads.

First of all, ensure all repositories on the Ubuntu system is up to date. Or you can run the apt command below to update system repositories.

sudo apt update

Now install the Nginx with the following apt command.

sudo apt install nginx -y

Once the Nginx installation is complete, you will need to add Nginx to the ufw firewall application list.

Check available applications on the ufw firewall.

sudo ufw app list

Now you will get three different profiles for Nginx, add the ‘Nginx Full’ for opening both HTTP and HTTPS protocols on port ’80’ and ‘443’.

sudo ufw allow in "Nginx Full"

ufw firewall nginx

After that, open your web browser and type the server IP address on the address bar.

http://10.9.9.100/

And you will be displayed the Nginx default index html.

nginx default index.html

Step 2 – Installing MySQL Database

After installing Nginx HTTP web server, we’re going to install one of the most popular RDBMS (Relational Database Management System) MySQL server.

Install MySQL server using the following apt command.

sudo apt install mysql-server mysql-client -y

After that, we will secure the MySQL deployment by configuring the root password of the MySQL server. Simply, run the following command.

sudo mysql_secure_installation

And you will be asked about various configurations for securing the MySQL deployment.

Would you like to setup VALIDATE PASSWORD plugin?

Type ‘Y’ to yes.

Now Choose between three password policies, 0 for LOW, 1 for MEDIUM, and 2 for STRONG.

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 3

We recommend using the ‘STRONG’ password policy number ‘2’. Then type the strong password that you’re appropriate and type ‘Y’ again to confirm.

Then type ‘Y’ again for all the rest.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : Y
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y

And the MySQL server configuration is complete. Log in to the MySQL using the user root and your strong password, then create a new MySQL user for your applications.

mysql -u root -p
TYPE YOUR PASSWORD

Now run the following MySQL queries for creating a new MySQL user database for web aplication.

create user [email protected] identified by 'ahmad123#'

create database mydb;
grant all privileges on mydb.* to [email protected] identified by 'ahmad123#';

flush privileges;

At this point, a new MySQL user and database for web applications has been created.

create database and user mysql

Step 3 – Installing PHP-FPM 7.x

PHP is one of the most server scripting languages that has been used on the internet. It can run PHP scripts, connect to the MySQL database, and generate contents of web applications for you through a web browser.

Generally, the Nginx web server doesn’t have a native PHP processing engine like Apache. So, we need to install the PHP-FPM ‘FastCGI Processing Manager’ and configure the Nginx to pass all PHP requests to the PHP-FPM.

Install PHP and PHP-FPM 7.2 using the following commands.

sudo apt install php php-fpm php-mysql -y

Now the PHP and PHP-FPM has been installed to the system. And still, we need to make changes on the Nginx configuration to pass all PHP requests to the PHP-FPM. In order to do that, we need to edit the ‘default’ virtual host configuration on the ‘/etc/nginx/sites-available’ directory.

Go to the ‘/etc/nginx/sites-available’ directory, then edit the ‘default’ configuration file using vim/nano editor.

cd /etc/nginx/sites-available/
vim default

Add the ‘index.php’ to index list.

index index.php index.html index.htm index.nginx-debian.html;

Now change the server_name with the server IP address.

server_name 10.5.5.50;

After that, uncomment the PHP-FPM configuration like the following and change the PHP-FPM sock file with the new version ‘php7.2-fpm.sock’.

# pass PHP scripts to FastCGI server
#
location ~ \.php$ {

  include snippets/fastcgi-php.conf;
  
  # With php-fpm (or other unix sockets):
  fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
 
  # With php-cgi (or other tcp sockets):
  # fastcgi_pass 127.0.0.1:9000;
}

Save and close the file.

Next, test the new configuration file and ensure there is no error.

sudo nginx -t

If you get an error, go back and recheck the ‘default’ configuration file.

Now restart the Nginx service using the following command.

systemctl restart nginx

As a result, all PHP requests to the Nginx web server will be passed to the PHP-FPM ‘FastCGI Process Manager’.

Configure PHP-FPM with Nginx web server

Step 4 – Testing

Before going any further, make sure the Nginx and MySQL services is up running. Check using the following commands.

sudo systemctl status nginx
sudo systemctl status mysql

Now you will get both Nginx and MySQL services are up and running.

Next, go to the ‘/var/www/html’ directory and create a new php file called ‘info.php’.

cd /var/www/html
vim info.php

Paste the following PHP script into it.

<?php
phpinfo();
?>

Save and close the configuration.

After that, open the web browser again and type the server IP address following with the script file name.

http://10.9.9.20/info.php

And you will be shown the PHP configuration on the server.

lemp stack phpinfo on Ubuntu 18.04

After that, we’re going to test the PHP and MySQL by creating the other PHP file called ‘connector.php’.

vim connector.php

Change the MySQL user and password with your own that created on the top, then paste into it.

<html>
<head>
<title>Connector PHP to MySQL</title>
</head>
<body>
<?php echo "<center><p>Connector PHP to MySQL</p></center>";
// In the variables section below, replace user and password with your own MySQL credentials as created on your server
$servername = "localhost";
$username = "ahmad";
$password = "ahmad123#";
// Create MySQL connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection – if it fails, output will include the error message
if (!$conn) {
die("<p>Connection failed: <p>" . mysqli_connect_error());
}
echo "<center><h1>Connected successfully</h1></center>";
?>
</body>
</html>

Save and close.

Now type the server IP address again following with the PHP filename ‘connector.php’.

http://10.5.5.50/connector.php

And you will get the result as the PHP connection to MySQL database successfully.

testing LEMP Stack with MySQL Database and user

Finally, the installation of LEMP Stack on Ubuntu 18.04 has been completed successfully.