how to move nginx web root to new location

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

When you migrate your website to new location, you need to change NGINX root directory configuration so that NGINX server can serve requests from the new location. Let’s look at how to move NGINX web root to new location in Ubuntu, as well as CentOS.

 

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

By default, NGINX webroot is located at /var/www/html. If you need to move your website documents and content to a different location, then you will need to move NGINX default root directory to new location and make your server point to this location. You can use these steps to move NGINX web root to new location in Ubuntu (Debian) as well as CentOS (RPM) systems.

After you move NGINX web root to new location, you may want to use a reporting tool to monitor the key metrics about your website/application such as signups, traffic, sales, revenue, etc. using dashboards & charts, to ensure everything is working well and spot issues quickly.

 

1. Copy or Move NGINX Root Directory Content

First, we will move all the files & folders located at default NGINX web root location /var/www/html to its new location, say, /mnt/website

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

After this, you will find html folder at /mnt/website as /mnt/website/html

Bonus Read: How to Disable NGINX Cache

 

2. Update NGINX configuration

Next, we need to tell NGINX to serve files from the new location, for your website. So we will update server block in default NGINX site configuration file, located at, /etc/nginx/sites-available/default

Open the file using a text editor

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

 

Update the line that starts with root and change its location to /mnt/website/html shown in bold

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

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

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

If your site configuration is located in a different file (e.g /etc/nginx/sites-available/website) then update that file, instead. Also, if you have multiple configuration files at /etc/nginx/sites-available update them all, as described above.

Bonus Read : How to Redirect 404 to Home Page in NGINX

 

3. Restart NGINX web server

Check NGINX configuration

$ sudo nginx -t

 

If you don’t get any error message, restart NGINX server.

$ sudo service nginx reload #debian/ubuntu
$ systemctl restart nginx #redhat/centos

 

That’s it! Now NGINX will serve website requests from new location.

By the way, if you want to create charts & dashboards to monitor your business or website, you can try Ubiq. We offer a 14-day free trial.