Install & Add New Site Into Your Nginx-based Ubuntu (14.04) Server

step-1

Install Nginx

sudo apt-get update
sudo apt-get install nginx

Step-2

Check Nginx Status

sudo service nginx status

If you get message from your terminal that “nginx is not running” then start it by the following commend line.

sudo service nginx start

If start successfully then you will get this message from your terminal “Starting nginx: nginx.”

Step-3

Configure Server

Open up the virtual host file you need to make some change within.

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

We need to make a couple of changes in these few lines:

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.php 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.php;
                # Uncomment to enable naxsi on this location
                # include /etc/nginx/naxsi.rules
        }

        location /doc/ {
                alias /usr/share/doc/;
                autoindex on;
                allow 127.0.0.1;
                deny all;
        }

        location = /50x.html {
                root /usr/share/nginx/www;
        }

        location ~ \.php$ {
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
                fastcgi_index index.php;
                include fastcgi_params;
        }

        location ~ /\.ht {
                deny all;
        }
}

Restart nginx and make the changes visible.

sudo service nginx restart

Step-4

Goto your browser in address bar type “localhost” and hit enter. If you get welcome message then nginx will work successfully.

Step-5

Install phpmyadmin

sudo apt-get install phpmyadmin

we simply need to copy the phpmyadmin into a new custom directory’

sudo ln -s /usr/share/phpmyadmin /usr/share/nginx/www
sudo php5enmod mcrypt

Restart php5-fpm and make the changes visible.

sudo service php5-fpm restart

Step-6

Goto your browser in address bar type “localhost/phpmyadmin” and hit enter. If you get phpmysql interface then phpmysql will work successfully.

Step-7

Site Create

We need to create a new file called index.html within the directory.
Here “your-pc-name” replace with your pc name.

mkdir -p /home/your-pc-name/www/example.com/public_html
sudo nano /home/your-pc-name/www/example.com/public_html/index.html

We can add some text to the file so we will have something to look at when the the site redirects to the virtual host.

<html>
  <head>
    <title>www.example.com</title>
  </head>
  <body>
    <h1>Success: You Have Set Up a Virtual Host</h1>
  </body>
</html>

we simply need to copy the default file text into a new custom example.com file:

sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/example.com

Open up the virtual example.com file you need to make some change within.

sudo nano /etc/nginx/sites-available/example.com

We need to make a couple of changes in these few lines:

server {
	listen 80; ## listen for ipv4; this line is default and implied

	#here <strong>"your-pc-name"</strong> replace with your pc name
	root /home/your-pc-name/www/example.com/public_html;

	index index.php index.html index.htm;

	# Make site accessible from http://localhost/
	server_name example.com;

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

		# include /etc/nginx/naxsi.rules
	}

	location /doc/ {
		alias /usr/share/doc/;
		autoindex on;
		allow 127.0.0.1;
		deny all;
	}

	location = /50x.html {
		root /usr/share/nginx/www;
	}

	location ~ \.php$ {
		fastcgi_split_path_info ^(.+\.php)(/.+)$;
		fastcgi_pass 127.0.0.1:9000;
		fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
		fastcgi_index index.php;
		include fastcgi_params;
	}

	location ~ /\.ht {
		deny all;
	}
}

we simply need to copy the example.com text from sites-abailable directory to sites-enabled directory

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/example.com

open up your hosts file.

sudo nano /etc/hosts

You can add the local hosts details to this file, as seen in the example below. As long as that line is there, directing your browser toward, say, example.com will give you all the virtual host details for the corresponding IP address.

127.0.0.1 localhost
127.0.0.1 example.com

Restart nginx and make the changes visible.

sudo service nginx restart

Now go to your browser address bar and type example.com and hit enter. You will get your index.html file interface.