How to Change Default Root Directory of Apache Web Server

The Apache web server default root directory for storing site content is at the “/var/www/html” path. This directory can be manually changed to the custom directory of your choice.

To achieve this, you need to edit Apache configuration files depending upon the distribution you were using and replace the current directory path with the new location.

Prerequisites

  • Apache Web Server is installed on your system
  • The drive should be mounted where the new location will be assigned
  • Web Browser for testing the result (Chrome, Firefox, etc.)

Configuration Files

Changing the default root directory of Apache requires editing the configuration file. This configuration file location varied from distribution to distribution.

For Debian/Ubuntu distributions, the Apache Web Server configuration file required to be modified is located at “/etc/apache2/sites-available/000-default.conf“.

For CentOS/Fedora distributions, the Apache Web Server configuration file required to be modified is located at “/etc/httpd/conf/httpd.conf“.

Moving Files from the Default Root Directory to a New Location

Assuming, “/home/trendoceans/Documents/sitedata” is the new location for the Apache root directory. So, we shall have to move the content from the current location “/var/www/html” to the new location, as shown below.

$ mkdir sitedata
$ sudo mv /var/www/html/* ~/Documents/sitedata/

After the file is transferred, you can move to the next step configuring your Apache for the new root directory.

Changing the Configuration Files for Apache

After the site data is moved to the new root directory location, open your Apache configuration file using TextEditor, as shown below.

$ sudo nano /etc/apache2/sites-available/000-default.conf               [On Debian/Ubuntu]
$ sudo nano /etc/httpd/conf/httpd.conf                                  [On CentOS/Fedora]

Below is the output of the above 000-default.conf and httpd.conf configuration files.

Updating Root Document Location in Debian/Ubuntu

From the configuration file (000-default.conf), find the line starting with “DocumentRoot” and place “#” at the beginning of the line. Then copy and paste the below code by changing the location to your root document location.

DocumentRoot /home/trendoceans/Documents/sitedata
<Directory "/home/trendoceans/Documents/sitedata">
    Require all granted
</Directory>

Below is the output of the above 000-default.conf file after changes.

Apache web configuration file with modifications in Debian
Apache web configuration file with modifications in Debian

Updating Root Document Location in Fedora/CentOS

For RHEL-based distributions, first open the “/etc/httpd/conf/httpd.conf” file, then find the “DocumentRoot” and ‘<Directory “/var/ww/html”>‘ and replace the location with your own new root directory location, as shown below.

Apache web configuration file with modifications in RHEL
Apache web configuration file with modifications in RHEL

Getting 403 Forbidden Permission / Document Root

After making the mentioned changes, if you get an error such as “403 Forbidden Permission / Document Root” while visiting the domain or localhost.

Then most probably, your new root directory location is not viewable. If Apache tries to visit your new root directory, it might not have permission to view it.

To give all-sufficient permission for accessing the web page within the new root directory, execute the below command by replacing the home and root directory with your own.

$ chmod 711 /home/trendoceans
$ chmod 755 /home/trendoceans/Documents/sitedata
$ chmod 644 /home/trendoceans/Documents/sitedata/*

That’s all you need to change the Apache root directory. If you are facing any difficulty or got some random error, let us know in the comment section!

Leave a Reply