how to install nginx server in ubuntu

How to Install nginx server in ubuntu ? it  is really simple to do following steps will guide you the whole process in the end you will running nginx server.This tutorials are tested on Digital Oceans VPS  10$pm (recommended) but it will work on all . Learning practically is the best learning method i have ever encountered, so in my advice kindly do the tutorials as you read it this while help to understand easily.

Tools Needed

Putty (What is Putty more Info)

A VPS Subscription

Method

Note : Follow me exactly better if you copy codes to avoid errors 

1) I am assuming you have logged in using putty to VPs account

2) To Update the system type 

sudo apt-get update

3) Good you have updated you system this will download all the installed applications latest version

4) Now you have to install MySQL free data management tools for managing database

To install MySQL  type in putty terminal 

sudo apt-get install mysql-server libapache2-mod-auth-mysql php5-mysql

it will ask for password enter accordingly

mysqlpassword

mysqlpassword

Now to invoke MySQL enter the following command

sudo mysql_install_db
followed by 
sudo /usr/bin/mysql_secure_installation

It will ask for password enter which you had entered while installation.

repeatmysqlpassword

 

Enter the root password

Now the terminal will ask if you want to change the root password , after this there are series of questions asked select yes for all

mysqlyess

mysqlyess

Congratulation you have installed MySQL

5) Lets Install Nginx  copy pate the following commands  in you terminal.

sudo apt-get install nginx
start it 
sudo service nginx start

To get your IP address for VPS  type this 

ifconfig eth0 | grep inet | awk '{ print $2 }'

Good work now check your IP in your browser It should display “ Welcome to nginx!”

 

welcomnignx

You successful installed Nginx

6) Now Lets Install PHP copy pate the following commands  in you terminal.

sudo apt-get install php5-fpm

Configure PHP-FPM

Open php.ini file with this command 

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

find this line  with search press Ctrl(Left)+W 

cgi.fix_pathinfo=1,
Replace 
cgi.fix_pathinfo=0,
just replace 1 with 0

nginx

nginx

Save the change Ctrl(Left)+O then Enter

Open php5-fpm file with this command 

sudo nano /etc/php5/fpm/pool.d/www.conf

Search this line 

 listen = 127.0.0.1:9000
replace it with 
listen = /var/run/php5-fpm.sock

replaceshock

Save the change Ctrl(Left)+O then Enter

Restart PHP 

sudo service php5-fpm restart

7) Configure nginx(

Open nginx config file with this command  

sudo nano /etc/nginx/sites-available/default
  It will look like this
server {
        #listen   80; ## listen for ipv4; this line is default and implied
        #listen   [::]:80 default ipv6only=on; ## listen for ipv6

        root /usr/share/nginx/www;
        index index.html index.htm;

        # Make site accessible from http://localhost/
        server_name localhost;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to index.html
                try_files $uri $uri/ /index.html;
                # Uncomment to enable naxsi on this location
                # include /etc/nginx/naxsi.rules
        }

delete till only the above codes  only  and copy exact code below 

server {
        listen   80;
     

        root /usr/share/nginx/www;
        index index.php index.html index.htm;

        server_name example.com;

        location / {
                try_files $uri $uri/ /index.html;
        }

        error_page 404 /404.html;

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
              root /usr/share/nginx/www;
        }

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        location ~ \.php$ {
                try_files $uri =404;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
                
        }

}

Change in new code ( Just for information DON’T CHANGE AS THE WORKING FORMAT IS ALREADY GIVEN TO PASTE)

Add index.php  its should like index.php  index. html  index.htm

Replace the example.com to your IP or Domain 

Replace  location ~ \.php$ {   to  “location ~ \.php$ {“ section

Save the change Ctrl(Left)+O then Enter

8) Make a PHP Info page

Type the following command to create  page

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

add code to print info  

<?php
phpinfo();
?>

Save the change Ctrl(Left)+O then Enter

restart your server 

sudo service nginx restart

Type your IP or domain it should display like this image

nignx phpinfo

nignx phpinfo

Congraulations you will have a working VPS

This is referral link for Digital Ocean      Source: Digital Ocean

About Nishil Prasad

Nishil is a Blogger, Online Entrepreneur, Wordpress Consultant, SEO Expert love to explore web everyday. In this ride to explore web he share his experiences on various platforms. Spreading Knowledge.

Subscribe Us

Subscribe to our e-mail newsletter to receive updates.

, , , , , , , ,

2 Responses to how to install nginx server in ubuntu

  1. xami February 4, 2014 at 12:20 am #

    not working showing error

    sudo service nginx start
    Starting nginx: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
    nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
    nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
    nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
    nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
    nginx: [emerg] still could not bind()
    nginx.

    • Nishil Prasad April 6, 2014 at 4:12 am #

      Hi Xami sorry for replying late, Can you address the last step you have taken