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:
- 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…
- Install Laravel Nginx Ubuntu 18.04 Install Laravel Nginx on Ubuntu 18.04 Introduction In this tutorial, we will look at how to install Laravel with Nginx on an Ubuntu 18.04 server. Laravel is a powerful PHP…
- Cannot Create Directory Permission Denied Nginx Cannot Create Directory Permission Denied Nginx What is Nginx? Nginx is an open source web server software that helps people to build websites and web applications in a secure and…
- Nginx And Php-Fpm Configuration Nginx and Php-Fpm Configuration Why Configure Nginx and Php-Fpm Traditionally, web servers utilize Apache to serve webpages. However, recently Nginx has become more popular due to its scalability and lite-weight…
- How To Install Moodle On Nginx Postgresql Ubuntu How To Install Moodle On Nginx Postgresql Ubuntu Overview of Moodle and Requirements Moodle is an open-source learning management system (LMS) used by educators, businesses, and other organizations to create…
- Install Laravel 5.1 Ubuntu 16.04 Nginx Install Laravel 5.1 on Ubuntu 16.04 Nginx Introduction Laravel is a free, open-source PHP web application framework that is highly popular with modern web developers. It leverages an expressive and…
- Hide Html Extension On Nginx Disclaimer - This article is for informational purposes only. The author does not make any representations or warranties as to accuracy, completeness, or the results obtained from any information provided.…
- How To Set Rails On Nginx Ubuntu 18.04 How To Set Rails On Nginx Ubuntu 18.04 Step 1: Install Ruby Using RVM The first step for setting up Ruby on Rails on an Ubuntu 18.04 server with Nginx…
- Nginx Error Directory Of Index Is Forbidden Nginx Error Directory of Index Is Forbidden What is Nginx? Nginx is a web server that is used to serve content over the web. It is an open source server,…
- Install Nginx Windows Server 2012 Install Nginx Windows Server 2012 Introduction to Nginx for Windows Server 2012 Nginx is an open source web server and a reverse proxy server that was created to address the…
- 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…
- How To Know If Nginx Is Working For A Directory How To Know If Nginx Is Working For A Directory Nginx is a popular open-source web server that is widely used in many websites and applications. It is known for…
- 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…
- Install And Configure Nginx Mysql Install and Configure Nginx Mysql What is Nginx Nginx is an open source, high-performance HTTP server, reverse proxy, and IMAP/POP3 proxy server. It provides load balancing, content caching, access control,…
- 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 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…
- Mac Os X Nginx Conf Location Mac OS X Nginx Conf Location What is Nginx? Nginx is a high-performance web server and reverse proxy originally written for Linux, but now it’s also available for Mac OS…
- 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.…
- Setup Virtual Hosts In Nginx Setup Virtual Hosts In Nginx Understanding The Virtual Hosts Concept The Virtual Hosts concept is an important part of installing Nginx. It allows you to host multiple websites on a…
- How To Configure Websocket Nginx Fpm How To Configure Websocket Nginx Fpm Introduction Websocket is a modern web technology that provides bidirectional communication between a web server and a web client. The websocket protocol allows for…
- Install Nginx Php Mysql Windows Server Install Nginx Php Mysql Windows Server Install Nginx Php Mysql Windows Server Installing Nginx Nginx is a web server that can be used to serve dynamic web pages as well…
- Execstart Usr Sbin Nginx Etc Nginx Nginx Conf Execstart Usr Sbin Nginx Etc Nginx Nginx Conf What is Nginx? Nginx is a lightweight, high-performance web server and reverse proxy. It is known for its speed and scalability, and…
- 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…
- Install Nginx-Naxsi Ubuntu 16.04 Install Nginx-Naxsi on Ubuntu 16.04 Understanding Nginx and Naxsi Nginx is a high-performance web server which can also be used as a reverse proxy or load balancer. Naxsi is a…
- Membuat Virtualhost Menggunakan Nginx Didebian 8 Membuat Virtualhost Menggunakan Nginx Didebian 8 Apa yang Harus Dilakukan? Membuat Virtualhost di Debian 8 adalah salah satu proyek yang dapat Anda lakukan sendiri dengan mudah. Proyek ini termasuk memasang…
- Deploy Laravel Nginx Ubuntu 17 Deploy Laravel Nginx Ubuntu 17 Requirements for Installing Laravel 5.4 on Ubuntu 17 This article will guide you through the process of installing Laravel 5.4 on Ubuntu 17. Before we…
- 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 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…
- 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…
- How To Install Phpmyadmin On Nginx How To Install Phpmyadmin On Nginx Introduction PhpMyAdmin is an open source software program which is used to manage MySQL and MariaDB databases. It provides a graphical interface to execute…