How to Configure Multiple Document Roots for Different Paths in Apache Virtual Host


2 views

The fundamental misunderstanding here lies in attempting to use DocumentRoot inside Location directives - which Apache explicitly prohibits. The correct approach involves using Alias directives for path-based document root separation.

Here's the technically proper way to structure your virtual host:


    ServerName mydomain.no-ip.org
    
    # Main document root
    DocumentRoot "/var/www/alias"
    
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
        DirectoryIndex index.php
    
    
    # SquirrelMail configuration
    Alias /mail "/usr/share/squirrelmail"
    
        Options FollowSymLinks
        DirectoryIndex index.php
        
            php_flag register_globals off
        
        
            Require ip 127.0.0.1
        
    
    
    # SVN configuration
    
        DAV svn
        SVNParentPath "/svnrepo"
        SVNListParentPath On
        AuthType Basic
        AuthName "My SVN Repo"
        AuthUserFile "/svnrepo/htpasswd"
        Require valid-user
    
  • Use Alias for filesystem path mapping instead of trying to set DocumentRoot per location
  • Each Alias should have a corresponding Directory block for access control
  • Location blocks are reserved for URL-space configuration (like your SVN setup)
  • Remember to run apachectl configtest before restarting

For more complex setups where applications run on different ports:

# For a Node.js app at /app
ProxyPass /app http://localhost:3000/
ProxyPassReverse /app http://localhost:3000/

If you encounter permission issues after implementing this:

# Check SELinux contexts
ls -Z /usr/share/squirrelmail
# Fix context if needed
chcon -R -t httpd_sys_content_t /usr/share/squirrelmail

Many developers assume they can directly nest DocumentRoot directives inside Location blocks in Apache configuration. While this seems logical for path-based routing, Apache's architecture requires a different approach:

# This WILL NOT work:
<Location /mail>
    DocumentRoot /usr/share/squirrelmail  # Invalid syntax
</Location>

For your specific case with SquirrelMail and SVN, here's the proper virtual host configuration:

<VirtualHost *:80>
    ServerName mydomain.no-ip.org
    DocumentRoot "/var/www/alias"
    
    # Default location
    <Directory "/var/www/alias">
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
        DirectoryIndex index.php
    </Directory>

    # SquirrelMail configuration
    Alias /mail /usr/share/squirrelmail
    <Directory "/usr/share/squirrelmail">
        Options FollowSymLinks
        DirectoryIndex index.php
        <IfModule mod_php5.c>
            php_flag register_globals off
        </IfModule>
        <Files configtest.php>
            Require local
        </Files>
    </Directory>

    # SVN configuration
    <Location /svn>
        DAV svn
        SVNParentPath "/svnrepo"
        SVNListParentPath On
        AuthType Basic
        AuthName "My SVN Repo"
        AuthUserFile "/svnrepo/htpasswd"
        Require valid-user
    </Location>
</VirtualHost>

Alias Directive: The proper way to map URL paths to different filesystem locations. This creates a virtual path mapping without changing the primary DocumentRoot.

Directory Blocks: Replace your attempted Location-based DocumentRoot with proper Directory containers that specify filesystem permissions.

When implementing this configuration:

  • Ensure all filesystem paths have correct Apache user permissions (typically www-data or apache)
  • For SVN access, verify mod_dav_svn is properly installed
  • After configuration changes, always test with apachectl configtest

If you encounter 403 Forbidden errors:

# Check directory permissions:
ls -la /usr/share/squirrelmail
# Verify SELinux context if applicable:
ls -Z /usr/share/squirrelmail

For SVN-specific problems, ensure the repository directory exists and has proper ownership:

sudo chown -R www-data:www-data /svnrepo