Migrate Nginx Root Directory on Ubuntu 17.04 | 17.10

Nginx web server stores its documents in the /var/www/html directory on Ubuntu 17.04 | 17.10 systems by default. This is not a big deal for small websites or blogs. However, if your site or blog is huge you may run out of space because this location is on the root filesystem and may not be large enough for websites that are going rapidly.

When you website data is growing at a fast pace, you may find that the default root folder for Nginx web servers may not be adequate. You may have to move Nginx root folder to a different and larger location to accommodate your content.

This brief tutorial is going to show students and new users how to move Nginx default Root directory to a new location on Ubuntu 17.04 | 17.10.

To get started with migrating Nginx document root folder, follow the steps below:

Step 1: Copy Nginx Root content

Again, Nginx default root directory is at /var/www/html

Your goal is to change this default location to a new location with much higher free space. This is only useful if you’re running out of space at the default location..

This post assumes that you’ve already mounted or attached a large storage space to your system.. and that it’s mounted at /mnt/volume-mn1

The commands below will copy the entire html folder at /var/www/html and dump it into the new location at /mnt/volume-mn1

sudo rsync -av /var/www/html /mnt/volume-mn1

The new html folder will be at /mnt/volume-mn1/html

Step 2: Update Nginx Configuration Files

Now that you’ve copied all the content from Nginx default document root location, go and update Nginx configuration files of the new location. By default, Nginx site configuration files are stored in /etc/nginx/sites-available/ directory.

Run the commands below to open Nginx default site config file.

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

Then change the highlighted like to reflect the new root location

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;        


Save the file

Do this for all the site configuration files in the directory..

Step 3: Check and Restart Nginx

Now that everything is setup, run the commands below to test Nginx configuration

sudo nginx -t

If you get no error message, then run the commands below to reload the new settings.

sudo systemctl reload nginx.service

That’s it! Nginx should begin using the new and larger location to retrieve its content.


You may also like the post below:

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.