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.
Related Posts:
- Sites Available And Sites Enabled Nginx Sites Available and Sites Enabled Nginx If you are a web developer or system administrator, you might be familiar with Nginx as a web server with excellent performance and scalability.…
- Directory Index Of Is Forbidden Nginx Laravel Directory Index of is Forbidden Nginx Laravel What is Directory Index Of? Directory Index Of is an Nginx configuration setting which dictates whether or not a directory can be accessed…
- How To Php File Not Found Nginx How To Fix A Php File Not Found Nginx Error What Is Nginx and What Causes The Php File Not Found Error? Nginx is an open source web server and…
- Nginx How To Override Nginx.Conf Conf.D Nginx How To Override Nginx.Conf Conf.D What is Nginx? Nginx is a high-performance web server that is commonly used for web hosting and reverse proxies. It has become increasingly popular…
- Install Nginx Mysql Phpmyadmin Ubuntu Install Nginx Mysql Phpmyadmin Ubuntu 1. Introduction In today's tutorial, we will show you how to install Nginx, MySQL, and phpMyAdmin on Ubuntu 20.04. Nginx is an open source web…
- 403 Forbidden Nginx Debian 9 403 Forbidden Nginx Debian 9 What is 403 Forbidden Error? 403 Forbidden error is an HTTP status code that means that accessing the page or resource you were trying to…
- Nginx Permission To Access That Folder Nginx Permission To Access That Folder What Is Nginx? Nginx is a powerful web server that can be used to host web applications. It is commonly used for hosting large…
- Nginx After Change Root Directory I've Got 403 Forbidden Nginx After Change Root Directory I've Got 403 Forbidden What is a 403 Error? When you see an error saying "403 Forbidden", it means that you don't have permission to…
- Move On Nginx Web Root To A New Location Laravel Move On Nginx Web Root To A New Location Laravel Introduction Nginx is a web server and reverse proxy for sites running on the web. It’s fast and efficient, and…
- How To Install Nginx In Ubuntu How To Install Nginx In Ubuntu Introduction to Nginx Nginx is a very powerful web server for hosting websites and applications. It is a fast and reliable server, and is…
- Nginx Forgot Root Password Ssh Nginx Forgot Root Password Ssh Introduction to SSH and Root Password Secure Shell or SSH is a cryptographic network protocol that allows you to control and manage network devices such…
- Error Access Denided For User Root On Nginx Ubuntu 18 Error Access Denied For User Root On Nginx Ubuntu 18 What Is Nginx? Nginx is a web server which can be used to serve static files, run web applications, generate…
- Resize Max Upload File Nginx Also use two images throughout the article. Resize Max Upload File Nginx Understanding Nginx Nginx is a powerful open-source server software that helps run websites and web applications. It is…
- Multiple Block Server With Same Port In Nginx Configuration Multiple Block Server With Same Port In Nginx Configuration Introduction to Multiple Server Block Nginx is an extremely powerful and useful web server. One of its most powerful features is…
- Nginx Configure Multiple Sites Differen Port Nginx Configure Multiple Sites Different Port Introduction to Nginx Nginx is a web server software developed by Igor Sysoev in 2002. It’s a high performance web server with a large…
- Add Root Password Mysql Nginx Ubuntu 16.04 Add Root Password Mysql Nginx Ubuntu 16.04 What is a root password? In the context of computer security, a root password is a user account that is given access to…
- Nginx Conf Serve Static Files Nginx Conf Serve Static Files What is Nginx? Nginx is an open source web server created in 2004 by Igor Sysoev. It’s fast and reliable, making it an ideal web…
- Analyst Secops Sudo Nano Etc Nginx Custom_Server.Conf Analyst Secops Sudo Nano Etc Nginx Custom_Server.Conf What is Secops? Secops (Security Operations) is an integrated security system that aims to protect organizations from all manner of cyber threats, vulnerabilities,…
- Nginx Change Default Document Root Nginx Change Default Document Root Overview Nginx is one of the most popular web servers in the world and is used by millions of people to host their websites. It…
- How To Move Nginx Admin How To Move Nginx Admin Setting Up Nginx on a New Server If you need to move an existing Nginx server to a new machine, first you should set up…
- How To Disable Directory Listing In Nginx How To Disable Directory Listing In Nginx What is Directory Listing? Directory listing allows a web server or an FTP server to display a list of all the files or…
- Nginx Robots.Txt Exclude From Caching Nginx Robots.Txt Exclude From Caching Caching is an important part of any website as it allows content to be delivered quickly and efficiently to its users. But, as with any…
- Change Root Directory Nginx To Host Docker Change Root Directory Nginx To Host Docker What is a Root Directory? A root directory is the top-level directory on a file system that is used for the storage and…
- Nginx Conf Wordpress Root Directory Nginx Conf Wordpress Root Directory What Is Nginx? Nginx is a powerful web server that can be used to serve static or dynamic content. It has been used by some…
- Web Root Not Found In Nginx Web Root Not Found In Nginx What is Nginx? Nginx is an open-source Web server. It is written in C and can be used for a variety of purposes, from…
- Tutorial Start Nginx Ubuntu Webserver Tutorial Start Nginx Ubuntu Webserver Step 1: Prerequisites Before we start configuring Nginx as a web server on Ubuntu, we need to make sure we have the following prerequisites: A…
- Etc Nginx Sites-Available Default Permission Denied Etc Nginx Sites-Available Default Permission Denied What is Nginx and Why is it Used? Nginx is a powerful and open-source web server software used to host modern web applications. It…
- Nginx Read Php Files Outside Root Nginx Read Php Files Outside Root Understanding the Basics of Nginx Nginx is an open source web server and HTTP proxy server originally developed by Igor Sysoev. It can be…
- Set Root Folder Subdomain Nginx Set Root Folder Subdomain Nginx Nginx merupakan salah satu server web dengan performa yang cepat dan handal. Nginx dapat digunakan sebagai proxy server, load balancer, reverse proxy serta dapat digunakan…
- Centos 6 Nginx Phpmyadmin Forbideen Centos 6 Nginx Phpmyadmin Forbidden Introduction CentOS 6, an open-source Linux operating system, is one of the most popular web server operating systems. It is considered to be a reliable…