How To Move Nginx Web Root to a New Location on Ubuntu 18.04


how to move nginx web root to new location on ubuntu

By default, Nginx web server stores your website’s documents at /var/www/html. While this is fine for small websites, you may soon run out of space if your website is growing rapidly, as it is on root filesystem. In this case, you will have to move your website to a larger location to handle your content. Here’s how you can move Nginx Web Root to a new location on Ubuntu 18.04.

 

 

How To Move Nginx Web Root to a New Location on Ubuntu 18.04

Here are the steps to migrate Nginx document root folder to a new location.

 

1. Copy Nginx Root Content

By default, Nginx stores all your website’s content at /var/www/html. Assuming that you have mounted a larger storage space at /mnt/volume1 use the following command to copy the files & directories from your default web root location to the new one.


$ sudo rsync -av /var/www/html /mnt/volume1

You’ll find the html folder at the new location /mnt/volume1/html

 

2. Update Nginx Config files

Next, you need to point Nginx to the new location by updating its configuration file. You will find it at /etc/nginx/sites-available. Open the default config file in a text editor


$ sudo nano /etc/nginx/sites-available/default

 

Update the part in bold to be /mnt/volume1/html


server {
listen 80;
listen [::]:80;
 root /mnt/volume-mn1/html;
index index.php index.html index.htm;
server_name example.com www.example.com;

location / {
try_files $uri $uri/ /index.php?$args;
}

......
......

 

If you have multiple config files in the directory, please do this for each of them

 

 

3. Restart Nginx Server

Run the following command to test Nginx configuration


$ sudo nginx -t

 

If you don’t see any error message, run the following command to restart Nginx Server.


$ sudo systemctl reload nginx.service

 

 

That’s It! Nginx should serve your website content from its new Document Root. Now you know how to migrate Nginx Web Root to a New Location on Ubuntu

About Sreeram Sreenivasan

Sreeram Sreenivasan is the Founder of Ubiq, a business dashboard & reporting platform for small & medium businesses. Ubiq makes it easy to build business dashboards & reports for your business. Try it for free today!