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


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
    DocumentRoot /home/test 

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.

About The Author

With a strong software engineering background, Tony is determined to help as many people as possible start their online busines. Discover why Tony quit his hedge fund job to pursue this mission. You can send Tony a message here.

Leave a Comment