How To Move Apache Document Root to a New Location on Ubuntu 18.04

how to move apache document root to new location

By default, Apache web server’s files for each website are located at /var/www/html. It’s also called the web root or document root location of Apache web server. However, sometimes, you might need to move your files to a different location, or host multiple websites from the same Apache installation. In such cases, you can store each website’s document root as a separate folder in a different location. Here’s how you can move Apache document root to a new location on Ubuntu 18.04


How To Move Apache Document Root to a New Location on Ubuntu 18.04

Before you begin, make sure that you have installed Apache on Ubuntu 18.04 and have a new document root location ready to use.


1. Copy Files to a New Location

By default, Apache’s document root is located at /var/www/html . If you are not sure about it, you can find it with the following command

$ sudo grep -R "Document Root" /etc/apache2/sites-enabled

This will give you to document root location

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


Let’s say your new location is /home/ubuntu/new_site. Copy your files from your old location to the new one

$ sudo rsync -av /var/www/html /home/ubuntu/new_site


This will copy all the files and directories from /var/www/html to /home/ubuntu/new_site


2. Update Server Configuration Files

Next you need to update Apache’s server configuration files. Basically, you need to replace all instances of /var/www/html to /home/ubuntu/new_site. Open the default config file (000-default.conf) using your favorite text editor. If you have renamed it to something else, please use the correct filename in the command below.

$ sudo gedit /etc/apache2/sites-enabled/000-default.conf


Look for the line that begins with DocumentRoot and replace /var/www/html with /home/ubuntu/new_site/html

If your website is, your 000-default.conf will look something like this

<VirtualHost *:80>
 ServerAdmin sammy@example.comn
 DocumentRoot /home/ubuntu/new_site/html
 ErrorLog ${APACHE_LOG_DIR}/error.log
 CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine on
RewriteCond %{SERVER_NAME} [OR]
RewriteCond %{SERVER_NAME}
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]


Next you need to update apache2.conf file. You will find it in /etc/apache2

$ sudo gedit /etc/apache2/apache2.conf


Scroll down to the bottom. You’ll see a block

<Directory /var/www/>
 Options Indexes FollowSymLinks
 AllowOverride None
 Require all granted

Replace /var/www with /home/ubuntu/new_site/html

Now this block will look something like

<Directory /home/ubuntu/new_site/html>
 Options Indexes FollowSymLinks
 AllowOverride None
 Require all granted


3. Test the new configuration

Next, we need to test the configuration

$ sudo apachectl configtest

If there are no errors, you’ll see a message “Syntax OK”. Occasionally, you might see some warnings, which can be ignored.
In case of errors, you will see their details listed.
Next, you need to change the ownership of your new location

$ sudo chown www-data:www-data -R /home/ubuntu/Images

This will allow Apache to read, write and execute from the new location

Finally, restart your Apache web server to apply the changes

$ sudo systemctl restart apache2

By following the above steps, you will be able to move Apache document root to a new folder or even a new mount.

About Sreeram Sreenivasan

Sreeram Sreenivasan is the Founder of Ubiq, a business dashboard & reporting platform for small & medium businesses. Ubiq makes it easy to build business dashboards & reports for your business. Try it for free today!