Enabling Dynamically Configured Name-based Virtual Hosting on Mac OS X

Enabling virtual hosting on your Mac OS X machine will allow you to serve web content from a single IP address for multiple domain names, including sub-domains. This is useful for having a local development environment on your laptop, as you never need to edit your web server config files, just create additional folders matching the site names you want to work with.

Note: the original version of this document was created for pre-Leopard Mac OS X versions. See the previous revision here. This version references Apache 2.2 on Mac OS X Leopard, and recommends editing the hosts file rather than using NetInfo.

All the following involves editing Apache server config files, which requires admin privileges. They are located at /etc/apache2/httpd.conf (main config file) and /etc/apache2/extras/httpd-vhosts.conf (file that is included, keep all your virtual host related config here), and it's best to make a backup before you begin (just copy the original with a new extension like "bak", e.g. /etc/apache2/httpd.conf.bak).

First, you need to uncomment the sections related to virtual host aliasing in your main httpd.conf file, by deleting the # in front of the following lines:

#LoadModule vhost_alias_module libexec/httpd/mod_vhost_alias.so
#AddModule mod_vhost_alias.c

You'll also need to uncomment the include which will pull in the next file, again by removing the # from in front of Include.

# Virtual hosts
#Include /private/etc/apache2/extra/httpd-vhosts.conf

Decide where you want to put all your web files. I placed them all in a directory next to the main, default server, at /Library/WebServer/Hosts. Create that directory.

Now add this snippet at the end of the /etc/apache2/extras/httpd-vhosts.conf file:

#allow access to the Hosts directory where your sites are
<Directory "/Library/WebServer/Hosts">
Options Indexes FollowSymLinks MultiViews
AllowOverride All
#you could configure the following to only allow access from localhost
Order allow,deny
Allow from all

#get the server name from the Host: header
UseCanonicalName Off
VirtualDocumentRoot /Library/WebServer/Hosts/%0/html

Now restart your web server with the command apachectl graceful If you don't see any errors, everything should be working.

At this point, you should be able to add folders like /Library/WebServer/Hosts/example.bmannconsulting.com/html and you're good to go. You can also use symlinks, so have multiple domains point to the same content. Remember, you either need to have DNS for the domain set up correctly or edit your hosts file at /etc/hosts. An example of that would be adding a line like    example.local to the end of the file -- it's corresponding web directory would then be /Library/WebServer/Hosts/example.local/html.

Note: For those developing Drupal multisite, any additional sites you want to develop on the same codebase, just symlink to your base Drupal install. So, if your base install is at base.example.local, and you want to create newsite.example.local, symlink /Library/WebServer/Hosts/newsite.example.local to /Library/WebServer/Hosts/base.example.local. You will still need to create the directory for the newsite at /Library/WebServer/Hosts/base.example.local/html/sites/newsite.example.local for Drupal to properly pick it up.

Related Links