How To Move Nginx Web Root


How To Move Nginx Web Root

Introduction

By default, your Nginx web root, also known as the root directory, is located at /usr/share/nginx/html. However, it is a common practice to relocate this directory to another folder outside the Nginx installation directory, such as in a user-defined folder. Relocating the Nginx web root can be beneficial when managing multiple web-serving configurations, or when you need to keep certain folders secret. In this tutorial, we will cover how to move Nginx web root to a private folder. After completing this tutorial, you should have a better understanding of how to move and configure Nginx root directory.

Prerequisites

To follow along with this tutorial, you will need the following items:

  • A server with Nginx installed.
  • A user with root privileges.

Locate Nginx Web Root

By default, the Nginx web root is located in the /usr/share/nginx/html directory. On Ubuntu and Debian systems, this root directory is known as /var/www/html. To check the real path of the web root directory, you can check the main Nginx configuration file.

The main Nginx configuration file is located at /etc/nginx/nginx.conf. Open it using nano or your favorite text editor:

sudo nano /etc/nginx/nginx.conf

Look for the server_name directive and find the corresponding web root directory:

server {
root /usr/share/nginx/html;
index index.html index.htm;

server_name example.com www.example.com;
# ...
}

In the above example, your Nginx web root is located at /usr/share/nginx/html. This is the default Nginx web root directory.

Create a New Directory for the Nginx Website

Now that you know where your current web root is located, you can create a new folder to contain the web files. Since this folder will contain your web files, it should be located outside of the Nginx installation directory. For example, you can create a folder in your home directory that contains the web files.

Create the folder using the mkdir command:

mkdir ~/www

Now, add the necessary permission for the web files with the chmod command:

chmod -R 755 ~/www

Copy Existing Nginx Website Files

Now that you have created a new folder to contain the web files, you need to copy them over from the old web root directory. To do so, you can use the cp command. For example, if the old web root is located at /usr/share/nginx/html, you can use the following command to copy the files:

cp -R /usr/share/nginx/html/* ~/www

This will copy all the web files from the old web root to the new directory.

Configure the Nginx Web Root

Once you have moved the web files, you need to configure the Nginx web root. To do so, open the main Nginx configuration file with nano or your favorite text editor:

sudo nano /etc/nginx/nginx.conf

Look for the server_name directive and update the root line with the path of the new web root directory:

server {
root ~/www;
index index.html index.htm;

server_name example.com www.example.com;
# ...
}

Save and close the file, then check the syntax of the configuration file using the nginx -t command:

sudo nginx -t

If the syntax is valid, you will see the following output:

nginx: the configuration file /etc/nginx/nginx.conf syntax is OK
nginx: configuration file /etc/nginx/nginx.conf test is successful

Once you have confirmed that the syntax is valid, you can restart the Nginx server to apply the changes:

sudo systemctl restart nginx

Conclusion

In this tutorial, you learned how to relocate the Nginx web root directory to another folder. To do this, you created a new folder to contain the web files, copied the files from the old web root into the new folder, and updated the root directive in the main Nginx configuration file. Then, you tested the configuration file and restarted the Nginx server to reload the changes.

FAQs

Q: What is the purpose of relocating the Nginx web root?

A: Relocating the Nginx web root is beneficial when managing multiple web-serving configurations, or when you need to keep certain folders private.

Q: How do I copy the files from the old web root to the new folder?

A: You can use the cp command to copy the files. For example, if the old web root is located at /usr/share/nginx/html, you can use the following command to copy the files: cp -R /usr/share/nginx/html/* ~/www.

Q: What command should I use to restart the Nginx server?

A: You can restart the Nginx server with the command sudo systemctl restart nginx.

Thank you for reading this article. Please read other articles on our website for more information.

Leave a Reply

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