Learn how to change Apache document root in Linux by following this step-by-step guide. Adjust the server’s default web directory to a new location for better organization and functionality. #centlinux #linux #apache
Table of Contents
What is Apache Document Root?
The Apache Document Root is the directory on a web server where the files for a particular website are stored and served to users. It is the default location that Apache uses to find and load web pages when a request is made to the server. For example, when a user visits your website, Apache looks in the Document Root for the index file (such as index.html
or index.php
) to display the homepage.
The default Document Root is typically set to /var/www/html
in Linux, but it can be customized by modifying the Apache configuration files.

Pros and Cons of Alternate Document Root
Pros of Changing Apache Document Root:
- Better Organization: You can customize your web directory structure to fit your project’s needs, making it easier to manage files.
- Enhanced Security: Moving the document root outside of the default directory can reduce the risk of attacks targeting the default setup.
- Multiple Websites: Easier to host multiple websites on the same server by assigning different document roots for each virtual host.
- Custom Permissions: Allows for better control over access permissions and isolation of sensitive files.
- Flexibility: You can align the document root with existing directory structures that fit your workflow.
Cons of Changing Apache Document Root:
- Configuration Complexity: Changing the document root requires modifying configuration files, which can be confusing for beginners.
- Potential Errors: Misconfiguring the document root can lead to server errors or broken websites if the permissions or paths are incorrect.
- Security Risks: If not done properly, changing the document root can inadvertently expose sensitive directories.
- Increased Maintenance: You’ll need to manually update configurations for any future changes or additions to the server setup.
- Compatibility Issues: Some web applications expect the default Apache structure, and changing it might require additional modifications to the app’s configuration.
Recommended Training: Apache Web Server from Vipin Gupta

Change Apache Document Root in Linux
Below is the code with added descriptions and instructions for each piece of code to change the Apache Document Root in a Linux system:
1. Check Operating System and Kernel Version
cat /etc/os-release
uname -r
- Description: Displays the operating system’s release information and the current kernel version.
- Purpose: To verify the Linux distribution and kernel version, ensuring compatibility before making changes.
2. Update System Packages
dnf update -y
- Description: Updates all the installed packages to their latest versions.
- Purpose: Keeps the system secure and up to date with the latest features and security patches.
3. Install Apache HTTP Server
dnf install -y httpd
- Description: Installs the Apache HTTP server package.
- Purpose: Sets up the Apache web server, which is necessary for hosting web pages.
You May Also Like: How to Change Apache Port in Linux
4. Enable and Start Apache Service
systemctl enable --now httpd
- Description: Enables Apache to start automatically at boot and immediately starts the Apache service.
- Purpose: Ensures Apache is running and will restart automatically after a system reboot.
WiseWing 2.4GHz Wireless Gaming Headset for PC, PS5, PS4, Switch, USB Gaming Headphones with Noise Canceling Microphone, Bluetooth 5.4, RGB Lights, 60H Battery Headsets for Mac, Laptop, Mobile
$26.99 (as of April 17, 2025 16:01 GMT +00:00 – More infoProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)5. Create a Test Web Page in the Default Document Root
vi /var/www/html/index.html
- Description: Opens the default Apache document root directory in the
vi
text editor to create a new HTML file. - Purpose: To create a simple web page for testing the Apache server.
<html><head><title>Default Document Root</title></head>
<body>
<h2>Page from Default Document Root</h2>
</body>
</html>
- Description: Basic HTML content for the test page.
- Purpose: This page will be served by Apache, allowing you to verify the default setup is working correctly.
6. Test Apache Server
curl http://localhost
- Description: Uses
curl
to fetch the default page from the Apache server. - Purpose: To verify that Apache is serving the test web page correctly from the default document root.
7. Create a New Document Root Directory
mkdir -p /u01/www/html
- Description: Creates a new directory structure for the new document root.
- Purpose: To set up a new location for serving web content outside the default
/var/www/html
.
8. Set Ownership for the New Directory
chown -R apache:apache /u01/www
- Description: Changes the ownership of the new directory to the Apache user and group.
- Purpose: Ensures Apache has the necessary permissions to read and serve files from the new document root.
Linux for Beginners: A Practical and Comprehensive Guide to Learn Linux Operating System and Master Linux Command Line. Contains Self-Evaluation Tests to Verify Your Learning Level
$16.97 (as of April 17, 2025 15:57 GMT +00:00 – More infoProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)9. Create a Test Web Page in the New Document Root
vi /u01/www/html/index.html
- Description: Opens the new document root directory in the
vi
text editor to create a new HTML file. - Purpose: To create a simple web page for testing the Apache server from the new document root.
<html><head><title>New Document Root</title></head>
<body>
<h2>Page from New Document Root</h2>
</body>
</html>
- Description: Basic HTML content for the test page in the new document root.
- Purpose: This page will be served by Apache from the new directory, allowing you to verify that the new document root is configured correctly.
10. Install SELinux Management Utilities
dnf install -y policycoreutils-python-utils
- Description: Installs the SELinux policy management utilities.
- Purpose: Required to configure SELinux for the new document root directory.
Must Read: How to Create Virtual Host in Apache Server
11. Configure SELinux Context for the New Directory
semanage fcontext -l | grep httpd_sys_content_t
- Description: Lists all the current SELinux file context settings for the Apache server.
- Purpose: To check existing configurations before adding a new one for the new document root.
semanage fcontext -a -t httpd_sys_content_t "/u01/www(/.*)?"
- Description: Adds a new file context rule for the new document root directory.
- Purpose: Configures SELinux to allow Apache to serve files from the new directory.
restorecon -R -v /u01
- Description: Restores the SELinux context on the new document root directory.
- Purpose: Applies the new SELinux policy settings to the directory and its contents.
12. Edit Apache Configuration to Change Document Root
vi /etc/httpd/conf/httpd.conf
- Description: Opens the Apache main configuration file in the
vi
text editor. - Purpose: To change the DocumentRoot setting to point to the new directory.
- Instruction: Find the line that specifies
DocumentRoot
and replace the path with"/u01/www/html"
. Also, update the<Directory>
directive to match the new document root:DocumentRoot "/u01/www/html" <Directory "/u01/www/html">
Amazon Fire HD 10 Kids Pro tablet (newest model) ages 6-12. Bright 10.1″ HD screen, includes ad-free content, robust parental controls, 13-hr battery and slim case for older kids, 32 GB, Happy Day
$139.99 (as of April 17, 2025 15:58 GMT +00:00 – More infoProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)13. Restart Apache Service
systemctl restart httpd
- Description: Restarts the Apache service to apply the changes made in the configuration.
- Purpose: Necessary to make Apache use the new document root.
14. Test Apache on the New Document Root
curl http://localhost
- Description: Uses
curl
to test the Apache server and fetch the page from the new document root. - Purpose: To verify that Apache is correctly serving content from the new directory.
Complete Video Tutorial
Conclusion
These instructions guide you through changing the Apache web server’s document root in a Linux system. It includes setting up SELinux and the necessary permissions to ensure that Apache can serve content from the new directory.
Searching for a skilled Linux admin? From server management to security, I ensure seamless operations for your Linux systems. Find out more on my Fiverr profile!
Frequently Asked Questions (FAQs)
1. How do I change the Apache document root in Linux?
To change the Apache document root, edit the DocumentRoot
directive in the Apache configuration file (/etc/httpd/conf/httpd.conf
for RHEL/CentOS or /etc/apache2/sites-available/000-default.conf
for Ubuntu/Debian). Modify the path after DocumentRoot
to your desired directory and save the file.
2. Do I need to restart Apache after changing the document root?
Yes, after making changes to the Apache configuration file, you must restart Apache for the changes to take effect. You can do this with the following command:
sudo systemctl restart apache2 #(Ubuntu/Debian) or
sudo systemctl restart httpd #(RHEL/CentOS)
3. What permissions should the new document root directory have?
Ensure that the new document root directory has the proper read and execute permissions for the Apache user (www-data
on Ubuntu/Debian or apache
on RHEL/CentOS). You can set the correct permissions with:
sudo chown -R www-data:www-data /path/to/new/document/root
sudo chmod -R 755 /path/to/new/document/root
4. Can I change the document root for specific virtual hosts?
Yes, you can change the document root for specific virtual hosts by editing their respective configuration files, typically found in /etc/apache2/sites-available/
on Ubuntu/Debian or /etc/httpd/conf.d/
on RHEL/CentOS. Modify the DocumentRoot
directive within the virtual host configuration.
5. What if I get a “403 Forbidden” error after changing the document root?
A “403 Forbidden” error usually occurs if Apache doesn’t have the correct permissions to access the new document root. Ensure the directory and its contents are readable by the Apache user and the directory has the appropriate permissions. Additionally, check the Apache configuration to ensure that access is allowed for the new directory.
Leave a Reply
You must be logged in to post a comment.