Operating System Ubuntu

How to Change the Apache Document Root on Ubuntu 26.04 LTS

Move an Apache web root to another directory or volume on Ubuntu 26.04 LTS: copy the files, grant access, update the virtual hosts, test and reload.

Apache on Ubuntu serves files from /var/www/html unless a virtual host says otherwise. That directory sits on the root filesystem, so a growing site competes for space with the operating system. This guide moves a site's document root to another directory, for example a separately mounted data volume, on Ubuntu 26.04 LTS. The steps are identical on Ubuntu 24.04 LTS; only the Apache version differs (2.4.66 on 26.04, 2.4.58 on 24.04).

At the end, Apache serves example.com from /mnt/data/example.com/html, the configuration has been tested, and the old copy can be removed safely.

Requirements

  • An Ubuntu 26.04 LTS or 24.04 LTS server with the apache2 package installed and a working site.
  • A user with sudo rights (the examples use deploy).
  • The new location already mounted. The examples use /mnt/data. If it is a separate disk or volume, make sure it is listed in /etc/fstab so it is mounted again after a reboot; otherwise Apache will start and serve an empty directory.

The examples assume the site currently lives in /var/www/example.com/html. Replace example.com and both paths with your own.

Step 1: Find the current document root

Search the enabled virtual hosts so you only look at sites Apache is really serving:

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

Typical output for a site with a Let's Encrypt certificate:

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

Note every file that is listed. The HTTP and HTTPS virtual hosts are separate files and both have to be changed. If the only result is 000-default.conf with /var/www/html, you are moving the default site and that is the file to edit later.

Step 2: Copy the files to the new location

Create the parent directory and copy the site with rsync. The -a option preserves owners, permissions, timestamps and symbolic links:

sudo mkdir -p /mnt/data/example.com
sudo rsync -av /var/www/example.com/html /mnt/data/example.com/

There is no trailing slash after html on purpose: rsync then copies the directory itself, so the result is /mnt/data/example.com/html. With a trailing slash only the contents would be copied into /mnt/data/example.com.

If the site is being written to (uploads, a CMS), run the same rsync command once more just before you switch Apache over, so the copy is current.

Step 3: Check that Apache can read the new directory

Apache's worker processes run as the user and group www-data. That user needs read permission on the files and search (execute) permission on every directory in the path, starting at /. A restrictive mount point is the most common reason for a 403 error after a move. List the permissions along the whole path:

namei -l /mnt/data/example.com/html/index.html

Then test as the Apache user:

sudo -u www-data test -r /mnt/data/example.com/html/index.html && echo readable

If the test prints nothing, open up the directory that blocks access. For example, when the mount point itself is closed to other users:

sudo chmod o+x /mnt/data /mnt/data/example.com

Avoid home directories as a web root. On current Ubuntu releases a new user's home directory is not accessible to other users, so www-data cannot enter it without loosening permissions you probably want to keep.

Step 4: Update the virtual host

Open the first file found in step 1:

sudo nano /etc/apache2/sites-available/example.com.conf

Change DocumentRoot and add a <Directory> block for the new path inside the <VirtualHost> section:

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /mnt/data/example.com/html

    <Directory /mnt/data/example.com/html>
        Options FollowSymLinks
        AllowOverride None
        Require all granted
    </Directory>

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

The <Directory> block is not optional. Ubuntu's /etc/apache2/apache2.conf denies access to the whole filesystem (Require all denied for /) and then grants it only for /var/www/ and /usr/share. A document root anywhere else returns 403 Forbidden until you grant access explicitly. Write DocumentRoot without a trailing slash, and set AllowOverride All instead of None if the site depends on .htaccess files.

Repeat the change in the HTTPS virtual host (example.com-le-ssl.conf in the example). While you are in these files, update any other line that contains the old path, such as Alias, ScriptAlias, rewrite rules or an existing <Directory /var/www/example.com/html> block. A final search shows whether anything was missed:

grep -R "/var/www/example.com" /etc/apache2/sites-enabled /etc/apache2/conf-enabled

Step 5: Test the configuration and reload Apache

Check the syntax before applying anything:

sudo apache2ctl configtest

The command must end with Syntax OK. If it reports that a document root does not exist, the path in the virtual host is misspelled or the volume is not mounted. Then apply the change without dropping open connections:

sudo systemctl reload apache2

Step 6: Verify the move

Create a file that exists only in the new location and request it:

echo "new root" | sudo tee /mnt/data/example.com/html/moved.txt
curl -s http://example.com/moved.txt

If the output is new root, Apache is reading from the new directory. Use https:// in the URL to check the TLS virtual host as well, then delete the test file:

sudo rm /mnt/data/example.com/html/moved.txt

Browse the site and watch /var/log/apache2/error.log for a while. Once you are satisfied, and you have a backup, remove the old copy:

sudo rm -rf /var/www/example.com/html

AppArmor and Let's Encrypt

Ubuntu enables AppArmor, but the apache2 package does not ship a profile that confines Apache, so on a standard installation there is nothing to adjust. Check whether a profile has been added on your server:

sudo aa-status | grep -i apache

No output means Apache is unconfined. If a profile is listed, it must allow read access to the new path: add a rule such as /mnt/data/example.com/html/** r, to that profile in /etc/apparmor.d/ and reload it with sudo apparmor_parser -r followed by the profile file name.

If your certificate was issued by Certbot with the webroot method, Certbot remembers the old directory in /etc/letsencrypt/renewal/example.com.conf. Update the path there and confirm that renewal still works:

sudo certbot renew --dry-run

Certificates obtained with the Apache plugin (certbot --apache) do not store a web root and need no change.

Troubleshooting

  • 403 Forbidden, log says "client denied by server configuration": the <Directory> block is missing, has a typo in the path, or was added to only one of the two virtual hosts.
  • 403 Forbidden, log mentions permissions: www-data cannot traverse a parent directory. Repeat the checks from step 3.
  • Old content still appears: another enabled virtual host still points to the old path, or the HTTPS file was not changed. Run the grep from step 1 again.
  • Empty site after a reboot: the volume was not mounted. Check findmnt /mnt/data and the entry in /etc/fstab.

Frequently asked questions

Yes. You can replace /var/www/example.com/html with a symlink to the new directory, and Apache follows it as long as Options FollowSymLinks is in effect. You still need a <Directory> block granting access to the real target path, so changing DocumentRoot is usually the clearer solution.

Is a reload enough, or do I have to restart Apache?

A reload is enough for a document root change. systemctl reload apache2 makes Apache re-read its configuration gracefully, so requests in progress finish normally. A full restart is only needed after changes such as enabling or disabling certain modules.

How do I change the web root for every site at once?

There is no global switch: each virtual host has its own DocumentRoot. What you can do once is grant access to a common parent. apache2.conf contains a commented-out <Directory /srv/> example for this; a similar block for /mnt/data/ in a file under /etc/apache2/conf-available/, enabled with a2enconf, saves you from repeating the block in every site.

Do file owners need to change after the move?

No. rsync -a run with sudo keeps owners and modes, so an application that wrote to its upload or cache directories as www-data before can still do so. Only the permissions of the new parent directories need attention.

Share story:

Continue reading other guides on How VPS.