Tutorial

How To Move an Apache Web Root to a New Location on Ubuntu 20.04

How To Move an Apache Web Root to a New Location on Ubuntu 20.04
Not using Ubuntu 20.04?Choose a different version or distribution.
Ubuntu 20.04

Introduction

On Ubuntu, the Apache web server serves documents stored in the var/www/html directory by default. This directory is referred to as the document root. When an administrator or user makes a request to the server, it will respond with the appropriate files from the document root.

This directory is typically located on the root filesystem with the rest of the operating system. Sometimes, though, it’s helpful to move the document root to another location, such as a separate mounted filesystem. For example, if you serve multiple websites from the same Apache instance, putting each site’s document root on its own volume allows you to scale in response to the needs of a specific site or client.

In this guide, you will move an Apache document root from its default location of var/www/html to a new location.

Prerequisites

To complete this guide, you will need:

  • An Ubuntu 20.04 server and a non-root user with sudo privileges. You can learn more about how to set up a user with these privileges in our Initial Server Setup with Ubuntu 20.04 guide.
  • Apache installed on your server. Learn how to set this up by following our How To Install the Apache Web Server on Ubuntu 20.04 guide.
  • SSL configured for your domain. Configure this by following our How To Secure Apache with Let’s Encrypt on Ubuntu 20.04 guide.
  • A new location for your document root. In this tutorial,/mnt/volume_nyc3_01 is used as the directory for this new location. Although the instructions in this guide use the example of a mounted block storage device, you can use any directory location on your system as a new document root to serve your web content.

If you are using a Block Storage Volume from DigitalOcean, this guide will show you how to create and attach your volume. Your new document root location is configurable based on your needs. If you are moving your document root to a different storage device, you will want to select a location under the device’s mount point.

Step 1 — Copying Files to the New Location

If you followed the prerequisite tutorials, you will have created a new document root at /var/www/your_domain. You may also have additional document roots in corresponding VirtualHost directives. It is important to establish the location of your current document root before you copy the relevant files to their new location.

Search for the location of your document roots by using the grep command to search in the /etc/apache2/sites-enabled directory. This limits the focus to active sites. The -R flag ensures that grep will print both the DocumentRoot and the full filename in the output:

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

The following output reveals the location of your current DocumentRoot:

Output
/etc/apache2/sites-enabled/your_domain-le-ssl.conf: DocumentRoot /var/www/your_domain /etc/apache2/sites-enabled/your_domain.conf: DocumentRoot /var/www/your_domain

If you have preexisting setups, your results may differ from what’s shown here. In either case, you can use the output from grep to make sure you’re moving the desired files and updating the appropriate configuration files.

Now that you’ve confirmed the location of your document root, copy the files to their new location with rsync:

Note: When using the rsync command, please be mindful of two things:

  • Notice that there is a trailing slash after your_domain. If you don’t include the trailing slash here, you are copying over this directory to the new document root as a sub-directory. For example, your new document root will have this structure: /var/www/mnt/volume_nyc3_01/your_domain. This will cause an issue when trying to serve your index.html file from your new document root.
  • Second, there is no trailing slash on the new document root directory /mnt/volume_nyc3_01.
  1. sudo rsync -av /var/www/your_domain/ /mnt/volume_nyc3_01

The -a flag preserves the permissions and other directory properties, while the -v flag provides verbose output so you can follow the progress of the sync.

Your output should include the following:

Output
sending incremental file list ./ index.html sent 265 bytes received 38 bytes 606.00 bytes/sec total size is 134 speedup is 0.44

With your files in place, you can move on to modifying your Apache configuration to reflect these changes.

Step 2 — Updating the Configuration Files

After locating and copying files to the new document root, you can configure the virtual host files to point to this new location.

Start by opening /etc/apache2/sites-enabled/your_domain.conf with your preferred editor. This example uses nano:

  1. sudo nano /etc/apache2/sites-enabled/your_domain.conf

Find the line that begins with DocumentRoot and replace it with the new root location. In this example, the new root location is /mnt/volume_nyc3_01:

/etc/apache2/sites-enabled/your_domain.conf
<VirtualHost *:80>
    ServerAdmin sammy@your_domain
    ServerName your_domain
    ServerAlias www.your_domain
    DocumentRoot /mnt/volume_nyc3_01
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine on
RewriteCond %{SERVER_NAME} =www.your_domain [OR]
RewriteCond %{SERVER_NAME} =your_domain
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

After this <VirtualHost> directive, add these highlighted lines to ensure that the server will follow the symbolic links in the directory:

/etc/apache2/sites-enabled/your_domain.conf
. . .
</VirtualHost>

<Directory /mnt/volume_nyc3_01>
    Options FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

Altogether, your /etc/apache2/sites-enabled/your_domain.conffile should include all of the following lines specific to your new DocumentRoot location:

/etc/apache2/sites-enabled/your_domain.conf
<VirtualHost *:80>
    ServerAdmin sammy@your_domain
    ServerName your_domain
    ServerAlias www.your_domain
    DocumentRoot /mnt/volume_nyc3_01
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine on
RewriteCond %{SERVER_NAME} =www.your_domain [OR]
RewriteCond %{SERVER_NAME} =your_domain
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

<Directory /mnt/volume_nyc3_01>
    Options FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

Save and exit out of your editor. You can exit out of nano by pressing CTRL + X, then Y, and then ENTER.

After making these changes, you can turn your attention to the SSL configuration. Open the /etc/apache2/sites-enabled/your_domain-le-ssl.conf file:

  1. sudo nano /etc/apache2/sites-enabled/your_domain-le-ssl.conf

Like the previous configuration file, modify the DocumentRoot to reflect the new location:

/etc/apache2/sites-enabled/your_domain-le-ssl.conf
<IfModule mod_ssl.c>
<VirtualHost *:443>
    ServerAdmin sammy@your_domain
    ServerName your_domain
    ServerAlias www.your_domain
    DocumentRoot /mnt/volume_nyc3_01
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
. . .
</VirtualHost>
</IfModule>

Save and close this file.

You have now made the necessary configuration changes to reflect the new location of your document root.

Step 3 — Restarting Apache

Once you’ve finished making the changes, you can restart Apache and test the results.

First, make sure the syntax of your new configurations are right with configtest:

  1. sudo apachectl configtest

If your syntax is correct, your output should reveal the following:

Output
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

Note: If you want to suppress the top line in this output, add a ServerName directive to your global Apache configuration file at /etc/apache2/apache2.conf. The ServerName can be your server’s domain or IP address.

Open the file with your editor:

  1. sudo nano /etc/apache2/apache2.conf

Place the ServerName directive at the bottom of the configuration file with your_domain or your server’s IP address:

/etc/apache2/apache2.conf
# This is the main Apache server configuration file.  It contains the
# configuration directives that give the server its instructions.
# See http://httpd.apache.org/docs/2.4/ for detailed information about
# the directives and /usr/share/doc/apache2/README.Debian about Debian specific
# hints.
...

ServerName your_domain

Save and exit out of your editor.

This is just a message, however, and doesn’t affect the functionality of your site. As long as the output contains Syntax OK, you are ready to continue.

Use the following command to restart Apache:

  1. sudo systemctl reload apache2

When the server has restarted, visit your affected sites to ensure that they’re working as expected. Once you’re comfortable that everything is in order, don’t forget to remove the original copies of the data:

  1. sudo rm -Rf /var/www/your_domain

You have now successfully moved your Apache document root to a new location.

Conclusion

In this tutorial, you learned how to change the Apache document root to a new location. This can help you with basic web server administration like effectively hosting multiple sites on a single server. It also allows you to take advantage of alternative storage devices such as network block storage, which can be helpful in scaling a web site as its needs change.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us


About the authors


Default avatar
Kong Yang

author


Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
Leave a comment


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel