Apache Root Directory (DocumentRoot)

Apache root directory or web root directory is the folder location on the filesystem that the web server uses to serve files from, for a given request. It can be specified inside the <VirtualHost> block/container or outside it (main server config) using the DocumentRoot directive. From the documentation:

This directive sets the directory from which httpd will serve files. Unless matched by a directive like Alias, the server appends the path from the requested URL to the document root to make the path to the document.

The important bit is that the server appends incoming request path to the document root to find the path to the document or file that has to be served. So if a virtual host for example.com points to DocumentRoot /var/www/html then for example.com/foo/bar.htm, Apache will serve /var/www/html/foo/bar.htm.

Few things to note here:

  1. If the directory path passed to DocumentRoot is not absolute (or relative) then it is assumed relative to the server root.
  2. If the virtual host block lacks a DocumentRoot directive, then the default root directory is used.

How can I change the root directory ?

There are a bunch of options.

  1. Find the virtual host block in use for your website and then either set or change the DocumentRoot path there.
  2. Find the location for your main server config file (httpd.conf or apache2.conf) and set or change the DocumentRoot path there to modify the default root directory.

Based on the operating system in use, you need to find the virtual host file (for 1) or main server config file (for 2) and add or change DocumentRoot with the desired value.

# Something like the following:

# Old Value
DocumentRoot /var/www/html
# New Value
DocumentRoot /home/codingshower/html

On this page, you can check where these files can be found for default httpd installations across different unix-based or unix-like systems (Ubuntu, Debian, CentOS, Fedora, FreeBSD, macOS).

Note: For any change made to DocumentRoot, you may have to change the respective <Directory> block’s path as well.

Do not forget to restart apache after making any config changes with any of the following command that works for you (will most likely need a sudo):

$ service apache2 restart
$ service httpd restart

$ systemctl restart apache2
$ systemctl restart httpd

$ systemctl restart httpd.service

$ apachectl restart
$ httpd restart

Leave a Reply

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