Installing nginx, MySQL, PHP on Ubuntu 14.04

Posted By : Ravi Verma | 26-May-2015
Installing nginx, MySQL, PHP on Ubuntu 14.04

Step 1: Install the Nginx Web Server

		sudo apt-get update
		sudo apt-get install nginx
    

Step 2: Install MySQL to Manage Data

		sudo apt-get install mysql-server
    

Administrative (root) password will be asked to get into the MySQL system.

Your MySQL would be installed successfully and now you will need to set some configuration for it.

First, MySQL will be needed to generate the directory structure to store its databases and information. By giving following command:

		sudo mysql_install_db
    

Next, to run a simple security script it will ask you to modify some insecure defaults. so here is the command for it:

		sudo mysql_secure_installation
    

then it will ask you to enter the MySQL root password that you have selected during installation.

it will ask if you want to change that password. You can type "N" to change the password after that hit "ENTER". And you will be prompted to remove some test users and databases. You can press "ENTER" to remove the unsafe default settings.

Step 3: Install PHP for Processing

Nginx does not have native PHP processing like some web servers, so we would need to install php5-fpm, which extends as "fastCGI process manager". With some scripts in nginx configuration file we can tell nginx to pass PHP requests to PHP software for processing.

With php5-fpm module, we can also install an additional helper package which let PHP to communicate with backend database. This installation will get in the necessary PHP core files.by following the command below:

		sudo apt-get install php5-fpm php5-mysql
    

step 4: Configure the PHP Processor

After getting PHP components installed, we need to change some PHP configuration settings to get our steps more secured.

So now open the php.ini file with root permission, with the command below:

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

find "cgi.fix_pathinfo".

default - ;cgi.fix_pathinfo=1

This setting is not secure as it informs PHP to execute the nearest file if a PHP file is not matching exactly. This would authorize users to craft PHP requests in a way that would let them to execute scripts that they shouldn't be permitted to execute.

we need to change it by making it uncommented and setting it to "0" like this:

		cgi.fix_pathinfo=0
    

Save and close the file.

As configuration setting has changed, so we need to restart our PHP processor and follow the command:

		sudo service php5-fpm restart
    

Step 5: Configure Nginx to Use our PHP Processor

We have installed all required components. The last configuration which is to tell Nginx web server to use PHP processor for dynamic content.

For this, Open Nginx server (default) block configuration file and follow the given command:

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

Currently, with the comments removed, the Nginx default server block file looks like this:

		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 need to make some changes to this file.

  1. Firstly we need to add an index.php to index directive to allow php index files which will be served when a directory is requested. note : if you are going to set more than one files to be served as index file then focus on the sequence. nginx will serve the file which comes first in the sequence, if it does not find that file then it will serve the next file.
  2. To point our server's domain name or public Ip address,we need to modify the server_name directive
  3. The actual configuration files have some commented line which define error processing routines so they may have to be uncommented to include that functionality.
  4. For the actual PHP processing, we need to get uncomment a portion of a section. We can also add try_files directive to make sure that Nginx doesn't pass bad requests to our PHP processor.
		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 making above changes, save and close the file.

Restart Nginx to bring changes into effec, and here is the command:

		sudo service nginx restart
    

Step 6: Now create a PHP File to Test Configuration

Create a file test.php in "/usr/share/nginx/html" location and follow the code below:

		
    

and now visit http://server_domain_name_or_IP/test.php

you will see an server information page generated by PHP.

If everything goes well then you can delete test.php

Hope you find this helpful

About Author

Author Image
Ravi Verma

Ravi is a seasoned technologist with excellent experience in AngularJS , NodeJS and MEAN stack. He has good experience in developing complex UIs for web and mobile applications.

Request for Proposal

Name is required

Comment is required

Sending message..