How to change the default Apache2 webroot directory?

How to change the default Apache2 webroot directory?

This tutorial explains how to change default Apache2 webroot directory, from which your Apache2 webserver will load your websites.

Prerequisites: This tutorial assumes that you have already installed Apache2 webserver on your system.

  • How to install Apache2 webserver on CentOS 6, 7 tutorial can be found here.

 

1. First of all, create the new webroot directory for your websites, for example: /home/public_html/www/

Command: sudo mkdir -p /home/public_html/www/

 

2. Then, open the Apache2 webserver configuration file /etc/httpd/conf/httpd.conf and find the directive DocumentRoot "/var/www/html"

Command: sudo nano /etc/httpd/conf/httpd.conf

 

3. Then, change the default webroot location in the DocumentRoot directive

from DocumentRoot "/var/www/html"

to DocumentRoot "/home/public_html/www/"

Note: Since you have changed the directive for the default webroot directory, you might wish to change the directories for the complimenting directives as well.

 

4. Then, save and exit the configuration file and restart your Apache2 webserver for changes to take affect

Command: sudo systemctl restart httpd.service

 

5. Finally, set the correct ownership  

Command: sudo chown -R apache:apache /home/public_html/www/

Your default “It Works” page should be loading as soon as you try to open your website in your web browser.

 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.