Site icon CentLinux

Change Apache Document Root in Linux

Share on Social Media

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

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.

Change Apache Document Root in Linux

Pros and Cons of Alternate Document Root

Pros of Changing Apache Document Root:

  1. Better Organization: You can customize your web directory structure to fit your project’s needs, making it easier to manage files.
  2. Enhanced Security: Moving the document root outside of the default directory can reduce the risk of attacks targeting the default setup.
  3. Multiple Websites: Easier to host multiple websites on the same server by assigning different document roots for each virtual host.
  4. Custom Permissions: Allows for better control over access permissions and isolation of sensitive files.
  5. Flexibility: You can align the document root with existing directory structures that fit your workflow.

Cons of Changing Apache Document Root:

  1. Configuration Complexity: Changing the document root requires modifying configuration files, which can be confusing for beginners.
  2. Potential Errors: Misconfiguring the document root can lead to server errors or broken websites if the permissions or paths are incorrect.
  3. Security Risks: If not done properly, changing the document root can inadvertently expose sensitive directories.
  4. Increased Maintenance: You’ll need to manually update configurations for any future changes or additions to the server setup.
  5. 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

2. Update System Packages

dnf update -y

3. Install Apache HTTP Server

dnf install -y httpd

You May Also Like: How to Change Apache Port in Linux

4. Enable and Start Apache Service

systemctl enable --now httpd

5. Create a Test Web Page in the Default Document Root

vi /var/www/html/index.html
<html><head><title>Default Document Root</title></head>
<body>
<h2>Page from Default Document Root</h2>
</body>
</html>

6. Test Apache Server

curl http://localhost

7. Create a New Document Root Directory

mkdir -p /u01/www/html

8. Set Ownership for the New Directory

chown -R apache:apache /u01/www

9. Create a Test Web Page in the New Document Root

vi /u01/www/html/index.html
<html><head><title>New Document Root</title></head>
<body>
<h2>Page from New Document Root</h2>
</body>
</html>

10. Install SELinux Management Utilities

dnf install -y policycoreutils-python-utils

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
semanage fcontext -a -t httpd_sys_content_t "/u01/www(/.*)?"
restorecon -R -v /u01

12. Edit Apache Configuration to Change Document Root

vi /etc/httpd/conf/httpd.conf

13. Restart Apache Service

systemctl restart httpd

14. Test Apache on the New Document Root

curl http://localhost

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.

Exit mobile version