How To Change the Nginx Web Document Location in Linux

Changing document location will allow you to use another directory as a document location for storing site data in Linux. You can even set up multiple Nginx servers listening to different ports and redirect the traffic to two different document locations.

By default, the Nginx Web server uses the "/usr/share/nginx/html" document location to store site data, which can be changed to something else.

Today, you will learn how to change the Nginx Web Document Location on your Ubuntu/Debian/Fedora/CentOS Linux system.

Prerequisites

  • Nginx Web server is installed on your system
  • The drive should be mounted where the new location will be assigned
  • Web Browser for testing the result (Chrome, Firefox, etc.)

Configuration Files

Changing the default Nginx web document location requires modifications in the configuration file. This configuration file location might differ from distribution to distribution.

For Debian/Ubuntu distributions, the Nginx Web Server configuration file required to be modified is located at “/etc/nginx/sites-enabled/default“.

For CentOS/Fedora distributions, the Nginx Web Server configuration file required to be modified is located at “/etc/nginx/nginx.conf“.

Moving the Site Data to the New Location

Before changing the location, you need to first move your site data from the current location.

By default, the Nginx location for storing site data is at “/usr/share/nginx/html” for Debian/Ubuntu/Fedora/CentOS.

Assuming my new location for storing site data will be in the new directory at “/home/trendoceans/Documents/sitedata“.

Open your terminal using Ctrl+Alt+t or Ctrl+Shift+t and execute the below command to move site data from the current location to the new location “/home/trendoceans/Documents/sitedata“.

$ mkdir ~/Documents/sitedata
$ cp -r /usr/share/nginx/html/* ~/Documents/sitedata/

After the file is transferred, you can move to the next step configuring your Nginx for the new document location.

Changing the Configuration Files for Nginx

After the site data is moved to the new document location, open your Nginx configuration file using TextEditor, as shown below.

$ sudo nano /etc/nginx/sites-enabled/default     [On Debian/Ubuntu]
$ sudo nano /etc/nginx/nginx.conf                [On CentOS/Fedora]

Below is the output of the above default and nginx.conf configuration files.

From the configuration file, we need to find the line starting with root and comment it with “#” and then add the below code to the new line of the current “root” line.

root /home/trendoceans/Documents/sitedata;

Below is the output of the default and nginx.conf configuration files after the changes.

Restarting the Nginx Web Server

After the configuration is saved with the modification, start or restart the Nginx server using the below command.

$ sudo systemctl restart nginx  [On Debian/Ubuntu]
$ sudo systemctl restart nginx  [On CentOS/Fedora]

Finally, open your Web browser (Chrome, Firefox, etc.) and type domain or visit http://localhost.

Getting 403 Forbidden Permission

After making the mentioned changes, if you get an error such as “403 Forbidden Permission” while visiting the domain or localhost.

Then most probably, your new document location is not viewable. If Nginx tries to visit your new document directory, it might not have permission to view it.

To give all-sufficient permission for accessing the web page within the new document directory, execute the below command by replacing the home and root directory with your own.

$ chmod 711 /home/trendoceans
$ chmod 755 /home/trendoceans/Documents/sitedata
$ chmod 644 /home/trendoceans/Documents/sitedata/*

That’s all you need to change the Nginx Web Document Location directory. If you are facing any difficulty or got some random error, let us know in the comment section!

Leave a Reply