How to change Default Document root of Nginx Webserver
How to change Default Document root of Nginx Webserver
The Default document root is the default directory path where the user need to place index.html file to server the web page content for any site. It is imperative for the files to be saved in the appropriate directory for a website to function properly. This tutorial covers the configuration procedure of changing the default document root in Nginx server.
Configuration procedure
To start the configuration procedure, install the Nginx by enabling the epel repository by executing the following command.
[root@node2 ~]# yum install epel-release -y Loaded plugins: fastestmirror, refresh-packagekit, security Setting up Install Process Loading mirror speeds from cached hostfile * base: centos.excellmedia.net * epel: epel.mirror.angkasa.id * extras: centos.excellmedia.net * updates: centos.excellmedia.net Package epel-release-6-8.noarch already installed and latest version Nothing to do [root@node2 ~]# yum remove epel-release -y Loaded plugins: fastestmirror, refresh-packagekit, security Setting up Remove Process Resolving Dependencies --> Running transaction check ---> Package epel-release.noarch 0:6-8 will be erased --> Finished Dependency Resolution . . . Installed size: 22 k Downloading Packages: Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Erasing : epel-release-6-8.noarch 1/1 Verifying : epel-release-6-8.noarch 1/1 Removed: epel-release.noarch 0:6-8 Complete!
Now install the Nginx server by running the following command and press y to continue with the installation process.
[root@node2 ~]# yum install nginx -y
Loaded plugins: fastestmirror, refresh-packagekit, security
Setting up Install Process
Loading mirror speeds from cached hostfile
* base: centos.excellmedia.net
* epel: epel.mirror.angkasa.id
* extras: centos.excellmedia.net
* updates: centos.excellmedia.net
Resolving Dependencies
--> Running transaction check
---> Package nginx.x86_64 0:1.10.2-1.el6 will be installed
--> Processing Dependency: nginx-filesystem = 1.10.2-1.el6 for package: nginx-1.10.2-1.el6.x86_64
--> Processing Dependency: nginx-all-modules = 1.10.2-1.el6 for package: nginx-1.10.2-1.el6.x86_64
--> Processing Dependency: nginx-filesystem for package: nginx-1.10.2-1.el6.x86_64
--> Running transaction check
---> Package nginx-all-modules.noarch 0:1.10.2-1.el6 will be installed
--> Processing Dependency: nginx-mod-stream = 1.10.2-1.el6 for package: nginx-all-modules-1.10.2-1.el6.noarch
--> Processing Dependency: nginx-mod-mail = 1.10.2-1.el6 for package: nginx-all-modules-1.10.2-1.el6.noarch
.
.
.
nginx-mod-http-geoip.x86_64 0:1.10.2-1.el6
nginx-mod-http-image-filter.x86_64 0:1.10.2-1.el6 nginx-mod-http-perl.x86_64 0:1.10.2-1.el6 nginx-mod-http-xslt-filter.x86_64 0:1.10.2-1.el6
nginx-mod-mail.x86_64 0:1.10.2-1.el6 nginx-mod-stream.x86_64 0:1.10.2-1.el6
Complete!
The installation is done successfully. Then execute the following commands to start and enable Nginx service and then allow port 80 for HTTP service in iptables firewall.
[root@node2 ~]# service nginx start Starting nginx: [ OK ] [root@node2 ~]# chkconfig nginx on [root@node2 ~]# iptables -A INPUT -p tcp --dport 80 -j ACCEPT [root@node2 ~]# service iptables save iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ] [root@node2 ~]# service iptables restart iptables: Setting chains to policy ACCEPT: filter [ OK ] iptables: Flushing firewall rules: [ OK ] iptables: Unloading modules: [ OK ] iptables: Applying firewall rules: [ OK ]
Now create the new directory for new document root and set proper SELINUX context for Nginx.
[root@node2 ~]# mkdir /myroot
[root@node2 ~]# chcon -Rt httpd_sys_content_t /myroot/
Place the default index.html file to the server content for the specified web page by using the vim editor.
[root@node2 ~]# vim /myroot/index.html
This is new documentroot
Next edit the Nginx default.conf file to mention about the new document root content by running the following command.
[root@node2 ~]# vim /etc/nginx/conf.d/default.conf
Here comment out the default document root line and add the new document root line in the default.conf file.
root /myroot
Now restart the Nginx service by executing the following service.
[root@node2 ~]# service nginx restart
Stopping nginx: [ OK ]
Starting nginx: [ OK ]
Next open the web browser and check the output by typing the Nginx IP address on the browser.
Wasn' t that an easy configuration process? The document root can put each site’ s document root on its own volume allows the user to scale in response to the needs of a specific site or client.
sudo ln -s /etc/nginx/sites-available/default /etc/nginx/sites-enabled/