How to Move the Nginx Web Root to a New Location on Ubuntu 26.04 LTS
Relocate an Nginx document root to another directory or volume on Ubuntu 26.04 LTS: copy files, check www-data access, update root, test and reload.
The Nginx packages on Ubuntu serve the default site from /var/www/html, and most setups keep additional sites under /var/www as well. When a site outgrows the root filesystem, or you want each site on its own volume, you can move its files and point the root directive at the new place. This guide does that on Ubuntu 26.04 LTS (Nginx 1.28). The procedure is the same on Ubuntu 24.04 LTS, which ships Nginx 1.24.
At the end, example.com is served from /mnt/data/example.com/html, the configuration has passed Nginx's own test, and the old directory can be deleted.
Requirements
- An Ubuntu 26.04 LTS or 24.04 LTS server with Nginx installed from the Ubuntu repositories and a working site.
- A user with
sudorights (the examples usedeploy). - The new location mounted and ready. The examples use
/mnt/data. A separate disk or volume must have an entry in/etc/fstab, or it will be missing after the next reboot.
The examples assume the site currently lives in /var/www/example.com/html. Replace the domain and both paths with your own values.
Step 1: Find the current web root
Look for root directives in the enabled sites:
grep -Rn "root" /etc/nginx/sites-enabled
Example output:
/etc/nginx/sites-enabled/example.com:8: root /var/www/example.com/html;
/etc/nginx/sites-enabled/default:41: root /var/www/html;
Ignore commented lines and lines that merely contain the word, such as fastcgi_param DOCUMENT_ROOT. If your configuration is spread over other directories, let Nginx print everything it loads and search that instead:
sudo nginx -T | grep -nE "^\s*(root|alias)\s"
Write down which file holds the root line for the site you are moving.
Step 2: Copy the files to the new location
Create the parent directory and copy the site with rsync. The -a option keeps owners, permissions, timestamps and symbolic links intact:
sudo mkdir -p /mnt/data/example.com
sudo rsync -av /var/www/example.com/html /mnt/data/example.com/
Leave out the trailing slash after html. rsync then copies the directory itself and you end up with /mnt/data/example.com/html. If the application keeps writing files (uploads, caches), repeat the command right before you reload Nginx in step 5 to pick up the latest changes.
Step 3: Check permissions for the Nginx user
On Ubuntu the Nginx worker processes run as www-data (the user directive at the top of /etc/nginx/nginx.conf). The workers need read permission on the files and search (execute) permission on every directory above them. Show the permissions of each path component:
namei -l /mnt/data/example.com/html/index.html
And test access as the worker user:
sudo -u www-data test -r /mnt/data/example.com/html/index.html && echo readable
If nothing is printed, one of the directories is closed to other users. Mount points are often created that way. Allow traversal without making the contents listable:
sudo chmod o+x /mnt/data /mnt/data/example.com
If PHP-FPM or another application server reads the same files, it needs the same access, and its own configuration may also contain the old path.
Step 4: Change the root directive
Edit the site's server block:
sudo nano /etc/nginx/sites-available/example.com
Replace the old path. A site with a Let's Encrypt certificate usually has one server block that serves content on port 443 and a second one that only redirects port 80, so there is normally a single line to change:
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name example.com www.example.com;
root /mnt/data/example.com/html;
index index.html;
# ssl_certificate lines and the rest of the block stay unchanged
location / {
try_files $uri $uri/ =404;
}
}
Unlike Apache, Nginx has no separate access list of allowed directories, so nothing else is required for the path itself. Do check the rest of the file for the old directory, though: alias directives, a root inside individual location blocks, and included snippets may all contain it.
sudo nginx -T | grep -n "/var/www/example.com"
The command should print nothing once every reference has been updated.
Step 5: Test the configuration and reload Nginx
Always test before reloading. A syntax error in one site file prevents the whole server from loading its configuration:
sudo nginx -t
Expected result:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Then apply the change. A reload starts new worker processes with the new configuration and lets the old ones finish their current requests:
sudo systemctl reload nginx
Step 6: Verify the move
Place a file in the new directory only and fetch it through the web server:
echo "new root" | sudo tee /mnt/data/example.com/html/moved.txt
curl -s https://example.com/moved.txt
The response new root proves Nginx is reading from the new location. Remove the test file afterwards:
sudo rm /mnt/data/example.com/html/moved.txt
Click through the site and keep an eye on /var/log/nginx/error.log. When everything works and you have a backup, delete the old copy:
sudo rm -rf /var/www/example.com/html
AppArmor and Let's Encrypt
AppArmor is active on Ubuntu, but the Nginx packages do not install a profile for /usr/sbin/nginx, so a standard installation is not restricted to particular directories. Confirm it on your server:
sudo aa-status | grep -i nginx
If this prints a profile (because you or a hardening tool added one), that profile must allow reading the new path. Add a rule such as /mnt/data/example.com/html/** r, to the profile file in /etc/apparmor.d/ and reload it with sudo apparmor_parser -r followed by the file name.
Certificates issued by Certbot with the webroot method store the old directory in /etc/letsencrypt/renewal/example.com.conf. Correct the path there and run a renewal test:
sudo certbot renew --dry-run
Certificates obtained with certbot --nginx are not tied to a web root and need no change.
Troubleshooting
- 403 Forbidden and "(13: Permission denied)" in the error log:
www-datacannot enter one of the parent directories or read the file. Go back to step 3. - 403 Forbidden and "directory index of ... is forbidden": the directory is reachable but contains no index file. Usually the copy landed one level too deep or too high; compare the path in
rootwithls /mnt/data/example.com/html. - 404 Not Found for everything: Nginx builds the file path by appending the request URI to the
rootvalue. Check for a typo and for a leftoverrootin alocationblock that overrides the one you changed. - Site empty after a reboot: the volume was not mounted. Check with
findmnt /mnt/dataand fix/etc/fstab.
Frequently asked questions
Should root be set in the server block or inside location blocks?
Set it once in the server block. Every location inherits it, and you have a single line to change next time. A root inside a location is only needed when that part of the site really lives somewhere else.
What is the difference between root and alias?
With root, the full request URI is appended to the path: root /mnt/data/site; serves /img/a.png from /mnt/data/site/img/a.png. With alias, the part of the URI that matched the location is replaced by the given path. Use alias only when the URL prefix and the directory name differ.
Can each site be on a different volume?
Yes. Every server block has its own root, so you can mount one volume per site or per customer and repeat this procedure for each. It also makes it easy to grow or move one site's storage without touching the others.
Does the move affect file uploads or caches written by the application?
Not if you copied with rsync -a as root, because owners and modes are preserved. If the application runs under PHP-FPM, check its pool configuration and any absolute paths stored in the application's own settings.