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:
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
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
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.