How to Change Default Apache ‘DocumentRoot’ Directory in Linux

Apache web server is probably the most used web server across platforms including different Linux distributions and Windows. The web server is used to deliver web content and can serve many queries at once.

It is often the preferred choice by professionals for building different web projects. Having at least basic knowledge of this web server is vital for any young professional who wants to start a career as a Linux system administrator.

In this short tutorial, you are going to learn how to modify the root directory for Apache web server. For the purpose of this tutorial, we will use Ubuntu/Debian and RHEL/CentOS/Fedora based installations of the web server.

However the paths and directives are practically the same for other distributions as well, so you will be able to apply the learned in different OSes as well.

To make the necessary changes you need to modify the DocumentRoot directive of the web server. This is the directory from which Apache will read the contents that the visitor will access over browser. Or in other words, this is the directory that forms the tree of directories that will be accessible over web.

The default DocumentRoot for Apache  is:

/var/www/html
or
/var/www/

These paths are described in the Apache’s configuration file.

Ubuntu/Debian

/etc/apache2/sites-enabled/000-default
/etc/apache/apache2.conf

CentOS/RHEL/Fedora

/etc/httpd/conf/httpd.conf

To change the document root for your Apache web server simply open the corresponding file with your favourite text editor and search for DocumentRoot.

#
# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
#
DocumentRoot "/var/www/html"

After that change the path to the new target directory and make sure that Apache is able to read/write in that directory. Once you have modified the DocumentRoot, save the file and restart apache with:

# systemctl restart apache [For SystemD]
# service httpd restart [For SysVinit] 

Final thoughts

The change of default document root is an easy task that can be completed in a couple of minutes. When making such changes is important to make sure that you do not make any typos and make sure to always restart Apache after making changes to its configuration file.

Posted by Contributor