Virtual Hosts on OS X

21 October 2004 • code | Mac • PermaLink

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:

Turn on Name-Based Virtual Hosts in Apache

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.

Configure Your Virtual Host in Apache

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.

Set up the DNS

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.

Turn on Apache

Load System Preferences. Go to the “Sharing” panel and turn on “Personal Web Sharing”.

Enjoy

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.

Comments

  1. Excellent post.
    Randy
    6 March 2005, 13:16 • PermaLink
  2. any idea how to setup wildcard virtual domains?

    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.
    Steve Mallett
    11 October 2005, 20:02 • PermaLink
  3. Two options, Steve. First is to use the ServerAlias directive to define other host names that should use the same virtual host settings:

    ServerName steve.domain.com
    ServerAlias colin.domain.com

    Second 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.
    Colin
    11 October 2005, 22:35 • PermaLink
  4. As for the DNS, I’m not sure. Google doesn’t help much, but this post has a suggestion using a third-party program.

    (This only applies to local DNS. If your server has a public IP, you won’t care.)
    Colin
    11 October 2005, 22:45 • PermaLink
  5. After a day-long search of paid subscriptions and purchased books to no avail, I want to thank you for offering such clear, concise tutorial on this subject.
    Court Nielsen
    22 October 2005, 19:32 • PermaLink
  6. Excellent! i read a few tutorials before this one that went nowhere but this one works like a charm.

    Tijs
    7 November 2005, 08:47 • PermaLink
  7. 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.

    Morten Vadskaer
    28 November 2005, 08:59 • PermaLink
  8. 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

    Son
    15 December 2005, 12:39 • PermaLink
  9. 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.

    Colin
    16 December 2005, 02:27 • PermaLink