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

How To Install Linux, Nginx, MySQL, and 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. LEMP is 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 will teach you how to install an 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 starting with this guide, you will need to have a non-root user account on your server with sudo privileges.
After your account is available, enter into the 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 your site visitors, you must employ Nginx, a modern and efficient web server.
Every software we are going to be getting for this process will come directly from Ubuntu’s default package repositories; this means we may use the apt package management suite for completing the installation.
As this is our first time using apt for this session, we will begin by updating our local package index. Afterwards, we can install the server.

sudo apt-get update
sudo apt-get install nginx

When in Ubuntu 14.04, Nginx is defaultly configured to begin running after installation.
You can test if the server is up and running by using your server’s domain name or public IP address in your chosen web browser.
If you haven’t got a domain name for your server yet, or if you have no knowledge of your server’s public IP address, then you can 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 the lines that you’ve received in your web browser and it will take you to Nginx’s default landing page.
The message ‘Welcome to nginx!’ means you have 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, note that the configuration is not ready yet.
To begin, tell MySQL to generate the directory structure it requires to store its databases and information. This is done by entering the following.

sudo mysql_install_db

Afterwards, run a simple security script which will ask you to edit certain insecure defaults.
Start the script by entering the below.

sudo mysql_secure_installation

You will have to type the MySQL root password that you’ve chosen while installing.
It should now ask if you would like to change that password. If you are 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. 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

Now that Nginx is installed, it can now start serving your pages. MySQL is also installed so it can store and manage your data. However, we still require something to connect those two pieces and also to generate dynamic content. You can use PHP for this.
As Nginx does not have any native PHP processing like certain web servers, you will have to instal ‘php5-fpm’ which stands for ‘fastCGI process manager’. You need to tell Nginx to pass PHP requests to this software for processing.
You can install this module and also grab an extra helper package which will allow PHP to communicate with your 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 that the PHP components are installed, you still have to make a slight configuration change for making your setup more secure.
Next, open the main ‘php5-fpm’ configuration file with root privileges.

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

The line 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, it tells PHP to try executing the closest file it can find if a PHP file will not match exactly which 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.
Change both of these conditions by uncommenting the line and changing it to ‘0’ like shown below:


Save and close the file when you are done.
Then, restart your PHP processor by entering the following.

sudo service php5-fpm restart

This should cause the changes to take effect.

4. Configure Nginx to Use our PHP Processor

Now that you have all of the needed components installed, the only change needed in the configuration is to tell Nginx to use your PHP processor for dynamic content.
You 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;

Now make some changes to this file for your site.
To start, add an ‘index.php’ option as the first value of your index directive to allow PHP index files to be served when a directory is requested.
Edit the ‘server_name’ directive so that 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; uncomment these so it has that functionality.
The changes are detailed 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 made 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. However, you still have to test this to ensure Nginx is able to correctly hand .php files off to your PHP processor.
You can do this by making a test PHP file in your document root. Open a new file named ‘info.php’ inside your document root in the text editor.

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

You can enter this into the new file, this is a valid PHP code which should return formatted information about your server.


After you’ve finished, save and close the file.
Now you can 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 should 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 have set up PHP processing with Nginx successfully.
Once you test this, it is better to delete the file you have made as, otherwise, it might give unauthorized users certain hints about your configuration which could help them try to break in. You can always regenerate this file in case you require it at a later time.
For the time being, delete the file by entering the command below.

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 December 23, 2018

Was this article helpful?

Related Articles

Leave a Comment

[apsl-login-lite login_text='Please login with a social account']