Set up multiple Apache virtual hosts in OS X

When you are developing websites in OS X, you’ll often want to be able to have a unique test domain for site. For example, my testing domain for this site on localhost is
madross.lh [the lh for localhost, use anything you like]
For j-cafe.com , I use j-cafe.lh

To do do this is fairly straightforward. You just need to make sure the ‘dummy names’ are added to the hosts file in /etc


127.0.0.1 j-cafe.lh
127.0.0.1 madross.lh

Then in your Apache config file, have this:


NameVirtualHost 127.0.0.1

<VirtualHost 127.0.0.1>
ServerName madross.lh
### add the other stuff obviously!
</VirtualHost>

<VirtualHost 127.0.0.1>
ServerName j-cafe.lh
### add the other stuff obviously!
</VirtualHost>

Then just restart the server:

$ sudo apachectl graceful

If you are having trouble with host names being cached you might try this:

$ sudo lookupd -flushcache

I use madross.lh for my testing server, and madross.com is obviously the real site. I keep the number of dots the same to prevent cookie problems. However, you may prefer to have your test server and the real server to have the same name. Obviously, this means you are not going to be able to access the real site. But that’s not really a problem. Just comment the names in /etc/hosts when you want to visit the real site (and vice-versa :-) )


# 127.0.0.1 j-cafe.com
# 127.0.0.1 madross.com

And remember to do

$ sudo lookupd -flushcache

(Adding an alias in .profile is helpful for this)

Fundamentally, this should work on Windows, Unix, Linux etc. On Windows, the hosts file is probably somewhere else.

0 Responses to “Set up multiple Apache virtual hosts in OS X”


  1. No Comments

Leave a Reply