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 Change Default Index.Html Nginx Change Default Index.Html What is Nginx? Nginx is a popular open source web server and proxy server that powers some of the world's busiest websites. It is primarily used…
- 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…
- 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…
- Create Subdomain Nginx Debian 9 Server Create Subdomain Nginx Debian 9 Server Understanding Subdomain A subdomain is a subsection of a domain that a user can create to host one or more websites, usually for a…
- Nginx Copy Site Available Site-Available Site-Enable Cp Ls Nginx Copy Site Available Site-Available Site-Enable Cp Ls What is a Nginx Copy Command? Nginx copy command is a tool used to copy files and directories from one site to…
- Setting Proxy Nginx Centos 7 Setting Proxy Nginx Centos 7 Introduction Setting up a proxy server on a CentOS 7 server is a relatively straightforward task. Nginx is a powerful open source reverse proxy server…
- There Are No Sites-Available Folder Nginx . There Are No Sites-Available Folder Nginx What is Nginx Server? Nginx is an open-source web server that is used to deliver webpages to users. It is a free and…
- Reinstall Nginx Ubuntu 18.04 Reinstall Nginx Ubuntu 18.04 What is Nginx? Nginx is an open source, high-performance web server application designed to serve web traffic with lightning-fast speed and robust stability. Nginx is one…
- How To Set Static Nginx How To Set Static Nginx Understanding What is Nginx? Nginx is an open source Web server software used for hosting static or dynamic websites, media streaming, and other web applications.…
- Create Self Signed Certificate Centos 7 Nginx Create Self Signed Certificate Centos 7 Nginx Introduction A self-signed certificate is an authentication mechanism in computing that allows a user to verify his or her identity without the need…
- 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…
- 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…
- 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.…
- Sudo Nano Etc Nginx Sites Available Default Sudo Nano Etc Nginx Sites Available Default What Is ‘Sudo Nano etc/nginx/sites-available/default’? 'Sudo nano etc/nginx/sites-available/default' is a command used to edit an nginx config file, which stores the necessary configuration…
- Nginx Css And Image Not Showing Centos Nginx Css And Image Not Showing Centos Common Problems When Nginx CSS And Images Aren't Showing When you're working with Nginx on the Centos operating system, you may have run…
- Install Php Nginx Ubuntu 12.04 Install Php Nginx Ubuntu 12.04 Introduction Welcome to our guide on How to install PHP, Nginx & MySQL on Ubuntu 12.04 LTS. This guide should work on other Linux VPS…
- 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…
- 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…
- Nginx 1.14 Create Virtual Host Nginx 1.14 Create Virtual Host Overview Virtual hosting is a process for hosting multiple websites on a single physical server and IP address. Nginx version 1.14 is the most recent…
- Nginx Php-Fpm Permission Denied Nginx + PHP-FPM - Permission Denied Issue What is Nginx? Nginx is an open source web server and reverse proxy. It is an ideal platform for building high performance, scalable…
- 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…
- 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,…
- Cara Buat Virtual Host Di Nginx Cara Buat Virtual Host Di Nginx Apa Itu Virtual Host? Virtual Host adalah sebuah teknologi jaringan yang memungkinkan sebuah mesin untuk dapat melayani permintaan berbagai macam website, dengan tidak memerlukan…
- 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…
- 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 Change Root In Location Nginx Change Root In Location Introduction to Nginx Location Nginx is a web server. It provides webservices such as static file serving, reverse proxying, and creating an intranet or extranet.…
- Nginx Multi Domain Centos 7 Nginx Multi Domain Centos 7 Introduction to Nginx Nginx is an open source, high performance web server software written in C language, designed to be deployed on Linux and Unix-like…
- 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…
- 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…
- 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…