DocumentRoot defines the folder for which the content of your website is located in the server. It could be set in the main configuration for the default website or it could be set in a VirtualHost to set the folder specific to the website or virtual servers that you're hosting.

You can change the DocumentRoot location for you website by changing either in the main configuration file or from withing the VirtualHost configuration.

Steps to change DocumentRoot location for Apache website:

  1. Edit Apache configuration using your preferred text editor.
    $ sudo vi /etc/apache2/sites-enabled/000-default.conf
    [sudo] password for user:
  2. Set the DocumentRoot value in main or VirtualHost configuration to the folder that you prefer.
    <VirtualHost *:80>
            ServerAdmin webmaster@localhost
            DocumentRoot /home/user/website
            ErrorLog ${APACHE_LOG_DIR}/error.log
            CustomLog ${APACHE_LOG_DIR}/access.log combined
    </VirtualHost>
  3. Make sure Apache has the correct permission to the folder.
    $ sudo chown --recursive www-data:www-data /home/user/website/
  4. Make sure Directory option is setup properly for the folder.
    <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>
  5. Change SELinux context if required.
    $ 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 Apache for the changes to take effect.
    $ sudo systemctl restart apache2 # Ubuntu and Debian
    $ sudo systemctl restart httpd # CentOS and Red Hat
  7. Access the website to test if the changes was successful.
    $ curl 127.0.0.1
    Hi, I'm located in /home/user/website
Discuss the article:

Comment anonymously. Login not required.

Share!