Just a very quick article to compile some information I gleaned from various sites on setting up name-based virtual webhosting on your local OS X box.
I needed to set up http://foo.dev/ to do some local web development. Here are the steps to do it:
Use your favourite editor to edit /etc/httpd/httpd.conf. Uncomment and edit the line that starts with NameVirtualHost to read:
NameVirtualHost 127.0.0.1:80
Just below that line is a section with a “Virtual Host Example”. The example they have there (commented out, of course) is kinda useless. Instead, add this code right after the NameVirtualHost line given above:
<VirtualHost 127.0.0.1:80> DocumentRoot /Library/WebServer/Documents </VirtualHost>
This will configure it so that pointing your browser to http://localhost/ will bring up the standard “You’ve installed Apache” page.
This can be done in the main /etc/httpd/httpd.conf file, or in /private/etc/httpd/users/YOUR-USER-NAME.conf, whichever is preferred. A basic entry would look like this:
<VirtualHost 127.0.0.1:80> ServerName foo.dev ServerAdmin your-email@example.com DocumentRoot /Users/YOUR-USER-NAME/Sites/foo.dev </VirtualHost>
Make sure you create the ~/Sites/foo.dev directory.
Normally, you would just need to add an entry to your /etc/hosts files to say that foo.dev has the IP 127.0.0.1.
But OS X uses Netinfo to handle DNS (at least by default), so if you want to do things The Mac Way, run these commands instead:
sudo niutil -create . /machines/foo.dev sudo niutil -createprop . /machines/foo.dev ip_address 127.0.0.1 sudo niutil -createprop . /machines/foo.dev name foo.dev sudo niutil -createprop . /machines/foo.dev serves ’./local’
(You’ll have to enter your password for the first command).
Alternatively, this can all be down through Netinfo Manager. Simply launch the application (it’s in /Applications/Utilities), and click the lock to make changes (requires a password).
Then, use the Finder-like panes to navigate to / › machines › localhost. Click the “Duplicate” button, which will create the entry: / › machines › localhost copy. Select that entry and, in the Property/Value panes at the bottom, double-click on the name property to change the name from “localhost copy” to “foo.dev”.
Click the lock again to close it up, and confirm your changes.
Load System Preferences. Go to the “Sharing” panel and turn on “Personal Web Sharing”.
That’s it. You should now be able to open up http://foo.dev/ in your browser.
If you ever need to add a new virtual host, or change the Apache configuration for a host, you’ll just need to stop and restart Personal Web Sharing.
Copyright © 2000-2012 Colin Viebrock • All Rights Reserved
6 March 2005, 13:16 • PermaLink
I’m working on mu.wordpress.org mutliuser wordpress install that calls for handling of multiple x.domain.com instances like steve.domain.com, colin.domain.com from one installation.
11 October 2005, 20:02 • PermaLink
ServerName steve.domain.comServerAlias colin.domain.comSecond would be to know that you can set up a default virtual host with no server name. Any request that comes for a host not covered in another virtual host block will fall into this default configuration.
Third (okay, there are more than to options), is to try some of the strategies listed here.
11 October 2005, 22:35 • PermaLink
(This only applies to local DNS. If your server has a public IP, you won’t care.)
11 October 2005, 22:45 • PermaLink
22 October 2005, 19:32 • PermaLink
Excellent! i read a few tutorials before this one that went nowhere but this one works like a charm.
7 November 2005, 08:47 • PermaLink
Just what I needed. More articles please. I too wasted time on tutorials that went nowhere (maybe I read the same ones Tijs read).
Whatever, it works fine for me now. Thanks a lot.
28 November 2005, 08:59 • PermaLink
First, thank you for the great information. I returned all the books that I purchased as well.
Why do you need to set up the DNS? My domain names are purchased with Yahoo and I just manage my yahoo account to associate all of my domain names with the IP address that I am using.
Thanks again
15 December 2005, 12:39 • PermaLink
If you are using “real” domains, Son, and your machine has a static IP address, then you don’t need to set up the DNS.
The instructions I gave were for setting up “fake” domain names, like “foo.dev” or “something.dev”. Often you want to be able to refer to a development site via a domain name, and not via a subdirectory of another domain. This is so that, when your site goes live, you don’t need to change all your relative links and image tags.
16 December 2005, 02:27 • PermaLink