Apache mod_vhost_alias module fails to set DOCUMENT_ROOT

Mass virtual hosting using mod_vhost_alias or mod_rewrite module simplifies pattern-based virtual hosting. However, there is a major problem if your virtual host application makes use of the DOCUMENT_ROOT environment variable. According to the Apache documentation, the mod_vhost_alias does NOT correctly sets the DOCUMENT_ROOT variable and hence pontentially break PHP web applications that makes use of this environment variable. It seems that one logical thing to do is to use the VirtualDocumentRoot to map DOCUMENT_ROOT variable, but current mod_vhost_alias does NOT implement this function and it is very unlikely that it will any time soon.

To best way to solve this problem is to applying a patch for mod_vhost_alias.c, which is written by a person with an email address (cbs[at]cts.ucla.edu) that dynamically sets DOCUMENT_ROOT environment variable. The behavior is controlled by setting SetVirtualDocumentRoot to "on" or "yes". The original patch can be found apache bugzilla. To apply the patch, you'll have to rebuild the version of apache you're running. There is no easy way to rebuild mod_vhost_alias module only.

1. To rebuild apache, you'll need a source RPM for your platform. We're running Fedora Core 1, so we've downloaded the version from Fedora Legacy. RPM Find provides binary and source RPMs for various platforms.

2. Install source RPM by running, rpm -i httpd-2.0.x.y.z.tar.gz. Where x.y.z is the version of Apache you are installing. The source RPM will be installed in the rpm source directory. For redhat (and Fedora), the location of source rpm is /usr/src/redhat.

3. The next step is to modify the mod_vhost_alias.c as specified in the patch above, and rebuild the binaries. To get the mod_vhost_alias.c file, you'll need to untar httpd-2.0.x.y.z.tar file locaated in the /usr/src/redhat/SOURCE directory. Extract the tar archive in the temporary directory, and edit the mod_vhost_alias.c file located in the ./httpd-2.0.x/modules/mappers directory.

4. After modifying the mod_vhost_alias.c file, tar up the file and place it in the /usr/src/redhat/SOURCE folder. The new file replaces the old httpd-2.0.x.y.z.tar file.

5. Now, we need to rebuild a binary rpm. However, there are several development rpms that are needed to successfully compile the httpd RPM. There are a few dependencies that you'll have to resolve by installing a dozen or so rpms. You may retrieve those necessary rpms from the same place where you downloaded source rpm (step 1).

# cd /usr/src/redhat/SPECS
# rpmbuild -bb httpd.spec

6. Upon successfully rebuilding the Apache, extract the mod_vhost_alias.so file from the binary RPM. We only need to replace the mod_vhost_alias.so located in the /usr/lib/httpd/modules directory, and there is no reason to reinstall binary RPM. To extract a file from the RPM, use the rpm2cpio command below.

# cd /tmp
# rpm2cpio /usr/src/redhat/RPMS/i386/httpd-2.0.x.y.z.legacy.i386.rpm | cpio -d -i
# mv /tmp/usr/lib/httpd/modules/mod_vhost_alias.so /usr/lib/httpd/modules

You may wish to make a back of the mod_vhost_alias.so file before overwriting the original. You'll need to add SetVirtualDocumentRoot on in the httpd.conf file, and restart the apache.

Comments

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.