Atlantic.Net Blog

How to Install LEMP (Linux, Nginx, MySQL, PHP) on a Ubuntu 14.04 LTS Cloud Server

NGINX Car by Walker Cahall NGINX Car by Walker Cahall

Verified and Tested 07/3/15

Introduction

This how-to will show you how to install LEMP on a Ubuntu 14.04 cloud server. LEMP is a web service stack that consists of a Linux operating system, Nginx (pronounced “engine-x”), MySQL, and PHP. The main difference between LAMP and LEMP is that LAMP uses Apache, and LEMP uses Nginx. LEMP has been gaining popularity in the last few years because it excels in speed and scalability.

Installing LEMP on a Ubuntu 14.04  Cloud Server

First we want to make sure that your server is up to date by running the commands:

sudo apt-get update
sudo apt-get upgrade

Note: Depending on your installation you may need to remove apache2. You can do that by running the commands:

sudo apt-get remove apache2*

Followed by:

sudo apt-get autoremove

 

Installing Nginx on Ubuntu 14.04

To install Nginx, use the command:

sudo apt-get install nginx

When it asks “Do you want to continue?”, hit Enter.

Start the Nginx service with the following command:

sudo service nginx start

We can now test Nginx by going to your hostname or IP address in your browser’s address bar. If you do not know your server’s IP address, you can run the following command:

ifconfig

You should get a result similar to the image below.

An example of ifconfig showing the IP address of 192.168.0.192

An example of ifconfig showing the IP address of 192.168.0.192

In our example, 192.168.0.192 is the IP address. So in our browser we would go to http://192.168.0.192.

You should see a web page that looks like the image below.

This example is the default nginx web page on Ubuntu 14.04

This example is the default nginx web page on Ubuntu 14.04

Now that Nginx is installed, we can move on to installing MySQL.

Installing MySQL on Ubuntu 14.04

Install MySQL with the command:

sudo apt-get install mysql-server

When it asks “Do you want to continue?”, hit Enter.

Shortly after, a screen similar to the image below will appear.  You need enter a password for your MySQL root user. It should be a strong password.

Insert your secure password for your new MySQL root password Insert your secure password for your new MySQL root password

Hit enter to continue. Once you have hit enter, a new screen will appear prompting you to re-enter the password you just picked.

Retype your MySQL password

Retype your MySQL password

Now that MySQL is installed we need to do the MySQL secure installation by running the command:

sudo mysql_secure_installation

Enter your MySQL root password. When it asks “Change the root password?”, type “N” followed by Enter. The rest of the questions are up to you. For standard installations, you can hit Enter for the defaults.

An example of the MySQL secure installation

An example of the MySQL secure installation

Now that MySQL is installed, we can now install PHP.

Installing PHP on Ubuntu 14.04

Install PHP with the following command:

sudo apt-get install php5 php5-fpm php5-mysql

When it asks “Do you want to continue?”, hit Enter.

For Nginx to work with PHP correctly, we need to edit an Nginx configuration file. In this how-to, we are going to use a simple Nginx config file.

First, we need to move the original configuration file to a new file name. Run the command:

sudo mv /etc/nginx/sites-available/default /etc/nginx/sites-available/default.old

Using a text editor of your choice, we are going to make a file called default in /etc/nginx/sites-available. For nano use the command:

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

Copy the following into your text editor:

server {
        listen       80;
        server_name  your_site_name.com;
        root /usr/share/nginx/html;
        index index.php index.html;

        location / {
                try_files $uri $uri/ =404;
        }

        error_page 404 /404.html;
        error_page 500 502 503 504 /50x.html;

        location = /50x.html {
                root /var/www/html;
        }

        location ~ \.php$ {
                try_files $uri =404;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
        }
}

In nano, to exit and save, hit Ctrl+x , type “y”, and then Enter.

Since we made changes to the configuration file, we need to restart Nginx, by running the command:


sudo service nginx restart

We are now going to make a simple PHP page to test.

Using a text editor of your choice, we are going create a file called info.php in /usr/share/nginx/html/.

sudo nano /usr/share/nginx/html/info.php

Copy the following into your text editor.

<?php
phpinfo();
?>

In your browser, you can go to http://Your-Hostname/info.php or http://Your-IP-Address/info.php. As above, in this example, we would use http://192.168.0.192/info.php.

You should see a web page similar to the one below.

An example of the info.php web page An example of the info.php web page

Once you are done testing, it is a good idea to remove the info.php file, since it may give a potential attacker information that can be used to craft a specific attack against your server. To do that run the command:

sudo rm /usr/share/nginx/html/info.php

Congratulations, you have installed LEMP on Ubuntu 14.04. Thank you for following this how-to. Please check back for more updates, to purchase a cost-effective virtual private server, or take a look at our how-to on Installing WordPress on Ubuntu 14.04!

Get a $250 Credit and Access to Our Free Tier!

Free Tier includes:
G3.2GB Cloud VPS a Free to Use for One Year
50 GB of Block Storage Free to Use for One Year
50 GB of Snapshots Free to Use for One Year