1. Home
  2. Tutorials
  3. How To Install Linux, Nginx, MySQL, PHP (LEMP) stack on Ubuntu 14.04

How To Install Linux, Nginx, MySQL, PHP (LEMP) stack on Ubuntu 14.04

How To Install Linux, Nginx, MySQL, PHP (LEMP) stack on Ubuntu 14.04
How To Install Linux, Nginx, MySQL, PHP (LEMP) stack on Ubuntu 14.04


The LEMP software stack is a group of softwares, which are used for serving dynamic web pages and web applications. That’s an acronym which describes a Linux operating system, along with a Nginx web server. MySQL stores the backend data, then PHP handles the dynamic processing.

In this tutorial we’ll be showing how you can install a LEMP stack for your Ubuntu 14.04 server. Ubuntu already takes care of the first requirement. We will explain how you can get the other components up and running.


Before you start with this guide, you will need to have a non-root user account on your server with sudo privileges.

After you’ve got your account available, enter into your server using that username and you should then be ready to start following the steps below.

1. Install the Nginx Web Server

To be able to present web pages to our site visitors, we’ll have to employ Nginx, a modern, efficient web server.

Every software we are going to be getting for this process will come directly from Ubuntu’s default package repositories. Which means we may use the apt package management suite for completing the installation.

Because this is our first time using apt for this session, we will begin by updating our local package index and afterwards we can install the server:

sudo apt-get update
sudo apt-get install nginx

When in Ubuntu 14.04, Nginx is on default configured to begin running after installation.

You could test if the server is up and running by using your server’s domain name or public IP address in your web browser.

In case you haven’t gotten a domain name pointed at your server yet and you have no knowledge of your server’s public IP address, you could find it by entering one of the following into your terminal:

ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'


Or you can try entering:

curl http://icanhazip.com

Use one of these lines that you’ve received in your web browser and then it will take you to Nginx’s default landing page:


If you see the message “Welcome to nginx!” it means you’ve installed nginx successfully.

2. Install MySQL to Manage Site Data

After we’ve acquired a web server, we have to install MySQL, a database management system, for storing and managing the data on our site.

You could install this by simply entering the following command:

sudo apt-get install mysql-server

You are going to be prompted to enter a root (administrative) password for use inside the MySQL system.

Now the MySQL database should be installed, however the configuration is not really ready yet.

To begin, we have to tell MySQL to generate the directory structure it requires to store its databases and information. We could do It by entering the following:

sudo mysql_install_db

Afterwards, you’ll have to run a simple security script which will ask you to edit certain insecure defaults.

Start the script by entering:

sudo mysql_secure_installation

You will have to type the MySQL root password that you’ve chosen while installing.

Then, it should ask if you’d like to change that password. In case you’re satisfied with your MySQL root password, enter “N” for no and then hit “ENTER”. Afterwards, you should be prompted to delete some test users and databases. You just need to hit “ENTER” through those prompts to delete the unsafe default settings.

After the script has been run, MySQL is prepared to go.

3. Install PHP for Processing

Since we’ve got Nginx installed it can now start serving our pages and MySQL is installed so it can store and manage our data, however we still require something to connect those two pieces and to generate dynamic content. We may use PHP for this.

Because Nginx won’t have any native PHP processing like certain web servers, we will have to install php5-fpm, this stands for “fastCGI process manager”. We’ll need to tell Nginx to pass PHP requests to this software for processing.

We could install this module and also grab an extra helper package which should allow PHP to communicate with our database backend. This installation should pull in the needed PHP core files. Do this by entering the following command:

sudo apt-get install php5-fpm php5-mysql

Configure the PHP Processor

Now we’ve got our PHP components installed, though we still have to make a slight configuration change for making our setup more secure.

Next will open the main php5-fpm configuration file with root privileges:

sudo nano /etc/php5/fpm/php.ini

The line we are going to search for in this file is cgi.fix_pathinfo. By default this will be set to 1 and commented out with a semi-colon (;).

This is a very insecure setting since it tells PHP to try to execute the closest file it can find if a PHP file will not match exactly. That will basically grant users the ability to craft PHP requests in a way that would allow them to run scripts that they aren’t supposed to be allowed to run.

We need to change both of these conditions by uncommenting the line and changing it to “0” like shown below:


Save and close the file after you are done.

Then, we just have to restart our PHP processor by entering:

sudo service php5-fpm restart

This should cause for the changes to take effect.

4. Configure Nginx to Use our PHP Processor

Since we’ve got all of the needed components installed. The only change we will still need to do in the configuration is telling Nginx to use our PHP processor for dynamic content.

We can do this on the server block level (server blocks are similar to Apache’s virtual hosts).

Open the default Nginx server block configuration file by entering the following:

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

Right now, with the comments deleted, the Nginx default server block file will look like the below:

server {

    listen 80 default_server;

    listen [::]:80 default_server ipv6only=on;

    root /usr/share/nginx/html;

    index index.html index.htm;

    server_name localhost;

    location / {

        try_files $uri $uri/ =404;



We will now have to do some changes to this file for our site.

To start, we have to add an index.php option as the first value of our index directive to allow PHP index files to be served after a directory is requested.

We will have to edit the server_name directive so it points to the server’s domain name or public IP address.

The actual configuration file has a couple of lines commented out which define error processing routines. We have to uncomment these so it has that functionality.

The changes you’ll have to do are going to be in red text below:

server {

listen 80 default_server;

listen [::]:80 default_server ipv6only=on;


root /usr/share/nginx/html;

index index.php index.html index.htm;


server_name server_domain_name_or_IP;


location / {

try_files $uri $uri/ =404;



    error_page 404 /404.html;

    error_page 500 502 503 504 /50x.html;

    location = /50x.html {

        root /usr/share/nginx/html;



    location ~ \.php$ {

        try_files $uri =404;

        fastcgi_split_path_info ^(.+\.php)(/.+)$;

        fastcgi_pass unix:/var/run/php5-fpm.sock;

        fastcgi_index index.php;

        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

        include fastcgi_params;



After you’ve done the changes above, you may save and close the file.

Restart Nginx for the changes to take effect:

sudo service nginx restart

5. Create a PHP File to Test Configuration

Your LEMP stack will now be completely set up. We still have to test so we ensure Nginx is able to correctly hand .php files off to our PHP processor.

We could do this by making a test PHP file in our document root. Open a new file named info.php inside your document root in your text editor:

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

We could enter this into the new file. This is a valid PHP code which should return formatted information about our server:




After you’ve finished, save and close the file.

Now you may visit this page in your web browser by pointing it to your server’s domain name or public IP address followed by /info.php:


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

If you see a page which looks like the above, it means you’ve set up PHP processing with Nginx successfully.

Once you test this, it’s probably better if you delete the file you’ve made as it might give unauthorized users certain hints about your configuration which might help them try to break in. You could always regenerate this file in case you require it at a later time.

For the time being, delete the file by entering the following command:

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


You will now have a LEMP stack configured on your Ubuntu 14.04 server. This will grant you a very flexible foundation for serving web content to your visitors.

Updated on January 3, 2018

Was this article helpful?

Related Articles

Add A Comment