Change Document Root for Apache 2

On Ubuntu, by default, the Apache2 web server stores its documents in /var/www/html, which is typically located on the root file system with rest of the operating system. Sometimes, though, it’s helpful to move the document root to another location.

You can search for the location of additional document roots using grep. We’ll search in the /etc/apache2/sites-enabled directory to limit our focus to active sites. The -R flag ensures that grep will print both the DocumentRoot and the filename in its output:

grep -R "DocumentRoot" /etc/apache2/sites-enabled

The result will look something like

sites-enabled/000-default.conf DocumentRoot /var/www/html

We’ll edit the 000-default.conf file

sudo nano /etc/apache2/sites-enabled/000-default.conf

In /etc/apache2/sites-enabled/000-default.conf file we will change default /var/www/html with directory we want to use, in our case /home/user/www
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /home/user/www
</VirtualHost *:80>

We need to restart apache server, use the following command to restart Apache:
sudo service apache2 restart

You may get the 403 forbidden issue for the new directory, in that case we need to change the apache2 conf
sudo nano /etc/apache2/apache2.conf

Add following configuration to it
<Directory /home/user/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>

Also make sure the directory /home/user/www have the appropriate access.

We need to restart apache server, use the following command to restart Apache:
sudo service apache2 restart

Change Document Root for Apache 2

You May Also Like