Apache VirtualHost Configuration: Key Differences Between DocumentRoot and Directory Directives in Mac OS X


4 views

When configuring Apache VirtualHosts on Mac OS X, two critical directives often cause confusion: DocumentRoot and Directory. While they both deal with file system paths, they serve fundamentally different purposes in Apache's configuration.

The DocumentRoot specifies the base directory that contains the website's files. Apache will serve files from this location when requests come in for this VirtualHost. For example:

DocumentRoot "/Users/username/websites/myproject.dev"

The Directory block defines access permissions and other settings for a specific directory (which may or may not be the DocumentRoot). It's where you configure security settings like:

<Directory "/path/to/directory">
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

No, they don't have to match, but typically you'll want to include a Directory block for your DocumentRoot to ensure proper permissions. However, you might have additional Directory blocks for other paths your application needs to access.

Here's a properly configured VirtualHost for Mac OS X:

<VirtualHost *:80>
    ServerName myproject.dev
    DocumentRoot "/Users/me/websites/myproject/public"
    
    ErrorLog "/private/var/log/apache2/myproject-error_log"
    CustomLog "/private/var/log/apache2/myproject-access_log" common
    
    <Directory "/Users/me/websites/myproject/public">
        Options FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
    
    # Additional directory for uploads
    <Directory "/Users/me/websites/myproject/storage">
        Options -Indexes
        AllowOverride None
        Require all granted
    </Directory>
</VirtualHost>

When setting up Apache on macOS, watch for:

  • File permissions in your home directory
  • SELinux or App Sandbox restrictions
  • Case sensitivity in path names

Always test your config before restarting Apache:

sudo apachectl configtest

When configuring Apache virtual hosts on macOS, two critical directives often cause confusion: DocumentRoot and Directory. While they're related to file system paths, they serve fundamentally different purposes in your Apache configuration.

The DocumentRoot specifies the directory where Apache should look for files to serve when a request comes in for this virtual host. It's essentially the "home folder" for your website.

DocumentRoot "/Users/yourusername/Sites/mysite"

The Directory block defines access permissions and other settings for a specific directory and its subdirectories. It's where you configure security rules and override behaviors.

<Directory "/Users/yourusername/Sites/mysite">
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

While your Directory block should include your DocumentRoot path, they don't need to be identical. The Directory can cover parent directories or specific subdirectories with different permissions.

Here's a proper macOS VirtualHost configuration with both directives working together:

<VirtualHost *:80>
    ServerName mysite.local
    DocumentRoot "/Users/devuser/Sites/mysite/public"
    
    ErrorLog "/private/var/log/apache2/mysite-error_log"
    CustomLog "/private/var/log/apache2/mysite-access_log" common
    
    <Directory "/Users/devuser/Sites/mysite/public">
        Options -Indexes +FollowSymLinks +MultiViews
        AllowOverride All
        Require all granted
    </Directory>
    
    <Directory "/Users/devuser/Sites/mysite/private">
        Require all denied
    </Directory>
</VirtualHost>

Remember these macOS-specific tips when configuring Apache:

  • User home directory paths need proper permissions (chmod 755 ~/Sites)
  • Apache runs as _www user by default on macOS
  • You may need to adjust SELinux contexts if enabled

If you encounter 403 Forbidden errors, check these common macOS solutions:

# Fix directory permissions
sudo chmod -R 755 /Users/yourusername/Sites
sudo chown -R _www /Users/yourusername/Sites/mysite

# Verify Apache can access the path
sudo -u _www ls /Users/yourusername/Sites/mysite