Change Apache Root Directory: Step-by-Step Guide
In this article
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.
- 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. - Edit the file: Open the relevant file with a text editor like
sudo nano /etc/apache2/sites-available/000-default.conforsudo vi /etc/httpd/conf/httpd.conf. - Find the
DocumentRootline: It will look likeDocumentRoot /var/www/html. Change the path to your desired directory, e.g.,DocumentRoot /srv/www/mysite. - Update the
<Directory>block: Immediately after theDocumentRoot, 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. - Create the new directory (if it doesn't exist): Use
sudo mkdir -p /srv/www/mysiteand set appropriate ownership (e.g.,sudo chown -R www-data:www-data /srv/www/mysiteon Debian, orapache:apacheon CentOS). - Test the configuration: Run
sudo apachectl configtestorsudo apache2ctl configtestto check for syntax errors. - Restart Apache: Use
sudo systemctl restart apache2(Debian) orsudo 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 toDocumentRootand 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 inDirectoryIndex). Place a test file in the new root. - Syntax error on restart: Check for typos in the config file. The
configtestcommand 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-dataon Debian,apacheon 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
DocumentRootto/or/home/useras that could expose sensitive files if misconfigured. - Keep the
<Directory>block restrictive: UseRequire all grantedonly if needed, and consider usingRequire all deniedfor subdirectories that shouldn't be public. - Check for symlinks: If your new root is a symlink, ensure
Options FollowSymLinksis enabled (or useOptions SymLinksIfOwnerMatchfor 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.