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:
- Ubuntu Install Web Server Nginx Ubuntu Install Web Server Nginx Step 1: Install the Nginx Package The first step when installing Nginx on Ubuntu is to install the Nginx package from the Ubuntu repository. This…
- Install Wordpress On Ubuntu Vps On Nginx Install Wordpress On Ubuntu VPS On Nginx What is WordPress? WordPress is an open-source, content management system (CMS) top-tier overall that is used to create powerful online presence. It powers…
- How To Configure Php And Nginx Windows How To Configure Php And Nginx Windows Understanding Nginx and PHP Nginx (Engine X) is an open source web server. It is a powerful tool for hosting websites and 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…
- How To Install Nginx Maridb 10 On Ubuntu 16.04 Lts How To Install Nginx Maridb 10 On Ubuntu 16.04 Lts Step 1 — Installing Nginx The first step in installing Nginx and MariaDB 10 on Ubuntu 16.04 is installing Nginx.…
- Nginx Install Custom Directory Phpmyadmin Nginx Install Custom Directory PhpMyAdmin Introduction to Nginx and PhpMyAdmin Nginx is a web server and content caching solution used to host a variety of web applications. It is highly…
- Change Html Folder To Www Nginx Change Html Folder To Www Nginx Introduction As websites continue to grow in size and complexity, it has become increasingly important to be able to customize and tweak the hosting…
- Nginx Directory Index Of Is Forbidden Nginx Directory Index Of Is Forbidden What is Nginx Directory Index of? Nginx Directory Index of is a directive used by the Nginx web server to indicate the location of…
- 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…
- Install Nginx Passenger Ubuntu 16.04 Install Nginx Passenger Ubuntu 16.04 Installing Nginx On Ubuntu 16.04 Nginx is an open source web server that can be used to create web and application servers. It is a…
- Install Nginx Ubuntu Server 14.04 Install Nginx Ubuntu Server 14.04 Method 1: Installing Nginx from the Ubuntu Repositories Nginx is available for installation from the default Ubuntu repositories using the apt package manager tool. If…
- 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…
- 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…
- 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…
- Nginx Config Domain Based Root Nginx Config Domain Based Root What is Nginx Config? Nginx (pronounced “engine-x”) is a popular web server software program. It is open source, high performance and is being used by…
- Install Phpmyadmin Ubuntu 18.04 Nginx Install PhpMyAdmin Ubuntu 18.04 Nginx What is PhpMyAdmin? PhpMyAdmin is an open source software written in PHP that provides a graphical web-based interface for accessing and managing your MySQL or…
- Install Laravel Ubuntu 16.04 With Nginx Web Server Install Laravel Ubuntu 16.04 With Nginx Web Server Introduction Laravel is one of the most popular expressive, elegant, and robust PHP frameworks available today. It is an open source framework…
- 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…
- Different Root Multiple Virtual Host Nginx Different Root Multiple Virtual Host Nginx Overview of Nginx Nginx is an open source, high-performance web server that is used to serve webpages to the client. It is used to…
- How To Make Xampp Using Nginx How To Make XAMPP Using Nginx Introduction XAMPP is a free, open-source software package developed by Apache Software Foundation that can be used to create webpages and applications. It is…
- 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…
- Install Phpmyadmin For Nginx Debian 8 Install Phpmyadmin For Nginx Debian 8 Introduction The Nginx web server popularly known as Nginx is a lightweight web server written in C programming language. It is an open source…
- Setting Php Mysql Nginx Di Windows Server Setting PHP MySQL Nginx Di Windows Server Prerequisites Before you begin installing and configuring PHP, MySQL and Nginx on your Windows Server, there are several prerequisites you need to verify.…
- 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…
- Link Sites Available To Sites Enabled Nginx Link Sites Available To Sites Enabled Nginx Understanding Sites Available and Sites Enabled Nginx Nginx is a web server that can be used to host web applications. In order to…
- Where Is Nginx Document Root Where Is Nginx Document Root What Is Nginx? Nginx is an open source, high-performance web server and reverse proxy software popular on Linux and Unix. It is used to serve…
- 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 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…
- Instal Nginx Ubuntu 18.04 Installing Nginx on Ubuntu 18.04 What is Nginx? Nginx is a lightweight, open source, high-performance web server designed for serving dynamic and static web content. It is capable of handling…
- Nginx Windows Create Virtual Host Nginx Windows Create Virtual Host What Is Nginx? Nginx is an open source web server software. First released in 2004, it has steadily become the preferred choice of web administrators…