Why should I change my Root Directory?
If you want to protect your data, you should change your root directory. Create another partition for your home folder on the disk or use another drive and keep your web server root directory their. If your system get crashed, you can easily re-install the new OS without deleting or moving your web server root directory data because it is on another partition or disk. simplest method is create a symbolic link between /var/www/html folder and /home/www. But the best method is to change these setting in httpd configuration file.If you need to change the root directory of web server. The default root directory is at /var/www/html. In my case, I want to change it to /home/www.
Step-1: Edit apache/httpd configuration file (as root)
1 2 3 | # vi /etc/httpd/conf/httpd.conf |
Find these two sections:
1 2 3 4 5 6 7 8 | # # DocumentRoot: The directory out of which you will serve your # documents. By default, all requests are taken from this directory, but # symbolic links and aliases may be used to point to other locations. # DocumentRoot "/var/www/html" |
1 2 3 4 5 6 | # # This should be changed to whatever you set DocumentRoot to. # <Directory "/var/www/html"> |
Then, change the directory location whatever you want:
1 2 3 4 5 6 7 8 | # # DocumentRoot: The directory out of which you will serve your # documents. By default, all requests are taken from this directory, but # symbolic links and aliases may be used to point to other locations. # DocumentRoot "/home/www" |
1 2 3 4 5 6 | # # This should be changed to whatever you set DocumentRoot to. # <Directory "/home/www"> |
Save and exit from vi editor.
Step-2: Changing Permission of web server root directory
1 2 3 | # chmod -R 755 /home/www |
At this point if you try to restart apache/httpd server, you may get this error:
1 2 3 4 5 6 7 | # service httpd restart Stopping httpd: [ OK ] Starting httpd: Syntax error on line 294 of /etc/httpd/conf/httpd.conf: DocumentRoot must be a directory [FAILED] |
This step is necessary since in CentOS, httpd process will be executed by user ‘apache’, thus permission need to be change.
1 2 3 4 | # setsebool -P httpd_enable_homedirs 1 # chcon -R -t httpd_sys_content_t /home/www |
Note: httpd by default is not allowed to access users home directories.
Step-3: Restart apache/httpd web server
1 2 3 | # service httpd restart |
Result:
1 2 3 4 5 | # service httpd restart Stopping httpd: [ OK ] Starting httpd: [ OK ] |