How to Change DocumentRoot in Apache on Ubuntu: Configuration Reload vs Restart


2 views

In Ubuntu's Apache2 setup (especially in older versions like 8.04), the configuration is split across multiple files:

/etc/apache2/
├── apache2.conf       # Main configuration file
├── httpd.conf         # Often empty (for user configurations)
├── sites-available/   # Virtual host configurations
│   └── default        # Default virtual host
└── sites-enabled/     # Symlinks to enabled sites

For production environments, you should modify the virtual host configuration rather than httpd.conf:

# Edit the default virtual host
sudo nano /etc/apache2/sites-available/default

# Find and modify these lines:
DocumentRoot /home/username/temp
<Directory /home/username/temp>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

After making changes, you have three options to apply them:

# 1. Graceful reload (recommended for most changes)
sudo /etc/init.d/apache2 reload

# 2. Full restart (needed for some SSL changes)
sudo service apache2 restart

# 3. Alternative reload method
sudo apache2ctl graceful

When changing DocumentRoot to a user directory, permissions are critical:

# Set proper ownership
sudo chown -R username:www-data /home/username/temp

# Set directory permissions
sudo chmod -R 755 /home/username/temp

# For PHP files, you might need:
find /home/username/temp -type f -exec chmod 644 {} \;
find /home/username/temp -type d -exec chmod 755 {} \;

If changes don't take effect:

# Check for syntax errors
sudo apache2ctl configtest

# View active configuration
sudo apache2ctl -S

# Check error logs
tail -f /var/log/apache2/error.log

For newer Ubuntu versions (18.04+), the process is similar but uses different commands:

# Systemd reload command
sudo systemctl reload apache2

# Alternative location for default site
/etc/apache2/sites-available/000-default.conf

When working with Apache on Ubuntu (including legacy versions like 8.04), the configuration is typically split across multiple files in /etc/apache2/. The main files you'll encounter are:


apache2.conf - Main configuration file
httpd.conf - Historically used for user configurations (often empty)
sites-available/ - Contains virtual host configurations
sites-enabled/ - Symlinks to enabled sites

In modern Apache installations on Ubuntu, the DocumentRoot is usually defined in the default virtual host file:


/etc/apache2/sites-available/000-default.conf

For older versions (like Ubuntu 8.04), check:


/etc/apache2/sites-available/default

Here's the correct way to modify your document root:


# Open the default site configuration
sudo nano /etc/apache2/sites-available/000-default.conf

# Find the DocumentRoot line (usually around line 12)
DocumentRoot /var/www/html

# Change it to your desired path
DocumentRoot /home/username/public_html

After changing the DocumentRoot, you must ensure proper permissions and configure the directory:


# Create the directory if it doesn't exist
mkdir -p /home/username/public_html

# Set proper permissions
chown -R $USER:$USER /home/username/public_html
chmod -R 755 /home/username/public_html

# Create a test file
echo "<html><body><h1>It works!</h1></body></html>" > /home/username/public_html/index.html

You have several options to apply your changes:


# Graceful restart (recommended for production)
sudo apache2ctl graceful

# Alternative methods
sudo service apache2 reload
sudo /etc/init.d/apache2 reload

If your changes don't take effect:

  1. Check for syntax errors: sudo apache2ctl configtest
  2. Verify SELinux contexts if applicable
  3. Ensure the directory exists and is readable by Apache
  4. Check error logs: tail -f /var/log/apache2/error.log

For more complex setups, consider creating a dedicated virtual host:


<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /home/username/public_html
    ServerName yourdomain.com
    
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
    
    <Directory /home/username/public_html>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>