The development workflow
If I was developing a site locally on my Mac for a site called http://www.cooldomain.com/ I’d develop it under the domain dev.cooldomain.com that I’m hosting locally on my OS X powered Mac. This way I know, whenever I type dev. before the domain name it’s the local copy and when I have www. before the domain name it’s the live site.
Setting up vhosts
First I create a folder in my home directory called webserver this is where I keep all of my locally hosted sites. For a site called www.cooldomain.com I create a folder within the webserver folder called cooldomain. I then create 2 folders inside that folder first logs (for log files) and then public_html. Your site will be served from the public_html folder.

For the sake of consistency I’m going to pick a single OSX GUI text editor for this tutorial. TextWrangler is free, installs a handy command line tool (edit) and it works well so we’ll go with it. (Feel free to use emacs, vi, pico, TextMate, SubEthaEdit or any editor that supports plain text editing.)
First we’ll refresh the list of binaries (since we may have just installed the TextWrangler Command Line Tool). Fire up the terminal and type:
rehash
Next, we’ll change to the hosts file (/etc) directory and make a backup copy of the hosts file
cd /etc/
sudo cp hosts hosts.backup
Now, we’ll edit the file as a root user in TextWrangler
sudo edit hosts
Once the hosts file is open in TextWrangler, scroll down to the end and add two lines
# lookupd -flushcache
127.0.0.1 dev.cooldomain.com
The first line is merely a coment that will remind us to flush the lookupd cache whenever we edit the hosts file. The second line tells the machine to send all requests for dev.cooldomain.com to this machine and not out to the ‘net.
Save the file after you’ve added these lines and switch back to the terminal and type:
lookupd -flushcache
Now, we’re gonna edit the httpd.conf file. First change to the httpd.conf directory and make a backup copy of the httpd.conf file
cd /etc/httpd/
sudo cp httpd.conf httpd.conf.backup
Now, we’ll edit the file as a root user in TextWrangler
sudo edit httpd.conf
Once the file is open in TextWrangler, do a find (cmd-f) for NameVirtualHost and be sure the following line is not commented out (with a # in front of it) if it is remove the #:
NameVirtualHost *:80
Next set up your vhost configuration at the bottom of the document. You can copy and paste the following code remembering to change yourusername with your actual username and cooldomain with the name of the folder you created in the webserver folder earlier.
<VirtualHost *:80>
ServerAdmin youradminaddress@email.com
ServerName dev.cooldomain.com
DocumentRoot /Users/yourusername/webserver/cooldomain/public_html
ErrorLog /Users/yourusername/webserver/cooldomain/logs/dev.cooldomain.com-error_log.log
CustomLog /Users/yourusername/webserver/cooldomain/logs/dev.cooldomain.com-access_log.log common
<Directory /Users/yourusername/webserver/cooldomain/public_html>
Options Indexes ExecCGI FollowSymLinks MultiViews
AddHandler cgi-script .cgi
AllowOverride all
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Once you’re done modifying the httpd.conf file, save it return to the terminal to check your changes.
running
apachectl configtest
will tell you if there are any errors in your httpd.conf file. If you’ve got errors, go back to the httpd.conf file and check everything you changed or added.
If you get
Syntax OK
You’re ready to restart apache. Restarting apache can be done from the command line via
sudo apachectl restart or sudo apachectl graceful
or via the sharing preferences pane (clicking stop and then start will effectively restart apache).
That’s it. Just put your site inside the public_html you created earlier, fire up a web browser and type in the domain name (dev.cooldomain.com in this example). Apache should serve it up with no hassles at all.
This is just one approach to vhosting under OS X that happens to work for me. Here are references to two others:
Enabling Virtual Hosts on MacOS X (evolt.org)
Virtual Hosting on Mac OS X (patrickgibson.com)
Just stumbled across this article now (a little late) — this is exactly the way I do localhosting on OS X. FWIW, I like this method much better than the two others you referenced as it more closely reflects real-world usage.
Everything worked like you said except iI can’t access the page.
It says Forbidden You don’t have pemission to access / on the server.