How To Run Multiple Virtual Domains On Your Macbook Pro
How to support multiple virtual domains on a Mac is a question I had after recently purchasing a Macbook Pro. Part of the motivation in that purchase being that I wanted to get all my development stuff onto a portable platform so I could “work” when I’m supposed to be on vacation or from some destination deep in jungles of a remote island in the South Pacific or the couch. What I found was a lot of information on how to get one website to work under Apache but not multiple sites without manually switching them, it was a real pain, but after a good bit of research and some trial and error, I’ve come up with a solution that works for me.
First a few assumptions will be made:
1. Your running 10.4x of Mac OS. This might work on a previous version but I can’t confirm that.
2. You already have Apache & PHP preinstalled. (My Macbook Pro came this way I just had to turn them on).
3. Your using MySQL 5 (I’m running MySQL 5.0.45 for OSX 10.4 for i686).
4. You have the Mac utility NetInfo Manager installed (If you don’t have it, you should be able to install it from your install CD’s)
First things first. The only thing you should have to install is MySQL, so after running the installation when you go to “System Preferences” you should have a MySQL icon. Clicking that brings up a MySQL Server Status screen.

- Click the button to start MySQL
- Check the box to start the MySQL Server on Startup (this is optional but you’ll have to remember to always go start it up).
That done, our objective is to create an Apache/PHP/MySQL development environment that’s completely contained on the Mac and can support multiple virtual domains.
The first thing you should do is decide where you want to maintain your development files. I keep mine in:
/Macintosh HD/Users/<username>/Sites
(this will equate to something like /var/www/vhosts on your typical Linux server)
For this exercise we’ll deal with two sites. My blog Realitywired and another dev site I’m playing with The Unfocused Lens. Create two base directories in the path you decided on above, one for each site. Mine look like:
/Macintosh HD/Users/<username>/Sites/realitywired
/Macintosh HD/Users/<username>/Sites/unfocusedlens
Each of these would be considered my Web Roots for each site and I want them to respond to unique domain names without having to mess with DNS or Apache configs everytime I want to switch the site I’m working on. In this case I use:
http://www.realitywired.site
http://www.unfocusedlens.site
Note: You’ll want to use a domain suffix that isn’t internet routable, otherwise you’ll prevent yourself from being able to get to the real site on the net. For this reason I use .site but you can use any suffix you want.
Making this work is really pretty simple. First we need to tell Apache what virtual domains to use and where to find them, then we need to tell the Mac how to handle the domain names.
Apache first.
Open the file /etc/httpd/httpd.conf (/Macintosh HD/private/etc/httpd/httpd.conf)
Scroll to the very end of the file and add this line:
Include /etc/httpd/httpd-vhosts.conf
This is a precaution in the event that in future upgrades of Apache your virtual hosts won’t get lost. You may have to readd this line but that’s better than rebuilding your virtual hosts file from scratch.
Next we need to edit the virtual hosts file so in your editor open: /etc/httpd/httpd-vhosts.conf
(you may have to create a new one first time out).
At the very top of the file add this:
# Use name-based virtual hosting.
#
NameVirtualHost 127.0.0.1
Now, you need to add a section for your virtual hosts. Do this by adding the following:
<VirtualHost 127.0.0.1>
DocumentRoot /Users/<username>/Sites/realitywired
ServerName www.realitywired.site
<Directory “/Users/<username>/Sites/realitywired”>
Options All
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Don’t forget to change realitywired in my example to the Web Root we created earlier (physical location on your disk).
Now repeat this last step for each of your virtual hosts. In my example, I also have an entry for unfocusedlens that looks like:
<VirtualHost 127.0.0.1>
DocumentRoot /Users/<username>/Sites/unfocusedlens
ServerName www.unfocusedlens.site
<Directory “/Users/<username>/Sites/unfocusedlens”>
Options All
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
For each site you want to have, just add another entry below the last one. Once your done, save and close the file. (You may be asked to authenticate the change or you can SU from shell and edit the file that way).
Now we have to tell the Mac how to resolve these domains. For that we’re going to use the utility NetInfo Manager (found in Applications/Utilities). In NetInfo Manager, select “Machines” from the middle column list, then click “localhost” in the third column. (note: Your columns may shift themselves to the left depending on your screen size).

Now click the lock icon in the lower left hand corner to make the parameters editable, you’ll be asked to authenticate this action.
Ok, now you want to highlight the “localhost” item under the “machines” column. When you’ve done this, click the “Duplicate” button at the top of the window. You will be asked to confirm the modification. Click “Duplicate“.

You should end up with a copy of the “localhost” entry called “localhost copy“. Click this entry then look at the bottom window. You should see three properties (ip_address, name and serves). The only property we care about is “name“. In the bottom window double click the Value of the name property so you can edit it. You want to make this the name of the domain you used in your httpd-vhosts.conf file. So for Reality Wired I enter: www.realitywired.site
Now you have to save this. For me, the idiot Windows convert it’s a little tricky but you should just need to click on another machine entry, like the original “localhost“. You might also have to click on “broadcast” or another entry you may have but you should be presented with a Warning/confirmation box asking you if your sure you want to save the changes.

Click “Save” and you’ll be asked to confirm the modification, click “Update this copy”

Now repeat this process for each of your virtual domains. When your done, your NetInfo Manager window should look something like this:

You can now exit out of NetInfo Manager
The final thing you need to do is either turn on Apache or reload Apache so it reads in the new virtual host file. To do this, simply go to System Preferences and click the “Sharing” icon (located in the Internet & Network section).
Make sure your on the “Services” tab and find “Personal Web Sharing” in the list of services. To turn this on, highlight it and click the “Start” button to the right. (This should place a check mark in the box next to the service). You might also need to click the Unlock button in the lower left before you can change the settings.

If your adding a new virtual host you just need to reload Apache. Simply highlight the service, click stop, wait a few seconds, then click start.
That wasn’t too bad now was it? Before you get all giddy let’s test it out to make sure it works. Open a browser and type in one of the urls then click enter. What you should see is an Apache Index page, something like this:

If that doesn’t work then it’s possible directory listings is turned off in your httpd.conf file, so just create a bogus index.html file in the directory. Doesn’t matter what it is, just call it index.html, then try the url in the browser again. If you did everything correctly your test file should be loaded. You can do the same for your other virtual hosts and they should all resolve independently, just like they would on the internet.
Did it all work? Cool Beans! Now the next time you go on vacation you can take your work with you.
A couple of notes that might help you out further. If your using Wordpress, you extract the Wordpress files into the “Web Root” directory we created early on, so your structure should look like:
/Macintosh HD/Users/<username>/Sites/realitywired
/Macintosh HD/Users/<username>/Sites/realitywired/wp-admin
/Macintosh HD/Users/<username>/Sites/realitywired/wp-includes
/Macintosh HD/Users/<username>/Sites/realitywired/blog/wp-content
/Macintosh HD/Users/<username>/Sites/realitywired/index.php
/Macintosh HD/Users/<username>/Sites/realitywired/wp-config.php
and so on. In the wp-config file (or any other config file) to point it at your mysql server you simply use the local ip address of the machine for the hostname. Unless your using Parallels. I’m sure there is a way around this but because I have Parallels installed MySQL insists on responding on one of the 10.x addresses Parallels sets up. On my system, it uses the ip address for the Parallels Host-Guest adapter, not the Parallels NAT adapter. Like I said, it’s not really a big deal but it might cause you to scratch your head if you have Parallels installed.
If your running behind a proxy service (heaven forbid you’d work on your stuff while at your job), you need to set No Proxy for 127.0.0.1 and .site in your browsers proxy settings.
That should be it. There might be a simpler way of doing this but this is the easiest I managed to put together. If you found it helpful I’d really appreciate it if you passed it on or maybe give it a Digg or two.

Save this page
Stir it up on Mixx
Add to Reddit
Comment by Gerri on 27 September 2007:
Hi,
thanks for that very helpful and easy understanding howto.
Greetings
Gerri