Virtual hosting LocalHost linux OS ubuntu 14.04

Setting virtual hosting in localhost machine, (Used Apache  linux OS ubuntu 14.04)
 

site1.com 
site2.com 

Assume that I have two sites placed in /var/www/html, site1 at  /var/www/html/site1 and site2 /var/www/html/site2

Now I have access to my site by navigating through browser http://localhost/site1 and  http://localhost/site2

Now I wish to access my two sites in my local machine by site1.com and site2.com 

 

Let see the steps 

(Paths and sites-available default.conf file may vary in different OS version of linux currently Im using Ubuntu 14.04 but the steps are same) 

First create default conf to your site conf that 

Its the configuration file will say to the server that "when the browser access this url allow them access this folder path"

By default (/var/www/html will be accessed from http://localhost)

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/site1.com.conf
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/site2.com.conf

Then edit the conf file  and change it 
 

sudo gedit /etc/apache2/sites-available/site1.com.conf

Add  it to site1.com.conf

<VirtualHost *:80>
	ServerName site1.com
    ServerAlias www.site1.com
	ServerAdmin webmaster@localhost
	DocumentRoot /var/www/html/site1
	ErrorLog ${APACHE_LOG_DIR}/error.log
	CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

sudo gedit /etc/apache2/sites-available/site2.com.conf

Add  it to site2.com.conf

<VirtualHost *:80>
	ServerName site2.com
    ServerAlias www.site2.com
	ServerAdmin webmaster@localhost
	DocumentRoot /var/www/html/site2
	ErrorLog ${APACHE_LOG_DIR}/error.log
	CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Enable the added sites

 sudo a2ensite site1.com.conf; sudo a2ensite site1.com.conf;

Restart the apache server 

sudo service apache2 restart;

Now let us tell to the server that, when this domain is called in your local browser ,don't look into web just look into to the ip I mentioned. Add that ip as local one. 

 sudo gedit /etc/hosts

 add these lines 

127.0.2.1    site1.com
127.0.2.0    site2.com

next to 

127.0.0.1    localhost localhost.localdomain yourhost
127.0.1.1    unichnul-001


Will be like 

127.0.2.1    site1.com
127.0.2.0    site2.com
127.0.0.1    localhost localhost.localdomain yourhost
127.0.1.1    unichnul-001

Save it

now access http://site1.com and http://site2.com in browser in your local machine, 

Thats it!