How To Change The Apache Web Root To Another Directory on an Ubuntu 18.04 VPS or Dedicated Server

How To Change The Apache Web Root To Another Directory on an Ubuntu 18.04 VPS or Dedicated Server

Introduction

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
html/
html/index.html

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
    ErrorLog${APACHE_LOG_DIR}/error.log
    CustomLog${APACHE_LOG_DIR}/access.log combined
RewriteEngineon
RewriteCond%{SERVER_NAME} =www.mydomain.com [OR]
RewriteCond%{SERVER_NAME} =mydomain.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

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
</Directory>

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
. . .
</VirtualHost>
</IfModule>

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 127.0.1.1. 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

Conclusion

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

Check out these top 3 Linux hosting services

Webdock
$0.95 /mo
Starting price
Visit Webdock
Rating based on expert review
  • User Friendly
    3.8
  • Support
    4.5
  • Features
    4.5
  • Reliability
    4.3
  • Pricing
    4.3
Kamatera
$4.00 /mo
Starting price
Visit Kamatera
Rating based on expert review
  • User Friendly
    3.5
  • Support
    3.0
  • Features
    3.9
  • Reliability
    4.0
  • Pricing
    4.3
Ultahost
$2.50 /mo
Starting price
Visit Ultahost
Rating based on expert review
  • User Friendly
    4.3
  • Support
    4.8
  • Features
    4.5
  • Reliability
    4.0
  • Pricing
    4.8
  • Do you need the best VPS? Read about our different offers.

How to Install Apache Cassandra on an Ubuntu 18.04 VPS or Dedicated Server

This tutorial will help you install and configure Apache Cassandra (an opensourc
less than a minute
Max Ostryzhko
Max Ostryzhko
Senior Web Developer, HostAdvice CTO

How to Host Multiple Websites on an Ubuntu 18.04 VPS or Dedicated Server

This article will show you how to host two or more websites on a single Virtual
less than a minute
Idan Cohen
Idan Cohen
Marketing Expert

How to Enable Apache Mod_Rewrite on an Ubuntu 18.04 VPS or Dedicated Server

In this tutorial, we will cover the basics of enabling mod_rewrite on an Ubuntu
less than a minute
Max Ostryzhko
Max Ostryzhko
Senior Web Developer, HostAdvice CTO

How to Configure Nginx and Apache on the same Ubuntu VPS or Dedicated Server

Nginx and Apache are great and powerful web servers. However, they both have dra
less than a minute
Idan Cohen
Idan Cohen
Marketing Expert
HostAdvice.com provides professional web hosting reviews fully independent of any other entity. Our reviews are unbiased, honest, and apply the same evaluation standards to all those reviewed. While monetary compensation is received from a few of the companies listed on this site, compensation of services and products have no influence on the direction or conclusions of our reviews. Nor does the compensation influence our rankings for certain host companies. This compensation covers account purchasing costs, testing costs and royalties paid to reviewers.
Click to go to the top of the page
Go To Top