Skip to main content

[Beginners] How To Move Apache's Root Directory To A New Location

Updated over a week ago

Introduction

By default on Ubuntu the Apache2 web server stores its files in "/var/www/html" folder. Sometimes it’s helpful to move it to another location, such as a separate mounted file system.

In this tutorial, you will learn how to move the default directory to a new location. We are using Ubuntu 22.04 for this tutorial; however, you should be able to follow this guide using any of our offered Ubuntu or Debian distributions.

Copying Files

We are using "rsync" for copying files to a new location:

rsync -av /var/www/html /new/location

Where the "-a" flag preserves the permissions and other directory properties, and the "-v" flag provides comprehensive output.

Note: If you are using "Tab" completion, be sure to delete the trailing slash "/" on the directory.

Updating The Configuration File

Now we need to update the "000-default.conf" file:

Now we will find the line that begins with "DocumentRoot" and update it with the new location. It should look something like this:

# The ServerName directive sets the request scheme, hostname, and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com

ServerAdmin webmaster@localhost
DocumentRoot /new/directory

# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example, the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf

# vim: syntax=apache ts=4 sw=4 sts=4 sr note

Restarting Apache

Once you’ve finished the configuration changes, you can make sure the syntax is correct with "configtest":

sudo apachectl configtest

If everything is right, you should get the output:

As long as you did not receive a syntax error, restart the web server. Otherwise, track down and fix the problems it reported. Use the following command to restart Apache:

sudo systemctl reload apache2

Conclusion

That's it! You changed Apache's default folder, and from now on, all your affected sites will be loaded from the new location.

Did this answer your question?