TechBuzzPro

Change Apache Root Directory: Step-by-Step Guide

· 5 min read · Apps & Browsers

In this article
  1. What Is the Apache Root Directory?
  2. Why Change the Root Directory?
  3. Locating the Configuration Files
  4. Step-by-Step: Changing DocumentRoot
  5. Common Problems and Fixes
  6. Virtual Hosts vs. Global DocumentRoot
  7. Security and Permissions Best Practices
  8. Frequently Asked Questions

To change the root directory of an Apache web server, you edit the DocumentRoot directive in the Apache configuration file and update the corresponding <Directory> block, then restart Apache. This guide walks you through the process for both Ubuntu/Debian and CentOS/RHEL systems, covering common pitfalls and verification steps.

What Is the Apache Root Directory?

The root directory (often called DocumentRoot) is the folder on your server's filesystem where Apache looks for files to serve over HTTP. When a visitor requests http://yourserver.com/index.html, Apache maps that URL path to a file inside this directory. By default, the directory is set during installation—commonly /var/www/html on Debian-based systems and /var/www/html on RHEL-based systems (older versions used /var/www/html as well). Changing this root lets you serve content from a different location, such as a mounted drive, a user's home folder, or a dedicated data partition.

Why Change the Root Directory?

There are several legitimate reasons to move the document root:

  • Disk space: The default partition may be small; moving to a larger mounted volume avoids filling up the system drive.
  • Organization: You may want to separate application code from system files, or host multiple sites from different folders.
  • Security: Placing web files outside the default location can reduce exposure if other services share the default path.
  • Migration: You might be moving from another server where the root was in a non-standard location.

Locating the Configuration Files

Before editing anything, find your Apache configuration. The main file is usually:

  • Debian/Ubuntu: /etc/apache2/apache2.conf, with site-specific files in /etc/apache2/sites-available/ (e.g., 000-default.conf).
  • CentOS/RHEL: /etc/httpd/conf/httpd.conf, with virtual host files in /etc/httpd/conf.d/.

On Debian/Ubuntu, the DocumentRoot is typically defined in the virtual host file, not the main config. On CentOS/RHEL, it's often in the main httpd.conf unless you have virtual hosts.

Step-by-Step: Changing DocumentRoot

Here's the general procedure. The exact commands may vary slightly by distribution, but the logic is the same.

  1. Back up the configuration file: Before editing, copy the original. For example, sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/000-default.conf.bak.
  2. Edit the file: Open the relevant file with a text editor like sudo nano /etc/apache2/sites-available/000-default.conf or sudo vi /etc/httpd/conf/httpd.conf.
  3. Find the DocumentRoot line: It will look like DocumentRoot /var/www/html. Change the path to your desired directory, e.g., DocumentRoot /srv/www/mysite.
  4. Update the <Directory> block: Immediately after the DocumentRoot, there should be a <Directory /var/www/html> block. Change that path to match your new root. This block controls permissions for that folder, so it must match exactly.
  5. Create the new directory (if it doesn't exist): Use sudo mkdir -p /srv/www/mysite and set appropriate ownership (e.g., sudo chown -R www-data:www-data /srv/www/mysite on Debian, or apache:apache on CentOS).
  6. Test the configuration: Run sudo apachectl configtest or sudo apache2ctl configtest to check for syntax errors.
  7. Restart Apache: Use sudo systemctl restart apache2 (Debian) or sudo systemctl restart httpd (CentOS).

Common Problems and Fixes

Even with the correct steps, issues arise. Here are the most frequent:

  • 403 Forbidden error: This usually means the <Directory> block doesn't match the new path, or the folder permissions are too restrictive. Ensure the <Directory> path is identical to DocumentRoot and that the folder is readable/executable by the web server user.
  • 404 Not Found: The new directory is empty or the index file isn't named index.html (or whatever is set in DirectoryIndex). Place a test file in the new root.
  • Syntax error on restart: Check for typos in the config file. The configtest command will point to the exact line.
  • Old site still showing: Your browser may have cached the old page. Clear the cache or use a private window. Also, if you have multiple virtual hosts, make sure you edited the correct one.

Virtual Hosts vs. Global DocumentRoot

If you run multiple websites on one server, each virtual host has its own DocumentRoot. Changing the global DocumentRoot in the main config won't affect virtual hosts that explicitly define their own. You must edit each virtual host file individually. Conversely, if you only have one site, you can either edit the default virtual host or set the global DocumentRoot in the main config—but be consistent to avoid confusion.

Security and Permissions Best Practices

When moving the document root, security shouldn't be an afterthought. Follow these guidelines:

  • Set proper ownership: The web server user (usually www-data on Debian, apache on CentOS) should own the directory, or at least have read and execute permissions. For write access (e.g., uploads), you may need to adjust ownership or use group permissions.
  • Don't use the system root or home directories: Avoid pointing DocumentRoot to / or /home/user as that could expose sensitive files if misconfigured.
  • Keep the <Directory> block restrictive: Use Require all granted only if needed, and consider using Require all denied for subdirectories that shouldn't be public.
  • Check for symlinks: If your new root is a symlink, ensure Options FollowSymLinks is enabled (or use Options SymLinksIfOwnerMatch for stricter control).

Frequently Asked Questions

Do I need to change the <Directory> block too?

Yes, absolutely. The <Directory> block tells Apache what permissions apply to that folder. If it still points to the old path, Apache will use default permissions (often denying access), resulting in a 403 error. Always update both directives together.

What if I get a 'DocumentRoot must be a directory' error?

This means the path you specified doesn't exist or isn't a directory. Double-check for typos and ensure the directory exists. Also, make sure the path doesn't have a trailing slash—Apache expects the path without one (e.g., /srv/www/mysite, not /srv/www/mysite/).

Can I change the root directory without restarting Apache?

No, Apache reads the configuration at startup. After editing the config file, you must reload or restart Apache. A reload (sudo systemctl reload apache2) is less disruptive and picks up config changes without dropping connections, but a restart is also fine.

How do I change the root directory for a specific virtual host?

Edit the virtual host file (e.g., /etc/apache2/sites-available/example.com.conf), find the DocumentRoot inside that <VirtualHost> block, and change it. Then update the corresponding <Directory> block within the same file. Reload Apache afterward.

What's the difference between DocumentRoot and ServerRoot?

DocumentRoot is the folder that contains the web files served to visitors. ServerRoot is the directory where Apache's configuration, logs, and modules are stored (usually /etc/apache2 or /etc/httpd). You rarely need to change ServerRoot; changing DocumentRoot is what affects the content you serve.

Why does my new root show a directory listing instead of my index page?

This happens when there's no index.html (or the file specified in DirectoryIndex) in the new directory. Either create an index file or adjust the DirectoryIndex directive to include the filename you're using. Also, check that the <Directory> block has Options Indexes—if you don't want listings, remove that option.