Modifying the Nginx Web Root Location on Ubuntu 16.04
11:35, 16.05.2025
When hosting websites on an Nginx web server, you may want to change the default root directory. This can help you better organize files and documents, improve security, or accommodate different website structures. In Ubuntu 16.04, this process is simple, but requires careful configuration to ensure that your server works properly.
In this guide, you will learn how to change the location of the Nginx root directory and make sure your configurations are updated correctly.
Essential Requirements Before You Begin
Before you start modifying the Nginx web root location, ensure you have the following:
- Ubuntu 16.04 Server
Make sure you have access to an Ubuntu 16.04 server with Nginx installed. You can verify the Nginx installation by running `nginx -v` in the terminal. - Root or Sudo Privileges
You’ll need administrative access to modify Nginx configuration files. If you don’t have root access, ensure you have a user account with sudo privileges. - Text Editor
Familiarize yourself with a text editor such as `nano` or `vim` for editing configuration files. - Basic Knowledge of Nginx
A basic understanding of how Nginx operates and its configuration structure will help you make the necessary changes without issues. - Backup Your Current Configuration
Before making any changes, it’s crucial to back up your existing Nginx configuration file.
You can do this using the command `sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/default.bak` to ensure you have a fallback option in case of any mistakes.
Step 1 — Transferring Files to the Desired Directory
The first stage in modifying your Nginx web root location is to transfer your existing website files to the new directory where you want the web root to be located.
- Create the New Directory.
Choose a location for your new web root directory. Common locations include `/var/www/new_root` or `/home/username/new_root`. Create this directory using the following command:
sudo mkdir -p /var/www/new_root
- Transfer Your Website Files
Next, move your existing website files from the current web root (`/var/www/html` by default) to the new directory:
sudo mv /var/www/html/* /var/www/new_root
Ensure that all files and documents are moved correctly. You may use `cp` instead of `mv` if you prefer to keep the original files as a backup.
- Update Directory Permissions
It’s important to make sure that Nginx has the necessary permissions to read from the new directory. Adjust the ownership and permissions using:
sudo chown -R www-data:www-data /var/www/new_root
sudo chmod -R 755 /var/www/new_root
These commands ensure that the `www-data` user (which Nginx runs as) has the appropriate read and execute permissions.
After completing these tasks, you have successfully moved your site documents to the new root directory, preparing it for future configuration changes in Nginx.
Step 2 — Modifying Nginx Configuration Settings
Now that your website files and documents are in the new directory, you need to update the Nginx configuration to point to this new web root location.
- Open the Nginx Configuration File
The default Nginx configuration file is located at `/etc/nginx/sites-available/default`. Open it with a text editor such as `nano`:
sudo nano /etc/nginx/sites-available/default
- Update the Root Directive
Locate the `root` directive within the `server` block.
It typically looks like this:
root /var/www/html;
Change this line to point to your new web root directory, for example:
root /var/www/new_root;
- Check for Additional References
Make sure to update any other references to the old web root within the configuration file, such as in `location` blocks. For example:
location / {
root /var/www/new_root;
index index.html index.htm;
}
- Test the Nginx Configuration
After saving the changes, it’s important to test the Nginx configuration for any syntax errors. Run the following command:
sudo nginx -t
If there are no errors, you will see a message indicating that the configuration is successful.
- Reload Nginx to Apply Changes
Finally, reload Nginx to apply your updated configuration:
sudo systemctl reload nginx
At this point, Nginx should now be serving your website from the new web root directory. Make sure to check your site in a browser to confirm that everything is functioning correctly.
Step 3 — Rebooting Nginx for Changes to Take Effect
After modifying the Nginx configuration, it's crucial to ensure that all changes take effect by restarting the Nginx service.
- Restart Nginx
Use the following command to restart Nginx:
sudo systemctl restart nginx
This command stops and then starts the Nginx service, applying all the recent configuration changes, including the updated web root location.
- Verify Nginx is Running
To confirm that Nginx is running correctly after the restart, you can check its status with:
sudo systemctl status nginx
You should see a message indicating that Nginx is active and running. If there are any issues, review the error messages and correct any problems in your configuration file.
- Check Your Website
Finally, open your web browser and navigate to your website. Ensure that it loads correctly and serves documents from the new web root location. This confirms that your changes have been successfully applied.
This step will completely restart Nginx, and the updated configuration settings should take effect.
Final Thoughts
Changing the location of the Nginx web root directory in Ubuntu 16.04 is a simple process. It provides more flexibility in organizing and managing your website files and documents.
Your server will continue to function efficiently with the new web root directory as long as you reboot Nginx, carefully transferring files, documents, and updating configuration settings.
Remember to always backup your configuration files before making any changes and check your settings to avoid any downtime. Confidently customize your Nginx web root to suit your hosting needs and make your server more adaptable to future changes or requirements.