×


Install LEMP Stack on Ubuntu 18.04 - Step by Step Process ?

LEMP (Linux, Nginx, MySQL, PHP) is a variation of the LAMP stack (Linux, Apache, MySQL, PHP). The only difference being LEMP uses Nginx, where LAMP uses Apache. Nginx is much faster, and generally more secure than Apache.

Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to perform related LEMP queries.

In this context, we shall look into a step by step process on how to install LEMP stack on Ubuntu 18.04 server.


How to install LEMP Stack on Ubuntu ?

Before starting this installation procedure, ensure that you use a non-root sudo enabled user account.

Then, follow the steps given below.


1. Install Nginx Web Server

Nginx is a modern and efficient web server now a days. This will be used to show web pages to our site users.

i. First you need to update your software packages and then install Nginx, an open source, fast and high-performance web server so execute following command to update packages and install Nginx web server :

$ sudo apt update
$ sudo apt install nginx

ii. Once it installed, Nginx service should start automatically and will be enabled to start at boot time. 

You can check status by executing below command :

$ sudo systemctl status nginx

If you have enabled firewall then we need to allow connections to Nginx. 

By default Nginx registers itself with ufw upon installation. 

You can enabled by below command :

$ sudo ufw allow 'Nginx HTTP'

You also can verify by execute command :

$ sudo ufw status

Alternate option is, you can directly allow default ports by following commands:

$ sudo ufw allow 80/tcp
$ sudo ufw allow 443/tcp
$ sudo ufw reload

Now, we will test is nginx is installed properly and working or not. 

So open your web browser and type below url and it will open nginx default page as given below:

http://YOUR_SERVER_IP/

Now, you will see the Welcome to NGINX page.


2. Install MySQL

As you know MySQL is a database management system. So, It will organize and provide access to databases where your site can store information. 

i. Execute following command to install mysql server:

$ sudo apt install mysql-server

This command will also show you details of packages which will be install and how much disk space will take. 

Press Y to continue. Also it will prompt you to set MySQL root user password and it will be your mysql root password.

ii. After that, now run MySQL pre-installed simple security script. Which will remove defaults and lock down access to your database system. 

Execute given command :

$ sudo mysql_secure_installation

It will prompt you to configure password validation policy and more settings for MySQL. 

It will also ask to remove unnecessary users, disallow remote root login and test databases so go throughout it.

Finally, your database set up is completed and we will go ahead to install last component of the LEMP stack.


3. Install PHP and Configuring Nginx

Now, we have installed Nginx web server and MySQL database but these are only will not display dynamic content so we need to install PHP to play this role.

Nginx does not contain native PHP processing like some other web servers, you will need to install php-fpm, which stands for "fastCGI process manager". 

i. We will tell Nginx to pass PHP requests to this software for processing:

$ sudo add-apt-repository universe

ii. Now we will install php-fpm module along with an additional helper package, php-mysql. php-mysql will allow PHP to communicate with your database backend. 

Execute below command :

$ sudo apt install php-fpm php-mysql

After complete this installation you need to do few configuration changes in order to tell Nginx to use the PHP processor for dynamic content.

In Nginx have server blocks like VirtualHost in Apache, so we will do changes at server block level. 

iii. Create a new file server block configuration file at /etc/nginx/sites-available/ directory. 

Give file as your domain name like yourdomain.com:

$ sudo nano /etc/nginx/sites-available/yourdomain.com

iv. Add the following content to this file :

server {
listen 80;
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name yourdomain.com;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}

v. Save and close the file. 

vi. Now, you have to create a symbolic link from your new server block configuration file to enable your new server block. 

It will be stored at /etc/nginx/sites-available/ directory. 

Execute below command :

$ sudo ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/

vii. Next, run below command to test new configuration file for syntax errors :

$ sudo nginx -t

If everything is okay then it will show output as following :

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

viii. You must need to reload Nginx in order to take changes in effect by below command :

$ sudo systemctl reload nginx


4. Test Configuration

Now your LEMP stack completely set up. So we will check it by creating a .php file. 

i. Create a test PHP file called info.php in your document root:

$ sudo nano /var/www/html/info.php

ii. Add following code lines to this file :

<?php
phpinfo();
?>

iii. Save and close this file. Now visit this page using your server's public ip address like below :

http://YOUR_SERVER_IP_OR_DOMAIN/info.php

You should see a web page that has been generated by PHP with information about your server.


[Need urgent assistance in fixing NGINX errors? We can help you. ]


Conclusion

This article covers how to Install a LEMP Stack on Ubuntu 20.04 LTS. LEMP stack is a group of open-source software that is typically installed together to enable a server to host dynamic websites and web apps. LEMP stands for Linux OS, with the Nginx (pronounced like "Engine-X") web server, Data store in a MySQL database, and dynamic content is processed by PHP.


To install Nginx Web Server:

1.  Run the commands below to install Nginx.

$ sudo apt update
$ sudo apt install nginx

2. Allow traffic on port 80.

$ sudo ufw allow 'Nginx HTTP'

You can verify the change by running:

$ sudo ufw status