Virtual Host configuration

The term Virtual Host refers to the practice of running more than one web site (such as site1.mysite.com and site1.mysite.com) on a single machine. Virtual hosts can be "IP-based", meaning that you have a different IP address for every web site, or "name-based", meaning that you have multiple names running on each IP address. The fact that they are running on the same physical server is not apparent to the end user.


Use Case: I have a static IP address but no registered domain attached to this IP. I wanted to run some test sites, so this this is what I did:

1. Go to a site like freedns.afraid.org which provides free DNS subdomain hosting. They also provide a range of interesting domain names on which I can create my own subdomains.


2. At this site I made these links

site1.mysite.com to my IP address 999.999.999.999
site2.mysite.com to my IP address 999.999.999.999

3. In httpd configuration file etc/httpd/conf/httpd.conf I added the following directives

	Listen 80
	NameVirtualHost *:80

	# I add this dummy entry first, which will redirect users 
	# who enter URL 999.999.999.999 to DocumentRoot /var/www/default	  	
	
	<VirtualHost *:80>  
	  ServerName whatever.something.com
	  DocumentRoot /var/www/default	  
	</VirtualHost> 

	# route site1.mysite.com to /var/www/content1
	<VirtualHost *:80>  
	  ServerName site1.mysite.com
	  DocumentRoot /var/www/content1
	  ServerAlias www.site1.mysite.com  
	</VirtualHost> 

	# route site2.mysite.com to /var/www/content2
	<VirtualHost *:80> 
	  ServerName site2.mysite.com
	  DocumentRoot /var/www/content2
	  ServerAlias www.site2.mysite.com
	</VirtualHost> 

4. After re-booting Apache server, I can now access pages located at /var/www/content1 using URL site1.mysite.com


Some Apache Directives

NameVirtualHost - a directive used in name-based virtual hosting used to designate the IP address (and possibly port) on the server that will be accepting requests for the hosts. In case where any and all IP addresses on the server should be used, you can use * as the argument to NameVirtualHost. If you're planning to use multiple ports (e.g. running SSL) you should add a Port to the argument, such as *:80. Note that mentioning an IP address in a Name VirtualHost directive does not automatically make the server listen to that IP address.

VirtualHost - Container that defines the addresses and/or ports that may be send to contact the server. The directives within this container further define the virtualhost including what servername(s) are used and document root for each

ServerName - If the host part of the HTTP request matches this name, then allow the request. Normally this would be a domain name that maps to an IP.

ServerAlias - Alternate names accepted by the server.