Apache Virtual Host Configurations – Linux
Virtual Hosting is method of hosting multiple websites on single web server, web server might be anything of Apache HTTP,Microsoft IIS, Lighttpd etc.There are two type of Virtual Hosting in web server, Name based and IP based.
With the Name based virtual hosting, we can configure multiple website on single ip address, in ip based, we can configure only one website on single ip address. Apache supports both name and ip based virtual hosting, virtual host should configured using the virtual host container, typical virtual host container look like below.
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot /www/docs/dummy-host.example.com
ServerName dummy-host.example.com
ErrorLog logs/dummy-host.example.com-error_log
CustomLog logs/dummy-host.example.com-access_log common
</VirtualHost>
Name Based Virtual Host:
Name based virtual host uses domain name requested by the client to identify the correct virtual host to serve, you need to setup the DNS server to map each hostname to the corresponding IP address and then configure the Apache Server to recognize hostname. Before hosting multiple domains, you need to setup the default virtual host. Default virtual host will serve the pages to client, who’s requested domain yet to be configured or not configured on the server (due to the wrong pointing by the DNS server). Configure the DNS server to setup the Name based virtual hosting.
NameVirtualHost www.example1.com
<VirtualHost www.example1.com:80>
ServerAdmin [email protected]
ServerName www.example1.com
DocumentRoot /var/www/example1
ErrorLog logs/example1-error_log
CustomLog logs/example1-access_log common
</VirtualHost>
<VirtualHost www.example2.com:80>
ServerAdmin [email protected]
DocumentRoot /var/www/example2
ServerName www.example2.com
ErrorLog logs/example2.com-error_log
CustomLog logs/example2.com-access_log common
</VirtualHost>
If the client requests www.example1.com or www.example2.com from web server, client will receive the home page receptively. If the client requests other than www.example1.com and www.example2.com, client will receive the home page of the default virtual host ie. www.example1.com. Name based virtual require either DNS server or host entries to verify the configuration.
IP Based Virtual Host:
IP based virtual host uses ip address requested by the client to identify the correct virtual host to serve, there fore you need to have separate ip address for each virtual host. Use ip address instead of host name in the
<VirtualHost 192.168.0.1:80>
ServerAdmin [email protected]
ServerName www.example1.com
DocumentRoot /var/www/example1
ErrorLog logs/example1-error_log
CustomLog logs/example1-access_log common
</VirtualHost>
<VirtualHost 192.168.0.2:80>
ServerAdmin root@example2.com
DocumentRoot /var/www/example2
ServerName www.example2.com
ErrorLog logs/example2.com-error_log
CustomLog logs/example2.com-access_log common
</VirtualHost>
From the above you can see that each virtual host configured with diffident ip address, you need to have multiple network card’s installed on the server. Name based virtual host is most widely used on the internet servers to serve the web content.