How to Install Fastest LEMP Stack on Ubuntu 18.04 / 18.10

LEMP stack thumbnail image

When I just started to learn about VPS and Linux the most difficult thing I felt in my initial days was to properly install a secure and Fast LEMP stack. I tried about more than 50 times but I failed every time. Then I figured what was actually going wrong! And I am here to show you the right way.

LEMP is a group of software stack that is often used to run high traffic and huge Web Application and Web Page. It is an acronym that usually describes a Linux Operating System (in this case Ubuntu), with an NGINX web server. The data of this type of web stack is usually stored in a MySQL database, and the dynamic processing is handled by PHP.

Do you Know that WordPress, Drupal or most other Software like these uses LEMP or LAMP stack?

Installing Nginx Web Server

Nginx or Engine-X stands for the E in the LEMP stack. And we are going to install this Nginx on our Ubuntu operating system.

All of the Software used here comes from the Ubuntu official apt repository. This means we will use the apt package management suite to complete the necessary installations.

Since this is your initial session on your newly created server we will first update the pre-installed packages and then proceed to install Nginx.

sudo apt update
sudo apt install nginx

On Ubuntu Nginx is pre-configured to run on system startup.

If you have a firewall installed in your Linux system like UFW you can open the NGINX HTTP port to allow connections to Nginx (not recommended while using AWS or Lightsail) by the following command:

sudo ufw allow 'Nginx HTTP'

similarly, if you want to use HTTPS you can open it also:

sudo ufw allow 'Nginx HTTPS'

You can also verify that Nginx in running or not by any of the following command:

sudo service nginx status
sudo systemctl status nginx

It should output:

nginx is running

Some useful Nginx commands:

1. Starting Nginx:

Nginx can be started manually by using the following command:

sudo systemctl start nginx.service

2. Stopping Nginx:

Nginx can be stopped manually by using the following command:

sudo systemctl stop nginx.service

3. Quit Nginx:

Do not confuse stopping Nginx with Quitting. Nginx quitting is similar to stopping but by using quitting nginx will Stop Gracefully. It will stop serving open connections before stopping. Use the Quitting feature of Nginx very carefully by the following command:

sudo systemctl quit nginx.service

4. Restart Nginx:

Restarting Nginx is similar to restarting your PC, Phone or Laptop. It just stops and then Automatically starts Nginx. Nginx can be restarted by the following command:

sudo systemctl restart nginx.service

5. Reload Nginx:

Again reload nginx is different from Restart. It does it gracefully. According to official Nginx docs reload “start the new worker process with a new configuration, gracefully shut down old worker processes”. Nginx can be reloaded by the following command:

sudo systemctl reload nginx.service

6. Test Nginx Configuration:

If you are editing Nginx core files this comes handy to check the configuration before implementing those changes by reloading or restarting. The Nginx Configuration can be checked by any of the following command:

sudo nginx -t
sudo systemctl config nginx.service
sudo service nginx configtest

it should output something like this:

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

7. Check Nginx Version:

If are eager to see which version of Nginx are you running then run any of the following commands:

sudo systemctl -V nginx
sudo nginx -v

Installing MySQL 8.0 to manage Site Data:

MySQL 8.0 is the fastest of any version of MySQL ever made today. It is even faster than MariaDB. But unfortunately it is not yet available in the official Ubuntu repo. To use MySQL 8.0 we need to install it from the official MySQL Developer repo.

cd /tmp/ && wget
sudo dpkg -i mysql-apt-config_0.8.10-1_all.deb

When you run the above command you should see something like this:

LEMP Stack MySQL 8.0 install on Ubuntu 18.04 screenshot

Just select Ok.

Now official MySQL repo is added to our ubuntu repository list and we can update our apt to begin install MySQL 8.0

sudo apt update
sudo apt install mysql-server mysql-client

This will install MySQL. When Prompted Enter a Password for MySQL root or if you don’t want to add password to your MySQL root you can just press Enter.

LEMP stack MySQL install password prompt screenshot

When you are prompted to select MySQL Password Encryption Select the Recommended one.

LEMP stack MySQL Strong PAssword Encryption.

After sucessful MySQL installation you can login to your MySQL server

sudo mysql -u root -p

You should see MySQL welcome screen like this:

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.11 MySQL Community Server - GPL

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

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.

type this to exit


Some useful MySQL Statement:

You can also reffer the MySQl official docs to know about MySQL Statements and their uses. Here I will show some basic things like restarting, starting MySQL server which you can’t find on the official docs.

1. Starting MySQL Server:

To manually start the MySQL server you need to use this command:

sudo systemctl start mysql.service

2. Stopping MySQL Server:

To manually stop MySQL server you need to use this command:

sudo systemctl stop mysql.service

3. Restarting MySQL Server:

sudo systemctl restart mysql.service

Installing PHP and Configuring Nginx to use PHP:

Now, you have installed Nginx, installed MySQL for storing data but still your Web Stack is not able to generate dynamic data. And here we need PHP.

Since Nginx doesn’t come bundled with native PHP processing you need to install php-fpm (FastCGI process manager), which will tell Nginx to pass PHP process to it for processing.

Note: Depending on your cloud provider, you may need to add Ubuntu’s repository universe, which includes free and open-source software maintained by the Ubuntu community, before installing the package php-fpm. You can do this by typing: sudo add-apt-repository universe 

install the php-fpm and related modules by typing this:

sudo apt install php-fpm php-common php-mbstring php-xmlrpc php-soap php-gd php-xml php-intl php-mysql php-cli php-zip php-curl

Now, we all the LEMP stack component required but for a better security and speed we need to edit some of the configurations.

In order to tell Nginx to use the PHP processor for dynamic content we need to edit the file located on nginx sites-enabled. To do this, open a new server block configuration file within the /etc/nginx/sites-available/ directory. For the purpose of this tutorial we have named the server block But you name name yours what you want.

sudo nano /etc/nginx/sites-available/

By editing a new server block configuration file, rather than editing the default one, you’ll be able to easily restore the default configuration if you ever need to.

Add the following content, which was taken and slightly modified from the default server block configuration file, to your new server block configuration file:

server {
        listen 80;
        root /var/www/html;
        index index.php index.html index.htm index.nginx-debian.html;
        server_name; //remember to replace this with your domain name or IP

        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;

Here’s what each of these directives and location blocks do:

  • listen — Defines what port Nginx will listen on. In this case, it will listen on the port 80, the default port for HTTP.
  • root — Defines the document root where the files served by the website are stored.
  • index — Configures Nginx to prioritize serving files named whenindex.php an index file is requested if they’re available.
  • server_name — Defines which server block should be used for a given request to your server. Point this directive to your server’s domain name or public IP address.
  • location / — The first location block includes a try_files directive, which checks for the existence of files matching a URI request. If Nginx cannot find the appropriate file, it will return a 404 error.
  • location ~ \.php$ — This location block handles the actual PHP processing by pointing Nginx to the configurationfastcgi-php.conf file and the file,php7.2-fpm.sock which declares what socket is associated with.php-fpm
  • location ~ /\.ht — The last location block deals with files.htaccess, which Nginx does not process. By adding the directive deny all, if any files.htaccess happen to find their way into the document root they will not be served to visitors.

After adding this content, save and close the file. Enable your new server block by creating a symbolic link from your new server block configuration file (in the /etc/nginx/sites-available/ directory) to the /etc/nginx/sites-enabled/ directory:

sudo ln -s /etc/nginx/sites-available/ /etc/nginx/sites-enabled/

Then, unlink the default configuration file from the /sites-enabled/ directory:

sudo unlink /etc/nginx/sites-enabled/default

Note: If you ever need to restore the default configuration, you can do so by recreating the symbolic link, like this:

sudo ln -s /etc/nginx/sites-available/default /etc/nginx/sites-enabled/

Test your new configuration file for syntax errors by typing:

sudo nginx -t

If any errors are reported, go back and recheck your file before continuing.

When you are ready, reload Nginx to make the necessary changes:

sudo systemctl reload nginx

This concludes the installation and configuration of your LEMP stack. However, it’s important to confirm that all of the components can communicate with one another.

PHP file to Test Configuration:

To check if all the component on your LEMP stack works properly, you need to make a sample php file in the document root:

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

Enter the following lines into the new file. This is valid PHP code that will return information about your server:


When you are finished, save and close the file.

Now, you can visit this page in your web browser by visiting your server’s domain name or public IP address followed by /info.php:

PHP info screenshot

After verifying that everything is working properly we need to remove that file for security purposes:

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

With that, you now have a fully-configured and functioning LEMP stack on your Ubuntu 18.04 server.


A LEMP stack is a powerful platform that will allow you to set up and serve nearly any website or application from your server.

There are a number of next steps you could take from here. For example, you should ensure that connections to your server are secured. To this end, you could secure your Nginx installation with Let’s Encrypt. By following this guide, you will acquire a free TLS/SSL certificate for your server, allowing it to serve content over HTTPS.

Leave a Comment

Your email address will not be published. Required fields are marked *