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:
- Nginx Access Css Not Found Nginx Access Css Not Found Introduction For web developers, one of the most commonly encountered problems is when a page is not properly displaying due to the server not being…
- 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 Reverse Proxy Apache Change Document Root Nginx Reverse Proxy Apache Change Document Root What is a Reverse Proxy? A reverse proxy is a type of proxy server that takes HTTP or HTTPS requests from a client…
- 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…
- 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…
- 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…
- Nginx Multiple Domains Same Port Only Get One Domain Nginx Multiple Domains Same Port Only Get One Domain Understanding the Problem You just recently set up your Nginx web server and you're trying to set up multiple domains on…
- Nginx Can't Access Folder Forbidden Nginx Can't Access Folder Forbidden What is Nginx? Nginx is a web server that is developed to serve static content quickly and efficiently. It is an open source, popular web…
- Setting Serverblock For Domain Using Nginx On Ubuntu 18.04 Setting ServerBlock For Domain Using Nginx On Ubuntu 18.04 Introduction Nginx is a powerful open-source web server that can be used for serving static, dynamic websites and applications. Nginx is…
- Nginx Cannot Read Conf File In Nfs Drive Nginx Cannot Read Conf File In NFS Drive What is Nginx? Nginx is free and open source web server software. It's fast, reliable, and highly efficient. It's a reverse proxy,…
- 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…
- Stup Nginx In A Domain Medium Steps to Setup Nginx In A Domain Medium Introduction To Nginx Nginx (pronounced engine-x) is a lightweight, open source web server that was originally designed as a proxy server for…
- 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…
- Install Letsencrypt Debian 9 Nginx Install Letsencrypt Debian 9 Nginx What is Letsencrypt? Letsencrypt is an open source, free, automated SSL service that provides users with the ability to secure their websites and use HTTPS…
- Cannot Get Index.Html Nginx Cannot Get Index.Html Nginx What is Nginx? Nginx is a web server software designed for high-traffic websites and web applications. It is an open source reverse proxy server for HTTP,…
- Laravel In Local Nginx Windows Laravel In Local Nginx Windows Introduction Laravel is an open-source PHP framework that allows you to quickly create robust web applications. A large part of the development process for any…
- 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…
- 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…
- Nginx.Service Etc Nginx Permission Denied Nginx.Service Etc Nginx Permission Denied Understanding Nginx Services Nginx Services are a web server which is frequently used to serve web content and host web applications. It can be used…
- Remove Nginx Completely Centos 7 Remove Nginx Completely Centos 7 Overview This article will provide a brief overview of Nginx, some of the ways it can be removed from a Centos 7 system and directions…
- Install Php Nginx Mysql Ubuntu 18.04 Install Php Nginx Mysql Ubuntu 18.04 What is PHP, Nginx, and MySQL PHP is a popular and powerful scripting language that can be used to create dynamic web pages, web…
- Nginx Dev Mapper Centos-Root Is 100 Full Nginx Dev Mapper CentOs-Root Is 100 Full What Is Nginx Dev Mapper? Nginx Dev Mapper is a file system space mapping tool for the Linux operating system and is shipped…
- Cara 404 Not Found Nginx Cara 404 Not Found Nginx What is 404 Not Found Error? The HTTP 404 Not Found Error is a common browser error server webpages display when a requested page 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…
- Setup Vhost Nginx Debian 9.3 Setup Vhost Nginx Debian 9.3 Introduction This article will provide step-by-step instructions to setup Nginx Virtual Hosts (vhosts) on a Debian 9.3 system. Nginx is a web server software used…
- Webdav Nginx Ubuntu 16.04 Webdav Nginx Ubuntu 16.04 Introduction to Webdav with Nginx in Ubuntu 16.04 The ever-evolving world of the internet has given us a powerful platform to share information, media, and files…
- 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 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…
- 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…
- 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…