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

Posted on

Introduction

Nginx is a powerful HTTP server designed to store documents in the directory /var/www/html (document root). This directory is found on a root filesystem with other operating system directories. Sometimes it’s important to transfer this document root (/var/www/html) to a different directory such as a separately mounted filesystem.

This comes in handy, primarily when you are serving numerous sites from one Nginx instance. By putting each document root on a different volume makes it easy to scale up with the growing needs of each website or client.

This tutorial will help you transfer the Nginx web root to another location.

Prerequisites

For this guide to follow flawlessly, you require the following:

  • Ubuntu 18.04 server
  • Nonroot user with sudo permissions
  • An SSL/TLS certificate setup for your Ubuntu 18.04 server.
  • Besides, this tutorial will make use of example.com as the domain name. Remember to substitute this domain with your unique domain name.
  • A new document root location. In our guide, we’ll utilize the directory /mnt/volume-nyc3-01, as the new location for the document root.

Step 1 – Transfering Files to Your New Location

When Nginx is installed and fully configured, /var/www/html serves as its default document location or document root. Our goal is to transfer this document root to another location /var/www/example.com/html. First, let us establish the exact location of the document roots, then transfer all the vital files to the new location.

We’ll use the grep command to see where the document roots are located. Execute the command below to search the location in the directory /etc/nginx/sites-enabled:

$ grep -R "root"/etc/nginx/sites-enabled

The command focuses the search on all the active sites. It includes an -R flag which enables grep to print both the full name and the line featuring a root directive. This will give you an output similar to the one below:

/etc/nginx/sites-enabled/example.com:           root /var/www/example.com/html;
/etc/nginx/sites-enabled/default:               root /var/www/html;
/etc/nginx/sites-enabled/default:               # deny access to .htaccess files, if Apache's document root
/etc/nginx/sites-enabled/default:#              root /var/www/example.com;

The output may differ depending on the pre-existing configurations. For this reason, it’s nice to utilize grep to ensure that you are moving right files to the new locations and that you are making changes to the desired configuration files.

Once you confirm the document root location, let use use the rsync to copy these files and move them to the new location.

$ sudo rsync -av /var/www/example.com/html /mnt/volume-nyc3-01

The command includes an -a flag which carries on the directory’s permissions and preserves other vital directory properties. On the other hand, the -v flag offers a verbose output to help you monitor the synchronization.

The output will be:

sending incremental file list
created directory /mnt/volume-nyc3-01
html/
html/index.html

sent 318 bytes  received 39 bytes  714.00 bytes/sec
total size is 176  speedup is 0.49

By now the files are in place, but we have to modify the Nginx configuration file to adapt the changes.

Step 2 – Updating Nginx Configuration Files

Nginx web server utilizes both site-specific and global configuration files. Here, we’ll modify the file /etc/nginx/sites-enabled/example.com; a block server file for the domain example.com.

Note: You should substitute example.com with your domain name.

First, execute the command below to open this file:

$ sudo nano /etc/nginx/sites-enabled/example.com

Once the file is opened, locate a line that starts with root. Now, update this line with the value of your new document root location. This tutorial, uses /mnt/volume-nyc3-01/html as the new location. You should have the following:

server {

        root /mnt/volume-nyc3-01/html;
        index index.html index.htm index.nginx-debian.html;
        . . .
}
. . .

Note: Take note of any other place featuring traces of the root path to the old document. Any root path, alias or rewrite that shows details of the original document file should be updated to feature the new location of your document root.

Once you make the required changes, save block server file and close it.

Step 3  – Restarting Nginx

We have successfully move the files and made changes to the Nginx configuration file. Next, we restart Nginx service for these changes to take effect.

First, run the command below to check the syntax:

$ sudo nginx -t

If all is okay, you should get the output below:

nginx: the configuration file /etc/nginx/nginx.confsyntax is ok
nginx: configuration file /etc/nginx/nginx.conftest is successful

In case this test fails, inspect the problem and fix it. Next, run the command below to restart your Nginx service:

$ sudo systemctl restart nginx

Once the service restarts, check all the sites affected by the web root trabsfer and make sure they are running correctly. If you are comfortable with the results, execute the command below to erase the original data copy:

$ sudo rm -Rf /var/www/example.com/html

That is it!

Conclusion

Congratulations! You have transferred the Nginx web root to another location, and it’s now easy to manage your web server.

Check out these top 3 Linux hosting services

0

Was this article helpful?

Submit

Leave a Reply

Your email address will not be published. Required fields are marked *