Official Power Up Hosting Blog

Everything about Linux, Windows, and hosting ;)

Selvakumar
Author

I am an Online Marketer and technology lover. I like to learn new things and share that with people.

Share


Our Newsletter


Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

Tags


Twitter


Official Power Up Hosting Blog

How To Install Nginx on Ubuntu along with MySQL, PHP (LEMP)

SelvakumarSelvakumar

Introduction

LEMP is the short form of Linux, Nginx, MySQL and PHP. Here, we will use Ubuntu one of the Linux Distribution.

Nginx is the modern web server which has gained the popularity within the short time of period due to its ability to handling the heavy traffic.

Here in this article, we are going to see, how to install the LEMP stack on Ubuntu 14.04 Operating system.

Prerequisites

  • You will require an Ubuntu 14.04 server with sudo non-root user.
  • Login to the user as sudo non-root user and then follow the steps.

Installing the Nginx Web Server

In order for a server to serve a website, we have to install the web server software to make it serve the requests.

There are many web servers available in the market. In that Nginx is one of the most powerful and most used web servers.

We can install the Nginx web server from the Ubuntu default repository.

First, Update the Package index using the below command.

sudo apt-get update

Then, use the below command to install the Nginx web server.

sudo apt-get install nginx

After the installation, the Nginx will start working immediately.

You can check that by accessing the domain name or IP address of the server.

If you don't have a domain and don't know the IP address, use the below command to find the IP address.

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

The command will Print two IP address of the server.

Anyone will work. Find the working IP address by checking both of them.

You can also ping an external source to tell your server IP address.

curl http://icanhazip.com

The above command will return your IP address.

Enter the IP address in the following format in the browser.

http://server_domain_name_or_IP

You can see the Nginx page saying

Welcome to Nginx!

If you can see that page, you have installed the Nginx successfully.

Your next task is to install MySQL.

Install MySQL

MySQL is essential for the Dynamic sites. It is the Database Management System which will help in storing, manipulating and retrieving data.

To install MySQL, use the below command.

sudo apt-get install mysql-server

You will be asked to enter a root password for MySQL. You will use this root password to login to the MySQL.

Make sure to set up the strong password and remember it.

The MySQL has to generate the directory structure which is required to store the database and corresponding information.

sudo mysql_install_db

Once the installation process is completed, you have to secure the MySQL.

Execute the below command for that.

sudo mysql_secure_installation

You will be asked few questions which will result in securing the MySQL server.

You should enter Yes for Removing the Test database and user in MySQL.

Check all the prompts and answer accordingly.

Install PHP

We have installed the MySQL. The next thing is that we have to install the PHP.

Nginx does not support the PHP directly. Instead, we can use php5-fpm.

This is called FastCGI Process Manager. It will handle all the PHP requests coming to the Nginx.

We have to install one more package which will make the PHP to communicate with the database.

You can do both tasks by executing the below command.

sudo apt-get install php5-fpm php5-mysql
PHP Processor Configuration

Even though we have installed our PHP components, still we need to make some changes in the configuration.

First, open the php5-fpm main configuration file.

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

You have to find the following line in the configuration file.

cgi.fix_pathinfo

comment out that by removing;. The pathinfo is set to 1 by default. This is insecure settings it will make people run the PHP script and exploit the vulnerability.

We will set it to pathinfo=0;

cgi.fix_pathinfo=0

Save and close the file.

Then restart the PHP processor using the below command.

sudo service php5-fpm restart

This will make the changes to work.

Configure Nginx to Use the PHP Processor.

We have to make the Nginx use the PHP processor for dynamic content.

We have to make changes in the server block to do that.

Open the Nginx server block configuration.

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

The server block will look like below one.

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;
}
}

The index.php should be the first value to the Index directive.

It will make the Nginx serve the index file if any directory is requested.

Make the change to the server name to make the directive to point the server domain name or Public IP address.

The configuration file has few lines commented out. Those have error handling functionality.

Uncomment those line to include those functions.

We also need to uncomment another section. Also, you should add try files to make sure that the Nginx does not pass any bad request to our PHP processor.

Make changes in the file as mentioned 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;
}
}

Once you made the changes save and close the file.

Restart the Nginx server.

sudo service nginx restart

Test the Configuration using PHP File

You should check whether the .php file is handover to the PHP processor by Nginx. For that, we will create a PHP file called info.php.

It should be created inside the document root.

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

Add the below code to that file.

<?php
phpinfo();
?>

This script will return the information about the server.

Save and close the file. Now you can visit this file by entering https://www.example.com/info.php.

You can see the PHP page with the server information.

If you see this page, then it means the Nginx is working well with the PHP processor.

After that, you should remove this file.

Because it can reveal the sensitive information about the server.

Use the below command to remove that file.

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

Conclusion

Now, you have successfully installed the LEMP stack on your server.

If you have any doubt, just leave them in the comment.

Also, don't forget to signup for the upcoming tutorial.

Thank You!

Selvakumar
Author

Selvakumar

I am an Online Marketer and technology lover. I like to learn new things and share that with people.

Comments