Watts (chipotle) wrote,

Virtual Host Testing on OS X

John Gruber of Daring Fireball wondered if there were any “testbed virtual hosting utilities” for OS X like Headdress and VirtualHostX. I only knew of the first, but I stopped using it when I realized how easy doing this actually was.

“Virtual hosting” is the ability to run multiple virtual web servers with one Apache server. It’s how ranea.org, esmuck.net, stardancer.org and solluna.org are all actually on the same box (sneaky, huh?). For testing purposes, it can be useful to do this on your local machine, particularly if you’re dorking around with web applications like Ruby on Rails or Django: instead of trying to cram that into your personal ~/Sites/ directory, you just put the application anywhere, and make it run on a specific port on your machine. So you can run your new CakePHP application on, say, port 9000, and go to it at http://localhost:9000 in your browser.

What Headdress and VirtualHostX do is add a little GUI to make setting up virtual hosts under OS X. Yes, this is nominally easier than mucking around in Apache configuration files, but unless you’ve got a highly unusual situation, you’re not setting up new virtual hosts on a near-daily basis. You’re doing it a few times a year, more than likely, and that only if you’re into pretty hardcore web application nerdery. And, at least under Leopard (10.5), mucking around in configuration files isn’t that difficult, actually.

The first thing to do is to just open a Terminal window, and at the shell prompt type:

cd /etc/apache2

Now, edit the apache configuration file with your favorite text editor. Mine is TextMate, so I’d type:

mate httpd.conf

(Or if you’re feeling more nerdy, try sudo vi httpd.conf there. TextMate and BBEdit will prompt you for your password when you’re saving the file because it’s an “administrator” file; I’m going to presume anyone who’d be using vi knows how to set up sudo to do the same thing from the command line.)

Toward the end of that file, there’s a bunch of Include directives, mostly commented out (lines beginning with # characters are comments). Find the section that reads:

# Virtual hosts
#Include /private/etc/apache2/extra/httpd-vhosts.conf

And uncomment the include line, so it now reads:

# Virtual hosts
Include /private/etc/apache2/extra/httpd-vhosts.conf

Now save the file, and go back to the Terminal prompt. Type

cd extra
cp httpd-vhosts.conf http-vhosts.orig.conf

(This makes a backup of your configuration file in case it gets screwed up.) Now, edit the virtual host configuration:

mate httpd-vhosts.conf

And at this point, pretty much delete everything from the comment that reads

# Use name-based virtual hosting.

on down, or just comment it out. You don’t need it. Yes, this means NameVirtualHost *:80 should be commented out, too, because you’re going to be using port-based virtual hosting.

Now, add these three lines:

<VirtualHost *:80>
    DocumentRoot "/Library/WebServer/Documents"

This is your “root” host, the one that actually runs on the standard HTTP port.

Okay, so what about the virtual hosts? Glad you asked. This is the cool part here. For any virtual host you want to add to the system, add a block that looks like this:

Listen 9000
<VirtualHost *:9000>
    DocumentRoot "/Users/watts/webapp"
    Options Indexes FollowSymLinks MultiViews
    <Directory "/">
        AllowOverride All
        Order allow,deny
        Allow from all

In this block, 9000 is the port I want to run the virtual host on, and /Users/watts/webapp is the “root” of the virtual server, i.e., an index.html file in that directory will be the one you see if you go to http://localhost:9000.

After you save that file, you can restart Apache by going back to the terminal window and typing

sudo apachectl restart

…or just turn it off and back on again in the Sharing preferences pane, if you prefer. And that’s it! If you want another virtual host running, just add another block with a new port:

Listen 9100
<VirtualHost *:9100>
    DocumentRoot "/Users/watts/drupal"
    Options Indexes FollowSymLinks MultiViews
    <Directory "/">
        AllowOverride All
        Order allow,deny
        Allow from all

Now. Is this as easy as a GUI to do this? No. But once you have that first block in place, copying and pasting and changing a couple of numbers is, to use Rails guru David Hansson’s phrase, not rocket surgery.

Tags: internet, mac, programming
  • Post a new comment


    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded