As a web developer, I wanted a simple way to easily create and remove virtual hosts for Apple's built-in Apache server. The solution I've discovered allows me to create a virtual host just by creating a folder with the virtual hostname I want to use and editing my /etc/hosts file so that my browser will be able to find it. Removing the virtual host then is as easy as deleting the folder and the entry in /etc/hosts -- there's no need to edit Apache configuration files or restart Apache each time.
To set this solution up in Mac OS X 10.5.x, you'll need to edit two Apache configuration files, but you'll only have to do it this once. (Note that it's a good idea to make backups of your configuration files, just in case something doesn't work. I make no guarantees, nor am I responsible if you mess your system up!)
Here's what you need to do to get this working.
- In Terminal, edit (with root privileges) /etc/apache2/httpd.conf. In the section, add the following two lines:
You will also need to comment out the following line, by putting a # at the beginning, as seen here:LogFormat "%V %h %l %u %t \"%r\" %s %b" vcommon CustomLog /private/var/log/apache2/access_log vcommon
When finished, that section will look something like this: (Note: For brevity's sake, I took out the explanatory comments):#CustomLog /private/var/log/apache2/access_log commonFinally, uncomment the Include line in the Virtual hosts section so that it looks like this:<IfModule log_config_module> LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined LogFormat "%h %l %u %t \"%r\" %>s %b" common LogFormat "%V %h %l %u %t \"%r\" %s %b" vcommon <IfModule logio_module> # You need to enable mod_logio.c to use %I and %O LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio </IfModule> #CustomLog /private/var/log/apache2/access_log common # # Logging virtual-host data with the common format # CustomLog /private/var/log/apache2/access_log vcommon #CustomLog /private/var/log/apache2/access_log combined </IfModule># Virtual hosts Include /private/etc/apache2/extra/httpd-vhosts.conf - Edit (again with root privileges) /etc/apache2/extra/httpd-vhosts.conf to look like this:
# # Virtual Hosts # # If you want to maintain multiple domains/hostnames on your # machine you can setup VirtualHost containers for them. Most configurations # use only name-based virtual hosts so the server doesn't need to worry about # IP addresses. This is indicated by the asterisks in the directives below. # # Please see the documentation at # <URL:http://httpd.apache.org/docs/2.2/vhosts/> # for further details before you try to setup virtual hosts. # # You may use the command line option '-S' to verify your virtual host # configuration. # # Use name-based virtual hosting. # NameVirtualHost *:80 # # Mass Virtual Host configuration as taken from the Apache web documentation # <VirtualHost *:80> # get the server name from the Host: header UseCanonicalName Off # include the server name in the filenames used to satisfy requests VirtualDocumentRoot "/Library/WebServer/Documents/%0" VirtualScriptAlias "/Library/WebServer/Documents/%0/cgi-bin" </VirtualHost> - If Web Sharing is currently on, restart it using the GUI or apachectl.
- Create a folder named mysite.dev in /Library/WebServer/Documents
- Edit /etc/hosts, adding the following line:
The /etc/hosts file needs root privileges to modify it. I recommend using BBEdit or TextWrangler, as they will prompt you for your password to save the changes.127.0.0.1 mysite.dev
Bonus Round: Set up Parallels to access your local site.
- If you look in the Network System Preference on your Mac, you should see one or more Parallels[networking type] Adapter interfaces on the left. Examine each, and note down the IP addresses. (The virtual machines see one of these addresses as your Mac's IP address.)
- Run your virtual machine and find out its IP address. The first three numbers should match one of the ones for your Mac. Edit the virtual machines hosts file as above, but using the Mac's corresponding IP address instead of 127.0.0.1. (In my copy of Windows XP, the hosts file is stored at C:\system32\drivers\etc\hosts)

