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.
To complete this guide, you will need:
/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.
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:
- 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:
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./mnt/volume_nyc3_01
.- 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:
Outputsending 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.
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
:
- 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
:
<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:
. . .
</VirtualHost>
<Directory /mnt/volume_nyc3_01>
Options FollowSymLinks
AllowOverride None
Require all granted
</Directory>
Altogether, your /etc/apache2/sites-enabled/your_domain.conf
file should include all of the following lines specific to your new DocumentRoot
location:
<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:
- sudo nano /etc/apache2/sites-enabled/your_domain-le-ssl.conf
Like the previous configuration file, modify the DocumentRoot
to reflect the new location:
<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.
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
:
- sudo apachectl configtest
If your syntax is correct, your output should reveal the following:
OutputAH00558: 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:
- 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:
# 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:
- 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:
- sudo rm -Rf /var/www/your_domain
You have now successfully moved your Apache document root to a new location.
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.
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!
Sign up for Infrastructure as a Newsletter.
Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.