Configure Apache’s DocumentRoot

This article summarizes how to set Apache’s DocumentRoot.

Overview of DocumentRoot

The DocumentRoot is a fundamental configuration in the Apache web server that specifies the directory where the server looks for website files to serve to clients. Essentially, it defines the “root” directory of your web content.

In simple terms, the DocumentRoot is the folder (directory) on a web server that is set to be publicly accessible on the Internet.

Apache Configuration Files Vary by Linux Distribution

The location and configuration of the DocumentRoot in Apache differ depending on the Linux distribution.

  • Ubuntu/Debian: DocumentRoot is typically set in /etc/apache2/sites-available/000-default.conf (often /var/www/html by default). Site configurations are enabled via symbolic links in /etc/apache2/sites-enabled/.
  • CentOS/RHEL/Fedora: DocumentRoot is defined in /etc/httpd/conf/httpd.conf (commonly /var/www/html). Additional site-specific configurations can be placed in /etc/httpd/conf.d/.
  • SUSE/OpenSUSE: DocumentRoot is configured in /etc/apache2/vhosts.d/ for virtual hosts, with the default usually /srv/www/htdocs.

In short, the path and method of setting DocumentRoot vary by distribution, so it’s important to check the distribution-specific Apache configuration files.

Editing the File Where DocumentRoot Is Set

Below is a summary of how to edit the file where DocumentRoot is configured.

First, Check the Configuration

First, check the contents of the configuration file with the following command:

# cat /etc/<distribution-specific-path> | grep DocumentRoot

For example:

  • Ubuntu/Debian:
# cat /etc/apache2/sites-available/000-default.conf | grep DocumentRoot
  • CentOS/RHEL/Fedora:
# cat /etc/httpd/conf/httpd.conf | grep DocumentRoot
  • SUSE/OpenSUSE:
# cat /etc/apache2/vhosts.d/<vhost-file>.conf | grep DocumentRoot

This will show the line where DocumentRoot is currently set.

Create a Backup of the Configuration File

Next, create a backup of the original configuration file with the following command:

# cp -p <distribution-specific-config-file> <backup-file-name>

For example:

  • Ubuntu/Debian:
# cp -p /etc/apache2/sites-available/000-default.conf 000-default.conf_bk
  • CentOS/RHEL/Fedora:
# cp -p /etc/httpd/conf/httpd.conf httpd.conf_bk
  • SUSE/OpenSUSE:
# cp -p /etc/apache2/vhosts.d/<vhost-file>.conf <vhost-file>.conf_bk

This ensures you can restore the original settings if anything goes wrong.

Edit the File Using the vi Command

Next, edit the configuration file using the following command:

# vi /etc/<distribution-specific-config-file>

For example:

  • Ubuntu/Debian:
# vi /etc/apache2/sites-available/000-default.conf
  • CentOS/RHEL/Fedora:
# vi /etc/httpd/conf/httpd.conf
  • SUSE/OpenSUSE:
# vi /etc/apache2/vhosts.d/<vhost-file>.conf

This opens the file in vi so you can modify the DocumentRoot setting.

Change the DocumentRoot line (for example, DocumentRoot /var/www/) to the directory you want to use.

For example, if you want the DocumentRoot to be /home/user/public_html, edit the line like this:

DocumentRoot /home/user/public_html

Make sure the directory exists and has the correct permissions so that Apache can serve files from it.

The DocumentRoot Parameter Sets the Directory Accessible from a Browser

By properly configuring the DocumentRoot, you can set up access to subdomains or subdirectories from a browser. Understanding DocumentRoot is a fundamental skill for any engineer.