how to change the document root directory in apache web server in linux

The Apache Web Server is one of the most popular web servers that is used both in development and production. As per the latest count, it boasts close to 50% of the market share. There are many reasons as to why the Apache server is popular.

It is easy to install, configure and run. It is pretty light weight and resource friendly allowing it to be installed on almost any hardware. You can run it even on your laptop for development with out much of an overhead.

When running Apache Web server, it has what is called a document root directive which is a folder on your local file system. This is the ‘root‘ folder from which the server will serve the files for an URL. So for example, if this folder is set to /var/www/ then the files in this folder are used to resolve URLs. For example, a URL will resolve to the file /var/www/page/

Many times, you might want to change this default document root folder or serve files from an alternate location. There are couple of different ways you can do this. One is to change the directive itself so that it now serves the files out of a different folder.

The DocumentRoot directive is specified in the Apache configuration file(s), which is often called httpd.conf in most distros. The location of the file can differ based on the distro and/or based on how you have installed the package. We will just go through some of the popular distros as examples.



You should be able to find the configration file under the folder /etc/httpd/conf/ on Fedora and related distros.



Some distros have two configuration files that you might need to modify.



It is a little more tricky in Gentoo. The DirectoryRoot might be in one of the files inside the vhosts.d/ directory if you have the virtual host feature enabled. Look for a file named default_vhost.include inside the /etc/apache2/vhosts.d/ folder. There maybe several include files here, one for each of the website that you have configured and you might have to modify the site specific file as well.

Once you have found the correct configuration files, modifying the document root itself is pretty trivial. Open the file in any text editor of your choice and search for the directive DocumentRoot in the content. Change the directory path to the new path in this line. Now, look for a tag that looks like <Directory ……. /> immediately after and modify the directory path to the one you want.

document root in apache web server

After you have modified the path and saved the configuration file, you will need to restart the apache service for the changes to take effect. Again how you restart the service depends on the distro you are using. In case you use SystemD, you can use

$ systemctl restart apache

If you happen to use SysVinit, then you can do

$ service httpd restart

In gentoo, you could do something similar to restart the service

$ /etc/init/apache2 restart

The configuration files provide a lot more options and flexibility for mapping URLs to files such as URL redirection, proxy and aliases. This post only deals with the simple case of modifying the default or current document root to another location.