Document Root in Apache

document root

Today we will talk about Document Root in Apache. It works as a directory by default that serves to Apache web server.

By default the path of document root is “/var/www/html“. We can change it from the configuration of Apache. If you don’t know anything about Apache web server then learn from this link.

We will start with installing and starting the Apache web server by “dnf install httpd -y” and “systemctl start httpd“.

Now, We can check our IP address by “ifconfig” command. We have to put our IP address in browser and check for Apache’s default page.

We can see Apache’s default page on our IP address. Now we will create one file named “index.html” with the text “Hi from /var/www/html“.

This will help us to know later for the file location as we are going to create the file on default location of Apache that is “/var/www/html“.

Next, We will check on browser again with our IP and check the content of the file.

Now we will create the new document root for our Apache web server as “/apache/documents” with the help of “mkdir -p” command.

We will check the created path for surety before changing the document root.

Next, We will open the configuration file of Apache to change document root from “/var/www/html” to “/apache/documents“.

We will go by doing “vim /etc/httpd/conf/httpd.conf“.

Now we will search for the term “DocumentRoot “/var/www/html”” and change it to “DocumentRoot “/apache/documents”“.

We will also change the other details that contain the path “/var/www/html” to “/apache/documents“.

We can save the file and restart the Apache server by “systemctl restart httpd“.

We can check our IP address again as it will not show the previous file content now because of changing document root.

Next, We will make new file named index.html under /apache/documents with the content “Hi from /apache/documents” as it will confirm our new document root for Apache web server.

After creating the file we have to provide the necessary permissions to the new document root.

First, We will start with chown and chmod permissions as “chown -R apache:apache documents” for giving the ownership permission for document directory to Apache.

Similarly, “chmod -R 755 documents” for giving the read, write and execute permission for document directory to Apache.

Last permission is for SELinux that will make Apache to work securely by “chcon -Rt httpd-sys-content_t /apache/documents“.

Now you can restart the httpd service and refresh the browser.

As we can see our new document root is working perfectly with Apache web server.

For buying affordable hosting with the best support service, Please click on the link.

FAQ on Document Root in Apache

Q. What is the DocumentRoot in Apache?

A. The DocumentRoot is the directory from which Apache serves web files when a user visits your site. For example, if your DocumentRoot is /var/www/html, then https://domain.com/index.html will serve /var/www/html/index.html.

Q. Why is Apache not loading my index.html after changing the DocumentRoot?

A. Possible causes:
File doesn’t exist at the new location.
Incorrect file or directory permissions.
You didn’t update the corresponding <Directory> block.
SELinux restrictions.
Apache not restarted after change.

Q. What are the required permissions for the DocumentRoot directory?

A. The directory should be readable by Apache’s user.

chown -R apache:apache /path/to/site
chmod -R 755 /path/to/site

Q. How can I have different DocumentRoots for multiple sites?

A. Use Apache Virtual Hosts. Example:

<VirtualHost *:80>
ServerName site1.com
DocumentRoot “/var/www/site1”
</VirtualHost>

<VirtualHost *:80>
ServerName site2.com
DocumentRoot “/var/www/site2”
</VirtualHost>


Discover more from Root Learning

Subscribe to get the latest posts sent to your email.

5 thoughts on “Document Root in Apache

Leave a Reply

Your email address will not be published. Required fields are marked *