How To Install LEMP (Linux, nginx, MySQL, PHP) on Ubuntu 15.04

Introduction

The LEMP (Linux, nginx, MySQL, PHP) stack is a group of software that can be used to serve dynamic web pages and web applications. This is an acronym that describes a Linux operating system, with an Nginx web server. The backend data is stored in MySQL and the dynamic processing is handled by PHP.

In this guide, we will demonstrate how to install a LEMP stack on an Ubuntu 15.04 server. The Ubuntu operating system takes care of the first requirement. We will describe how to get the rest of the components up and running.

1. Install the Nginx Web Server

Install the Nginx web server using these commands
sudo apt-get update
sudo apt-get install nginx

 

2. Install MySQL

Install MySQL database management system using this command

sudo apt-get install mysql-server

You will be asked to supply a root (administrative) password for use within the MySQL system.

Now, we need to tell MySQL to generate the directory structure it needs to store its databases and information. We can do this by typing this command:
sudo mysql_install_db

Next, you’ll want to run a simple security script that will prompt you to modify some insecure defaults. Begin the script by typing command:
sudo mysql_secure_installation

You will need to enter the MySQL root password that you selected during installation.

Next, it will ask if you want to change that password. If you are happy with your MySQL root password, type “N” for no and hit “ENTER”. Afterwards, you will be prompted to remove some test users and databases. You should just hit “ENTER” through these prompts to remove the unsafe default settings.

3. Install PHP

Since Nginx does not contain native PHP processing like some other web servers, we will need to installphp5-fpm, which stands for “fastCGI process manager”. We will tell Nginx to pass PHP requests to this software for processing. We can install this module and will also grab an additional helper package that will allow PHP to communicate with our database backend. The installation will pull in the necessary PHP core files. Do this by typing command:
sudo apt-get install php5-fpm php5-mysql

Configure the PHP Processor

We now have our PHP components installed, but we need to make a slight configuration change to make our setup more secure. Open the main php5-fpm configuration file with root privileges:

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

What we are looking for in this file is the parameter that sets cgi.fix_pathinfo. This will be commented out with a semi-colon (;) and set to “1” by default.

This is an extremely insecure setting because it tells PHP to attempt to execute the closest file it can find if a PHP file does not match exactly. This basically would allow users to craft PHP requests in a way that would allow them to execute scripts that they shouldn’t be allowed to execute.

We will change both of these conditions by uncommenting the line and setting it to “0” like this:
cgi.fix_pathinfo=0

Save and close the file when you are finished.

Now, we just need to restart our PHP processor by typing:
sudo service php5-fpm restart

4. Configure Nginx Server to Use our PHP Processor

Now, we have all of the required components installed. The only configuration change we still need to do is tell Nginx to use our PHP processor for dynamic content.

We 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 typing command:

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

The changed file will look like the 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;
    }
}

Restart Nginx to make the necessary changes using this command:

sudo service nginx restart

2 Comments

  • antonio January 29, 2016 Reply

    Thanks for one’s marvelous posting! I definitely enjoyed
    reading it, you could be a great author. I will remember to bookmark your blog and will often come back down the
    road. I want to encourage one to continue your great writing, have a nice day!

  • JonasYLavgle July 15, 2016 Reply

    I blog quite often and I genuinely thank you for your content.
    Your article has really peaked my interest. I am going to book mark
    your website and keep checking for new details about once per week.
    I opted in for your RSS feed as well.

Leave a Reply