An Architect's View

CFML, Clojure, Software Design, Frameworks and more...

An Architect's View

Named Virtual Hosts

April 11, 2007 · 25 Comments

I don't know why but I've never bothered setting up named virtual hosts for local development. I've generally just used multiple ColdFusion instances on different ports or multiple Apache listeners (and port-based virtual hosts). I guess that was related to the type of development I was doing (working for a corporate and thus focused on just one big project at a time). Today I decided that my Apache setup was less than ideal and took the plunge and reconfigured everything to use named virtual hosts instead. It went pretty smoothly because all I did was:
  • Replace existing Alias directives with VirtualHost groups (and copy in any associated Directory group)
  • Changed each port-based VirtualHost group to use *:80
  • Added appropriate ServerName directives with made up domain names
  • Added those domain names to /etc/hosts
  • Restarted Apache (sudo apachectl restart)
Now I can hit dev.corfield.org or dev.fuseboxframework.org or whatever and get the site I expect. Nice. Why didn't I do this sooner? The only roadblock I hit was that I drew a complete blank on how to enable directory listings (for my local "scratch" ColdFusion instance). I had Options Indexes enabled everywhere but no dice, just "403 Forbidden". I could browse directories on my main document root but couldn't browse directories on any of my ColdFusion domains. I thought it was something deep inside an XML file. After bashing my head against a wall for about half an hour I asked a few folks online. They didn't know. So I did what I should have done in the first place: I asked on the #coldfusion channel on irc.dal.net. Sure enough, within a few minutes I was up and running. Thanks go to B0llock5 for that! The solution? To add the following directives to the VirtualHost that needed directory browsing:
  • Order Allow,Deny
  • Allow from All
I knew this really so I just felt dumb for blanking on it. Duh!

Tags: apache · coldfusion

25 responses so far ↓

  • 1 Damon Gentry // Apr 11, 2007 at 5:21 PM

    I've often worked in places that used a 'shared' development server. Named Virtual Hosts are ideal because you can then use DNS based host headers to give each developer a 'working copy' of the project on the shared server.

    adam.project.net --> /users/adam/projects/xxxx
    cindy.project.net --> /users/cindy/projects/xxxx

    And, if you have the capacity, you can give each developer their own JRun server. Check-ins (or commits) can then be copied (via Ant or some alternative) to the integration directory.

    dev.project.net --> /web/int/projects/xxxx
  • 2 Ouz Demirkap1 // Apr 11, 2007 at 5:54 PM

    I have always named virtual hosts with Apache on windows. But I prefer to use names without TLDs. Instead of "demirkapi.net" I use "demirkapi".

    I also would suggest to use XAMPP (apachefriends.org) which has PHP, MySQL and some other useful stuff beside Apache.

    And here some other ideas for an efficient development platform:

    http://demirkapi.net/avblog/permalinks/2005/11/22/Development%2DPlatform%2D%2D%2D1/

  • 3 Jeff Self // Apr 11, 2007 at 6:44 PM

    I've been doing this for a while. The only downside is when I want to throw a quick test app up and I have to remember to add a virtual host for it and add it to my /etc/hosts file.

    Since I develop with multiple web technologies, I don't want the jrun_module all the time on my MacBook Pro. So I put the JRun module and settings inside of each virtual host that requires ColdFusion.
  • 4 SeanG // Apr 11, 2007 at 7:42 PM

    Personally, my default is always to set up a named virtual host for every project that I work on. Personally, I think it just makes things easier. Addressing things from a hard document root from the presentation side makes things a whole lot easier for me.

    I did have one client that insisted on having everything set up to use relative addressing relatively recently (no pun intended), however this is how they had set up their production environment.

    I guess the moral of the story..... mimic production as much as possible, even if it does mean editing your host and apache config files as time goes on.
  • 5 Kurt Wiersma // Apr 11, 2007 at 7:46 PM

    My development environment got a huge boost when 6.1 came out and I finally learned how to use Apache name based vhosts with multiple instances. That was great when I figured out how to do that. It has been great ever since. I just add the following inside my virtualhost tag.

    JRunConfig Verbose false
    JRunConfig Apialloc false
    JRunConfig Ignoresuffixmap false
    JRunConfig Bootstrap 127.0.0.1:51000

    Replace porst 51000 with the proxy port for the cf instance oyu want. I usually have a default instance which I define at the top of my apache config.
  • 6 Sammy Larbi // Apr 11, 2007 at 8:06 PM

    I've recently been using things like codeodor.dev... I was thinking about posting and asking if anyone is doing that - good to see I'm not alone =)
  • 7 Sean Corfield // Apr 11, 2007 at 10:23 PM

    Just goes to show that sometimes I am *very* late to the party :)

    @Oguz and Sammy - interesting... never occurred to me to use completely bogus domain names!

    @Kurt - thanx for the tips on multiple instances and multiple named virtual hosts.
  • 8 Spike // Apr 11, 2007 at 11:08 PM

    If you have a lot of projects with different hosts entries (like I often do) you might find the VirtualDocumentRoot directive more useful than Named Virtual Hosts. Here's a minimal example:

    <VirtualHost 127.0.0.1>
       VirtualDocumentRoot C:/webapps/%0/www/
    </VirtualHost>

    As long as I have a directory under C:/webapps/ that has the same name as the domain name (or IP address) in the browser, I don't need to add anything else to my Apache config file. I also don't need to stop and restart Apache if I add an entry to my hosts file. It automatically works as long as there is a directory to match the domain. I don't remember the last time I even looked at my Apache config file to be honest, and I have about 9 projects that are new in the last 6 months.
  • 9 Christoph Schmitz // Apr 11, 2007 at 11:58 PM

    I set up my dev environement so that I have one virtual host for each application server.

    So I have cf5.procept.net, cf61.procept.net, cf70.procept.net, bd.procept.net, and, railo.procept.net. All virtual hosts use the same document root directory. In case I want to test something agains a different app server, I just change the URL.

    Plus I have virtual hosts for my biggest clients for which I do corporate intranet development.
  • 10 Tero Pikala // Apr 12, 2007 at 12:36 AM

    One thing we did at the office was to add new top level domain "local" and then had "*.local" to resolve to 127.0.0.1. Local development sites are now http://client.local/

    Saves you trouble of modifying hosts file every time...
  • 11 Tom Chiverton // Apr 12, 2007 at 1:03 AM

    I always wondered how people who didn't do this managed :-)
  • 12 Dan Wilson (B0ll0ck5) // Apr 12, 2007 at 7:19 AM

    Wow. Great comments in this post! I learned another thing today.

    dw
  • 13 Ken Dunnington // Apr 12, 2007 at 7:58 AM

    I used to set up local VirtualHosts (even have a script written to add or delete entries from NetInfo - I don't remember why!) but now that I've added an internal company-wide DNS server, I use that to direct everybody to the right machine/site, so I'll add an entry for a project, and then anyone can browse to "project.dev.company.com" (since dev.company.com is the default domain suffix.) This lets me share sites on multiple machines with everyone in the office. I'm working on setting up a reverse proxy with Apache so I have less to maintain (and so I can make dev sites accessible through our firewall) but that's a task for a quieter month. :)
  • 14 Dave Shuck // Apr 12, 2007 at 8:41 AM

    Sean, another thing that I tend to do is to keep a directory in my home directory like /home/dshuck/apps/apache2. After I set up Apache into a typical place like /usr/local/apache2 I delete the 'conf' and 'htdocs' folders and symbolically link them to the ones in my /home/dshuck/apps/apache2/ directory. Considering I use my same unmolested /home partition no matter what distribution I happen to be running, I don't have to go through the pains of resetting up all my vhosts every new installation. This is really helpful since I often have about 10-15 virtual hosts in my httpd-vhosts file.
  • 15 William from Lagos // Apr 12, 2007 at 10:39 AM

    Sometimes, Apache doesn't pass CFM pages to ColdFusion for processing. Instead the source code is displayed. Why is that?
  • 16 dickbob // Apr 12, 2007 at 12:38 PM

    @Spike - I really like your use of VirtualDocumentRoot that's so neat. I've had play with that today but can't access my CFMX Admin because it doesn't match the pattern of the other sites on my server and I don't seem to be able to override it on a domain by domain basis.

    Any thoughts?
  • 17 Sean Corfield // Apr 12, 2007 at 1:58 PM

    @Dave Shuck - an httpd.conf file can include others: I keep all my v-hosts in a separate file and simply include it from my httpd.conf file. By default, Apple does this:

    Include /private/etc/httpd/users/*.conf

    to include the conf file for each individual user.
  • 18 Dave Shuck // Apr 12, 2007 at 2:05 PM

    Good point Sean. My httpd.conf does include the vhosts file in a subdirectory 'extra'. It would probably be simpler to just mod that in the httpd.conf.

    So pretty much... umm... forget what I said. :)
  • 19 Spike // Apr 12, 2007 at 2:14 PM

    @dickbob

    Try adding an Alias:

    Alias /CFIDE C:/CFusionMX7/wwwroot/CFIDE

    I use the standard port 8500 URL to access the CF adminstrator, so I don't know if you'll also need to add any <Directory> or <Location> directives. My guess is that you'll need a <Directory> directive something like this:

    <Directory C:/CFusionMX7/wwwroot/CFIDE>
    Order allow,deny
    Allow from all
    </Directory>

    For local development you should be able to put the Alias and <Directory> outside the VirtualHost. That way it will apply to all virtual hosts including any that don't use the standard VirtualDocumentRoot.
  • 20 dickbob // Apr 12, 2007 at 2:43 PM

    @Spike - I already have the Alias in there.

    Previous to your suggestion I had loads of...

    <VirtualHost *>
    DocumentRoot /Users/dickbob/Sites/foo/www
    ServerName foo
    Alias /CFIDE /Library/Apache2/htdocs/CFIDE
    </VirtualHost>

    ...entries, one for each site but now have one...

    <VirtualHost 127.0.0.1>
    VirtualDocumentRoot /Users/diskbob/Sites/%0/www
    Alias /CFIDE /Library/Apache2/htdocs/CFIDE
    </VirtualHost>

    ...which works find for all my sites until I try and hit the CFMX Admin on...

    http://localhost/cfide/administrator/index.cfm

    ...when it starts to complain...

    "File not found: /cfide/administrator/index.cfm"

    I've tried add another entry after my new one...

    <VirtualHost *>
    DocumentRoot /Library/Apache2/htdocs
    ServerName localhost
    </VirtualHost>

    ...but it doesn't seem to cure the problem.

    I just need some way of saying "use VirtualDocumentRoot for all sites except localhost".

    Hummm, I'm still new to this Apache stuff.
  • 21 Sean Corfield // Apr 12, 2007 at 3:40 PM

    @dickbob, Apache is case sensitive so you need /CFIDE in your URL.
  • 22 Spike // Apr 12, 2007 at 3:55 PM

    @dickbob

    As Sean mentioned, Apache is case sensitive. Putting in an Alias for both /CFIDE and /cfide should get around most of the issues you're likely to run into.
  • 23 dickbob // Apr 15, 2007 at 8:44 AM

    Yep, that's it Sean! Which is strange because it was working like this before I changed to using VirtualDocumentRoot. Oh well problem solved, thanks.

    @Spike, that's a neat idea I'll use that as well, thanks.
  • 24 Raymond Camden // May 15, 2007 at 6:41 AM

    I'm wondering if folks can help me. Sean pointed me to this blog entry and the changes he had made, and I've done the same to my box. But while I can hit my localhost, my other virtual server -always- returns a 403.
  • 25 Raymond Camden // May 15, 2007 at 8:02 AM

    Ugh. Never mind. Turns out it was permissions. I thought I had that fixed. SHoot, I had a+wr on the folder. But Apache wants search permissions higher. Pain in the rear I tell ya. :)

Leave a Comment

Leave this field empty