Virtual hosts in Apache

Apache, the popular Web server for Linux and Unix, allows you to host virtual hostnames with multiple IP addresses. You can set one IP to each hostname. But what if you want to host multiple hostnames on a single IP? Apache allows you to do it.

The trick is a single command that goes in the httpd.conf configuration file:

NameVirtualHost 1.2.3.4

Replacing 1.2.3.4 with your real IP address, this will allow Apache to know on which IP it should serve the virtual hostnames. Then you can add the virtual commands for every hostname you want to host:

<VirtualHost virtual.host.com> ServerAdmin webmaster@virtual.host.com DocumentRoot /home/httpd/virtual ServerName virtual.host.com

</VirtualHost>

This will add virtual.host.com in your list of virtual hosts and serve the pages in /home/httpd/virtual. Of course you need to have the actual virtual.host.com hostname pointing to that system.