Setting up virtual hosts on your Mac OS X system

Date: Fri Nov 09 2007
The Apache web server offers a feature called "virtual hosting". What this does is make one computer be a server for more than one domain. This is a widely used way to share multiple websites on one server. For example when a web hosting company offers shared hosting, they are using virtual hosting to make it so each of their servers houses more than one of their customers.

How many web sites can you host on one computer? Well, it depends on the amount of processing required to deliver the pages. Consider that a hosting provider will often house a hundred or more domains on one server, and that should give you an idea of the capabilities. Of course to successfully host many domains on one server, you should streamline the computer as possible, and run very few extraneous services. No iTunes, no Garage Band, don't log in and try to use that computer for work, just let it be a web server.

Implementing virtual hosting is very simple. There are two steps. First, one makes it so multiple domain names point to your Mac. Rather than your Mac being known by one name, it will have more than one name. The second step is to configure Apache so it knows about each of the domains.

Setting up the domain name: The steps required vary depending on your purpose. If your purpose is soley your own test needs, then perhaps the domain name doesn't have to be visible from anywhere outside your computer. Maybe the domain name can be a fiction you configure inside your computer and isn't visible to any other computer.

If this is the case, simply edit /etc/hosts and add this line:


127.0.0.1 domain.test

This makes your computer believe the host name domain.test is in reality the IP address 127.0.0.1, or your local host. Hence, in a web browser requesting http://domain.test will contact the web server on that computer.

Of course if the domain names you are configuring need to be known by others, then the domain names need to be part of the regular domain name system (DNS). The domain.test name shown above is obviously outside the regular DNS.

Telling Apache to resolve these domains: Edit the /etc/httpd/httpd.conf and look for a line containing NameVirtualHost. It should be commented out, so uncomment the line. Below it is an example of setting up a virtual host. You can add to /etc/httpd/httpd.conf a set of lines like these to enable Apache to recognize a virtual host.


<virtualhost domain.test="">
ServerName domain.test
DocumentRoot "/Users/UserName/Development/testdomain.domain.com"
</virtualhost>