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 set the DOCUMENT_ROOT variable and hence potentially breaks PHP web applications that make use of this environment variable. It seems that one logical thing to do is to use the VirtualDocumentRoot to map the 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 apply 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 the 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 located 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 the source rpm (step 1).

  6. # cd /usr/src/redhat/SPECS
    # rpmbuild -bb httpd.spec
    
  7. 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 in the httpd.conf file, and restart the apache.

Share this post

Comments (0)

    No comment

Leave a comment

All comments are moderated. Spammy and bot submitted comments are deleted. Please submit the comments that are helpful to others, and we'll approve your comments. A comment that includes outbound link will only be approved if the content is relevant to the topic, and has some value to our readers.


Login To Post Comment