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.
4 Responses
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
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.
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
Hi Susan. Please watch this video to get yourself up to speed with how to use the vim text editor https://youtu.be/uE4aljoMBeg