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 Online Training: Learn Bash Shell in Linux for Beginners
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.
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.
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">
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
If you are new to Linux and facing difficulty in working at Linux Bash prompt. We recommend that, you should read The Linux Command Line, 2nd Edition: A Complete Introduction by William Shotts.
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.