How to Change /var/www/html Document Root in Apache

How to Move the Apache document root from /home/tonyflor/domains/tonyteaches.tech/public_html

Hey there! Some links on this page may be affiliate links which means that, if you choose to make a purchase, I may earn a small commission at no extra cost to you. I greatly appreciate your support!

In this quick tutorial, you’ll learn how to change the default Apache document root from /var/www/html/ to something else. This requires you to have root access to your server. If you have that, let’s jump into the tutorial.

Change Apache DocumentRoot

DocumentRoot allows you to specify a different location to host your Apache website. By default, DocumentRoot is set to /var/www/html/. You can see this for yourself by looking at the configuration file for your website at /etc/apache2/sites-available/ and ending in *.conf.

However, for whatever reason you want to change this to another location on your server. For this example, let’s say we want to host a website from the /home/test/ directory.

The best way to demonstrate this is with a new Apache configuration file. Create a file at  /etc/apache2/sites-available/ called test.conf and paste the following code.

<VirtualHost *:80>
    <Directory /home/test/>
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
    </Directory> 
    DocumentRoot /home/test 
</VirtualHost>

The most important line in the code above is the DocumentRoot line. This line sets the Apache document root for the website to the /home/test/ directory. The block of code between the Directory tags simply enables directory listings in Apache and is safe to ignore if you are not following this tutorial verbatim.

If you are following the tutorial, disable all other websites with a2dissite * and enable the test configuration with the a2ensite test.conf command.

When you have made the necessary changes, restart the apache service with service apache2 restart or similar.

If you did everything correctly, you how have a website or listing that is served out of a directory other than /var/www/html/!

Please let me know below if you have any questions about changing the document root in Apache.

Facebook
Twitter
Pinterest
LinkedIn
Reddit

Meet Tony

Tony from Tony Teaches Tech headshot

With a strong software engineering background, Tony is determined to demystify the web. Discover why Tony quit his job to pursue this mission. You can join the Tony Teaches Tech community here.

4 Responses

  1. I’m doing this on linux mint 19 for local development, And here is a problem am facing after following the steps.

    Forbidden

    You don’t have permission to access this resource.
    Apache/2.4.29 (Ubuntu) Server at 127.0.0.1 Port 80

    1. Hi Joe,

      This might be a permissions issue. Make sure that the “www-data” user and group owns all directories leading up to your new root. For example, you can do:
      sudo chown -R www-data:www-data /path/to/new/root
      sudo chown -R www-data:www-data /path/to/new
      sudo chown -R www-data:www-data /path/to

      Etc.

      Give that a try and let me know if it works for you.

  2. Thanks for the video you did on this. Please I am bit stuck on how you left one page to get to the previous page.
    I could not see how to enter ‘wq’ down at the bottom…What key do you use
    Thanks

    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted

    DocumentRoot /home/test

Leave a Reply

Your email address will not be published. Required fields are marked *