Samuel Sjöberg's weblog

Skip to navigation

Dynamic virtual hosts

I started using VirtualDocumentRoot on my development server when I started the refactoring of this weblog. In this post I will describe how I've set things up and how I got the actual document root without using the DOCUMENT_ROOT environmental variable.

The benefit of this approach is that you can easily setup multiple virtual hosts without ever needing to edit httpd.conf again. My reason for wanting multiple virtual hosts in to have a setup similar to the production environment. If mod_rewrite is supposed to work on the document root directory, I like to set things up according to this. Otherwise problems arise when you change directory later and need to update all absolute path names (e.g. image paths in CSS files).

Setting things up involves three steps:

  1. Reconfigure Apache to use a virtual document root.
  2. Add the hostnames to the hosts file.
  3. Never ever trust DOCUMENT_ROOT. This rule applies regardless of this setup or not.

Configuring Apache

Open httpd.conf in your favorite text editor. Make sure this two lines is present and not commented:

LoadModule vhost_alias_module libexec/httpd/
AddModule mod_vhost_alias.c

The two lines should already be in the file, but might be commented. The will not be found together in pairs, so look for the one-by-one.

Next, search for the DocumentRoot and comment it out. Instead, add the VirtualDocumentRoot declaration. This is my setup:

VirtualDocumentRoot /Users/sas/Sites/%0/www

%0 is the name of the folder that is our hostname. The webroot is the www folder, but just as well be direct under the hostname folder, or in for example public_html.

Finally, if you have any VirtualHost configuration you can comment that out as well. After that, restart Apache. On OS X this can be done from the command-line by typing:

sudo apachectl graceful

Setting up the hosts file

For this virtual mapping to work you'll need to edit your hosts file. On OS X you will need to be root. The file is located in /etc/hosts. Here is an example on how I've added a couple of hostnames. thesis.local

When you enter or http://thesis.local you are directed to your local server and apache maps the requests to the corresponding folder name. An important thing to remember here is to not map actual domain names since they wouldn't be accessible from the machine any more.

If you are on a local network, for example behind a router, and have multiple test machines, or if you're using for example Parallels you can access all your dynamic virtual hosts by name if they are added to the machines hosts file. For this to work, you should of course point the domain names to your IP instead of

Get hold of the DOCUMENT_ROOT

The PHP variable $_SERVER['DOCUMENT_ROOT'] will not be set correctly when dynamic virtual hosts are used. However, this shouldn't be a problem since the value really isn't trustworthy ot begin with. Don't trust the DOCUMENT_ROOT ever. Instead, deduce it based on your scripts' current location.

The example below defines the constant BASEPATH to be the actual document root. I usually define this constant among others in /includes/, that is why ../ is necessary to get to deduce the document root.

// BASEPATH contains the DOCUMENT_ROOT of the virtual host
define('BASEPATH', realpath(dirname(__FILE__) . '/../'));

That was the final step. Hopefully you now have a dynamic virtual host environment up and running.

Pages linking to this entry

Pingback is enabled on all archived entries. Read more about pingback in the Pingback 1.0 Specification.

About this post

Created 22nd October 2006 19:01 CET. Filed under PHP.

0 Pingbacks