Don't Forget to Plant It!

Setting Up Virtual Hosts on Mac OSX

I’ve been juggling a few different web projects lately, and decided to setup different virtual hosts on my Mac so that I can easily work with them. Googling around gave me a lot of different answers, none of which seem to work completely. This is what finally worked for me (on Snow Leopard).

First, add a new local domain to your /etc/hosts file:

127.0.0.1       localhost devsite.local

Next, you’ll need to configure Apache with this new virtual host. Fortunately, the default Apache config has this partially setup. Open up /etc/apache2/httpd.conf and uncomment the following Include:

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

Now, we need to add our virtual host to the httpd-vhosts.conf file referenced above. The file already had a couple of sample configuration in it, but I commented out those and added the following:

1
2
3
4
5
6
7
8
9
<VirtualHost *:80>
DocumentRoot "/Library/WebServer/Documents"
ServerName localhost
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/usr/docs/devsite.local"
ServerName devsite.local
</VirtualHost>

This first entry will map localhost to its default document location (without it http://localhost won’t work correctly). The second entry maps my new domain.  Additionally, you’ll want to make sure files in your new docs directory have adequate access permissions.  I ended adding a new Directory section to httpd-vhosts.conf file:

1
2
3
4
5
6
<Directory "/usr/docs/devsite.local">
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
Allow from all
</Directory>

Now all you have to do is put your web files in /usr/docs/devsite.local. I originally had my new local domain map to <user dir>/Sites/devsite.local, but changed it because I would have to make sure Apache could access to all the directories leading up to those docs. So instead I just symlinked my http docs from my user directory into /usr/docs.

Comments