Saturday, February 2, 2013

Apache Web Server - Virtual Hosting

What is mean by Virtual Hosting ?

Using Virtual Hosting concept you can host more than one websites in a single web server.This can be achieved in two ways,
1] Name-Based Virtual Hosting
2] IP-Based Virtual Hosting

For Example, Here we are trying to host two different websites www.industryvertical.co.in, www.inland.com to a single Apache Web Server.

Virtual Hosting 

What is Name-Based virtual Hosting ?

This is very much simpler one implement compared with IP-Based Virtual Hosting.Browser requests(www.industryvertical.co.in) are served by web server based on the hostname of the website.

Requirements:

1] One IP Address( One NIC Card)

2] DNS Entry (In /etc/hosts map Hostname with IP Address)

Example:

In the following example, the server contains only one NIC card, which is configured with 192.168.56.101 IP address. The DNS entry for both www.industryvertical.co.in and www.inland.com website points to 192.168.56.101 IP address. When Apache receives a request, it looks for the hostname entry in the HTTP header, and serves the corresponding website.

image

Content of /etc/hosts file after Entry:

127.0.0.1   localhost
::1         localhost
192.168.56.101 www.inland.com
192.168.56.101 www.industryvertical.co.in

Flow of Request and Response?

1] Client browser request for the website(www.industryvertical.co.in)

2] Web Server receive the request header and search for www.industryvertical.co.in hostname in it’s VirtualHost configuration  directive

3] Once it found out the relative VirtualHost directive then it looks for requested page(index.html) inside DocumentRoot folder.(/var/www/html/industryvertical)

4] Sever fetches that document and respond to the client browser request

Implementation Part:

NameVirtualHost *:80

<virtualhost  *:80>
    ServerAdmin thiru@industryvertical.co.in
    DocumentRoot "/var/www/html/industryvertical"
    ServerName industryvertical.co.in
    ServerAlias www.industryvertical.co.in
    ErrorLog "logs/industryvertical/error_log"
    CustomLog "logs/industryvertical/access_log" common
</virtualhost>


<virtualhost  *:80>
    ServerAdmin thiru@inland.com
    DocumentRoot "/var/www/html/inland"
    ServerName inland.com
    ServerAlias www.inland.com
    ErrorLog "logs/inland/error_log"
    CustomLog "logs/inland/access_log" common
</virtualhost>

What is IP Based virtual Hosting ?

This is bit complex to implement compared with Name-Based Virtual Hosting.Browser requests(www.industryvertical.co.in) are served by web server based on the IP Addresses of the website.

Requirements:

1] Two IP Address( Two NIC Card server machine)

Example:

In the following example, the server machine contains two NIC card, which is configured with 192.168.56.101, 192.168.56.102 IP address.

192.168.56.101 -- assigned to www.industryvertical.co.in

192.168.56.102 -- assigned to www.inland.com

image Implementation Part

NameVirtualHost 192.168.56.101

<virtualhost  192.168.56.101>
    ServerAdmin thiru@industryvertical.co.in
    DocumentRoot "/var/www/html/industryvertical"
    ServerName industryvertical.co.in
    ServerAlias www.industryvertical.co.in
    ErrorLog "logs/industryvertical/error_log"
    CustomLog "logs/industryvertical/access_log" common
</virtualhost>

NameVirtualHost 192.168.56.102

<virtualhost  192.168.56.102>
    ServerAdmin thiru@inland.com
    DocumentRoot "/var/www/html/inland"
    ServerName inland.com
    ServerAlias www.inland.com
    ErrorLog "logs/inland/error_log"
    CustomLog "logs/inland/access_log" common
</virtualhost>

References:

1] http://httpd.apache.org/docs/2.2/vhosts/examples.html

No comments:

Post a Comment