Apache uses the DocumentRoot directive to identify which directory holds the files it serves for each domain or VirtualHost. By default, distributions such as Ubuntu set this to /var/www/html, though other layouts may be used depending on the platform or organizational requirements. Understanding how to customize this directory allows for more flexible web server configurations.

Many Linux systems store the main Apache configuration in /etc/apache2 or /etc/httpd, and each VirtualHost references the DocumentRoot path. Changing this setting often requires adjusting file permissions, SELinux contexts, and directory ownership. Properly configured permissions help avoid common errors such as forbidden messages or incomplete responses.

Administrators can benefit from moving the DocumentRoot to a path that aligns with deployment workflows, user home directories, or external storage locations. Consistent ownership and correct Apache group settings ensure uninterrupted operation. Keeping security policies in mind is crucial when specifying new directories, especially on Linux distributions that enforce mandatory access control.

Steps to change DocumentRoot location for Apache VirtualHost:

  1. Open the virtual host configuration file in your text editor.
    $ sudo vi /etc/apache2/sites-enabled/000-default.conf
    [sudo] password for user:
  2. Locate the DocumentRoot directive and change its path to the new directory.
    <VirtualHost *:80>
            ServerAdmin webmaster@localhost
            DocumentRoot /home/user/website
            ErrorLog ${APACHE_LOG_DIR}/error.log
            CustomLog ${APACHE_LOG_DIR}/access.log combined
    </VirtualHost>
  3. Add a Directory block for the new path, setting the required permissions.
    <VirtualHost *:80>
            ServerAdmin webmaster@localhost
            DocumentRoot /home/user/website
            ErrorLog ${APACHE_LOG_DIR}/error.log
            CustomLog ${APACHE_LOG_DIR}/access.log combined
     
            <Directory /home/user/website/>
                    #Options Indexes FollowSymLinks
                    #AllowOverride None
                    Require all granted
            </Directory>
    </VirtualHost>
  4. Ensure Apache has ownership of the new directory.
    $ sudo chown --recursive www-data:www-data /home/user/website/

    Make sure Apache has read and execute permission to the DocumentRoot folder along with its parent folder.
    Related: How to change file and folder permissions in Linux

  5. Update the SELinux context if your distribution uses SELinux.
    $ sudo semanage fcontext -a -t httpd_sys_rw_content_t "/home/user/website(/.*)?"

    This applies to distributions that implements SELinux such as CentOS, RHEL and Fedora Use httpd_sys_content_t instead for readonly access for Apache on the folder.

  6. Restart or reload the Apache service to apply the changes.
    $ sudo systemctl restart apache2 # Ubuntu, Debian, openSUSE and SLES
    $ sudo systemctl restart httpd # CentOS and Red Hat
  7. Verify the configuration by accessing the site.
    $ curl 127.0.0.1
    Hi, I'm located in /home/user/website
Discuss the article:

Comment anonymously. Login not required.