How to Install the LEMP (Linux, Nginx, MySQL, PHP) Stack on an Ubuntu 18.04 VPS or Dedicated Server

How to Install the LEMP (Linux, Nginx, MySQL, PHP) Stack on an Ubuntu 18.04 VPS or Dedicated Server

The LEMP Stack is a combination of free open-source software applications that work together to run websites and web application. These tools are commonly used in dynamic websites.

The ‘L’ part represents Linux – a prominent operating system. According to w3techs.com stats which are updated daily, Linux powers about 40.8% of all websites hosted on the web today. There are lots of different Linux distributions but we will focus on Ubuntu 18.04 on this guide.

‘E’ stands for Nginx because the name is pronounced as ‘Engine-X’. This is a modern web server that competes with Apache. It is popularly known for its accelerated content delivery, state-of-the-art security features as well as scalability.

Nginx runs the busiest websites on the internet and controls about a quarter of all websites according to netcraft.com.

‘M’ means, MySQL – a relational database management software that is widely used in production environment and it has all comprehensive features and management tools for managing modern databases. MySQL is popularly used by big companies like PayPal, YouTube, eBay, Twitter and Facebook.

On the other hand, PHP (Hypertext Preprocessor) is a server side general purpose programming language. PHP makes websites more interactive by communicating with MySQL databases to retrieve dynamic content.

In order to have a fully functional web server on your ubuntu 18.04 VPS, you should install Linux, Nginx, MySQL and PHP and this forms the basis of this guide.

Prerequisites

  • A VPS account
  • Username and password for your VPS account

Special Note: Find the best VPS hosting providers on HostAdvice, alongside reviews, prices and features.

Step 1:
Install Ubuntu 18.04 Operating System

There are lots of VPS providers in the market today and you should create an account with them. Most of them have a nice dashboard that provides an interface for firing up new servers or instances.

When creating your VPS server, you should choose Ubuntu 18.04 as the operating system. You will be asked to select the number of VCPUs that you want, RAM, hard disc space and server location. All these depend on your budget and preferences.

Step 2:
Connect to your server via SSH

All VPS plans allow SSH access. You should therefore connect to your Ubuntu 18.04 server with your public IP address that was allocated to you when you created the server. You should also supply the root user and password assigned during the setup process.

If you are running Windows as your local computer, consider downloading PuTTY SSH client. You can also initiate an SSH connection from Mac or Linux using the inbuilt SSH client Terminal

Step 3:
Install Nginx server

First update the package information using the command below:

$ sudo apt-get update

Then install the Nginx server

$ sudo apt install nginx

When you get a prompt to continue, press and hit Enter.

You can test if the installation was completed by typing your server’s public IP address on a browser. You should get the below default web page.

This confirms that Nginx was successfully installed and working as expected.

Step 4:
Install MySQL

Dynamic websites need MySQL for storing and managing data. We can install this relational database management system by running the command below:

$ sudo apt install mysql-server

A basic MySQL installation is not secure by default. Luckily there is a utility to configure most of the settings to make MySQL more secure.

To do this, run the command below:

$ sudo mysql_secure_installation

Then, supply the below answers on the prompts:

  • Enable validate password Plugin? Y
  • Password Validation Policy Level: 2
  • Root Password: Enter password here
  • Re-enter password: Repeat password here
  • Continue with the Password provided? Y
  • Remove anonymous users? Y
  • Disallow root login remotely? Y
  • Remove test database and access to it? Y
  • Reload privileges tables now? Y

You should get a ‘’Success! All done’’ message

Step 5:
Install PHP

Up to this point, you have a working Nginx web server and MySQL can store data for your website. However, we still need to install PHP in order for it to retrieve dynamic data from the MySQL database.

Nginx connects to PHP using FastCGI Process Manager (FPM) which works well with busier sites.

To install PHP FPM, run the command below. We have also need to include MySQL module to ensure that PHP will offer full support for MySQL server:

$ sudo apt-get install php-fpm php-mysql

Step 6:
Configure Nginx to support PHP

We need to tell Nginx to redirect PHP requests to the FPM to serve dynamic content. So we have to edit the ‘/etc/nginx/sites-available/default’ file

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

The configuration file should like this when edited. The values that you need to edit are highlighted in red:

server {
    listen 80;

    root /var/www/html;

    server_name _;

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

    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }

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

Reload Nginx

$ sudo systemctl reload nginx

Step 7:
Testing PHP

In order to test if PHP is working, we need to create a file using nano editor:

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

Once the editor opens, copy paste the text below:

<?php
phpinfo();
?>

Once you have finished copying the content, close the file by pressing CTRL + X, Y and Enter

Then, you can visit the URL http://your_ip_address/phpinfo.php

If everything works as expected, you should see a long PHP page with a lot of information.

Conclusion

In this guide, we have showed you how to install Linux, Nginx, MySQL and PHP on your Ubuntu 18.04 server. With the setup on your server, you can run any dynamic website as required. You may also install any content management system (e.g. WordPress) that relies on the LEMP stack to function. Finally, work your way towards building that beautiful website for your visitors.

Check out these top 3 VPS services:

Kamatera
$4.00 /mo
Starting price
Visit Kamatera
Rating based on expert review
  • User Friendly
    3.5
  • Support
    3.0
  • Features
    3.9
  • Reliability
    4.0
  • Pricing
    4.3
Hostinger
$2.99 /mo
Starting price
Visit Hostinger
Rating based on expert review
  • User Friendly
    4.7
  • Support
    4.7
  • Features
    4.8
  • Reliability
    4.8
  • Pricing
    4.7
Webdock
$1.05 /mo
Starting price
Visit Webdock
Rating based on expert review
  • User Friendly
    3.8
  • Support
    4.5
  • Features
    4.5
  • Reliability
    4.3
  • Pricing
    4.3

How to Install MYSQL 8.0 and Create a Database on an Ubuntu 18.04 Linux VPS

This article shows how to install MySQL 8.0 on an Ubuntu 18.04 VPS. In addition,
less than a minute
Michael Levanduski
Michael Levanduski
Expert Hosting Writer & Tester

How To Configure Nginx to use Self-Signed SSL/TLS Certificate on Ubuntu 18.04 VPS or Dedicated Server

This tutorial will help you create a self-signed SSL certificate and configure N
less than a minute
David Malcom
David Malcom
Author

How To Install Apache, MySQL & PHP on an Ubuntu 18.04 VPS or Dedicated Server

This article shows users how to setup PHP, Apache web server and MySQL database
less than a minute
Idan Cohen
Idan Cohen
Marketing Expert

How to Install PhpMyAdmin on a Ubuntu 18.04 VPS or Dedicated Server

This article will describe the process of installing phpMyAdmin on an Ubuntu 18.
less than a minute
Eliran Ouzan
Eliran Ouzan
Web Designer & Hosting Expert
HostAdvice.com provides professional web hosting reviews fully independent of any other entity. Our reviews are unbiased, honest, and apply the same evaluation standards to all those reviewed. While monetary compensation is received from a few of the companies listed on this site, compensation of services and products have no influence on the direction or conclusions of our reviews. Nor does the compensation influence our rankings for certain host companies. This compensation covers account purchasing costs, testing costs and royalties paid to reviewers.
Click to go to the top of the page
Go To Top