Write Review


By default, Apache archives its files in the directory /var/www/html on Ubuntu systems. This directory typically exists in a root filesystem; a location that also hosts other operating system files.  In some cases, it may be helpful to relocate your document roots to a different location.

This guide will show you how to move the Apache web root ( /var/www/html) to a different on your Ubuntu 18.04 system.

Ready? Let’s get started!

Start Here

To accomplish this task successfully, you need the following:

  • An SSL certificate for your own domain. This guide will use mydomain.com as our domain name. Remember to substitute this value with your unique domain name.
  • A unique location for document root directory. This guide will use /mnt/volume-ha1 as the unique location for the document root.

If everything is in place, let’s get started.

Step 1 - Transfer The File To A New Location

As aforementioned, Apache Web Server uses /var/www/html as the location for the document root. Also, by implementing the prerequisite instructions, you fashioned a document root called /var/www/mydomain.com/html. This should be your main document root, but you can have other document roots located in the corresponding Apache VirtualHost directives.

Our aim here is to pinpoint the exact location of the document roots, then transfer their corresponding files to the new directory. Since we want to limit our search to only the active websites, we’ll search for the locations in the directory /etc/apache2/sites-enabled using grep. First, execute the command below to search the locations:

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

The -R enables grep to display the full file name and the document root in the output, as shown below:

/etc/apache2/sites-enabled/mydomain.com-le-ssl.conf:  DocumentRoot /var/www/mydomain.com/html
/etc/apache2/sites-enabled/mydomain.com.conf:         DocumentRoot /var/www/mydomain.com/html

The output above may differ depending on your underlying setups. Regardless of the results, it should be easy to determine if you are relocating the correct files and that you are updating the desired config files.

Once, you confirm the exact location of the document root, use rsync to copy its existing files to the new location. Execute the command below:

$ sudo rsync -av /var/www/mydomain.com/html /mnt/volume-ha1

The command features an -a flag which ensures the directory properties and permissions are preserved during the file transfer. In addition, the command features a -v flag, which delivers the verbose output to help you monitor how the synchronization progresses. This will give you an output similar to the one below:

sending incremental filelist

sent 318 bytes  received 39 bytes  714.00 bytes/sec
total size is 176  speedup is 0.49

By now, the files are in their new location and it’s time to modify Apache web server configuration to rhyme with the above changes.

Step 2 - Modifying Apache Config Files

Apache web server is built to utilize site-specific and global config files. As a result, our configuration cannot reflect the changes made unless we modify the /etc/apache2/sites-enabled/mydomain.com.conf, which is the VirtualHost configuration file for our domain (mydomain.com).

We should also modify /etc/apache2/sites-enabled/mydomain.com-le-ssl.conf; a configuration file created when the SSL certs for our domain were configured.

Note: The files we are about to modify, are the config files displayed in step one after running the grep command.

First, execute the command below to open the first configuration file:

$ sudo nano /etc/apache2/sites-enabled/mydomain.com.conf

This will give you an output with file’s content. Find a line beginning with DocumentRoot. Update the value of this line with /mnt/volume-ha1/html; which is our new location for document root. Once you update that line, you should have the following:

<VirtualHost *:80>
    ServerAdmin hostadvice@mydomain.comn
    ServerName mydomain.com
    ServerAlias www.mydomain.com
    DocumentRoot /mnt/volume-ha1/html
    CustomLog${APACHE_LOG_DIR}/access.log combined
RewriteCond%{SERVER_NAME} =www.mydomain.com [OR]
RewriteCond%{SERVER_NAME} =mydomain.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]

Next, add the directive below to this configuration file to enable the server to follow the symlinks in the new directory.

. . .
<Directory /mnt/volume-nyc3-01/html>
    Options FollowSymLinks
    AllowOverride None
    Require all granted

Point to remember: You should monitor the DocumentRoot as displayed in Step 1 after executing the grep command. Also, ensure the DocumentRoot in rewrites or aliases is updated to reflect the changes in the new location for the document root.

Once that is done, run the command below to open your SSL configuration file:

$ sudo nano /etc/apache2/sites-enabled/mydomain.com-le-ssl.conf

Update the DocumentRoot with /mnt/volume-ha1/html; the new location for document root. Make sure you have the following after the update:

<IfModule mod_ssl.c>
<VirtualHost *:443>
    ServerAdmin hostadvice@mydomain.com
    ServerName mydomain.com
    ServerAlias www.mydomain.com
    DocumentRoot /mnt/volume-ha1/html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
. . .

That is it! You have modified both config files and they are now reflecting the new document root location.

Step 3 - Implementing the Changes

Now that you all the changes in place, you can easily restart your Apache service and confirm the results. But first, run the command below to confirm the syntax:

$ sudo apachectl configtest

This will give you an output similar to the one below:

AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using Set the 'ServerName' directive globally to suppress this message
Syntax OK

To deactivate that top line, include a unique ServerName directive (your IP address or Server’s Domain) to /etc/apache2/apache2.conf.

Now, execute the command below to restart your Apache service:

$ sudo systemctl reload apache2

Once the service restarts, visit your websites and check if everything is functioning as expected. If everything is fine, run the command below to delete the native files of the sites’ data.

$ sudo rm -Rf /var/www/mydomain.com/html


You have successfully moved the document root for your Apache web server to a different location.

Check out these top 3 Linux hosting services

Was this article helpful?