How to Change Apache Document Root on macOS and Fix 403 Forbidden Error


9 views

On macOS, Apache's default document root is set to /Library/WebServer/Documents in the main configuration file /etc/apache2/httpd.conf. When you modify this to point to a custom directory like /webcontent, you need to ensure proper permissions and configuration.

The 403 Forbidden error typically occurs because:

  • The new directory lacks proper read/execute permissions
  • Apache's user (_www on macOS) doesn't have access
  • SELinux contexts aren't properly set (though less common on macOS)

Here's how to properly change the document root:

# 1. Create the new directory
sudo mkdir /webcontent

# 2. Set ownership and permissions
sudo chown -R _www:_www /webcontent
sudo chmod -R 755 /webcontent

# 3. Edit httpd.conf
sudo nano /etc/apache2/httpd.conf

Find and modify these lines:

DocumentRoot "/webcontent"
<Directory "/webcontent">
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

After making changes, always:

# Test configuration
sudo apachectl configtest

# Restart Apache
sudo apachectl restart

For development environments, you might want to:

# Enable userdir module (for ~/Sites)
sudo a2enmod userdir

# Set up virtual hosts
<VirtualHost *:80>
    DocumentRoot "/webcontent/dev-project"
    ServerName dev.local
    <Directory "/webcontent/dev-project">
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>
  • Check Apache error logs: tail -f /var/log/apache2/error_log
  • Verify directory permissions: ls -ld /webcontent
  • Test with a simple index.html file first

When modifying Apache's DocumentRoot on macOS, simply editing httpd.conf often isn't enough. The 403 Forbidden error typically occurs because:

  • The new directory lacks proper ownership
  • Apache's _www user can't traverse the directory structure
  • SELinux contexts aren't properly set (though less common on macOS)

Here's the full procedure to change DocumentRoot to /webcontent:


# 1. Create directory with proper permissions
sudo mkdir /webcontent
sudo chown -R $(whoami):_www /webcontent
sudo chmod -R 775 /webcontent

# 2. Edit httpd.conf
sudo nano /etc/apache2/httpd.conf

# 3. Make these changes:
DocumentRoot "/webcontent"
<Directory "/webcontent">
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

# 4. Verify config and restart
sudo apachectl configtest
sudo apachectl restart

For stricter security setups, consider ACLs instead of broad permissions:


# Set ACL for Apache user
sudo chmod -R 755 /webcontent
sudo chown -R root:wheel /webcontent
sudo chmod +a "_www allow read,execute" /webcontent
sudo chmod +a "_www allow read,write,delete,add_file,add_subdirectory,file_inherit,directory_inherit" /webcontent/*

If issues persist:


# Check error logs
tail -n 20 /var/log/apache2/error_log

# Verify directory permissions
ls -ld /webcontent
ls -la /webcontent

# Test Apache user access
sudo -u _www ls -la /webcontent

For development environments, symlinks can be easier to manage:


sudo mkdir /Users/youruser/Sites
sudo ln -s /Users/youruser/Sites /webcontent