In this tutorial I’ll discuss about how to configuratin custom directory for web server in linux centos/redhat. when you configure webserver, the default directory are contained on /var/www/html.
you can setup custom directory if you do not want the webserver directory have in /var/www/html. But all process and files in linux centos/redhat are labeled by SElinux so that we can’t changes arbitrary setup for used certain directory. we must declare the default labeling for certain directory with file context what will you use.
for the example we will to configure html directory in /mywebsite
First install the httpd packages in you system
# yum install httpd -y
then create new directory in root dir with the name as mywebsite
# mkdir /mywebsite
create new file index.html and fill in text in the index.html file.
# echo “Test my new website with custom directories” > /mywebsite/index.html
Do changes default html directory to the /mywebiste directory in /etc/httpd/conf/httpd.conf as the shown bellow
Restart and enable the httpd service
# systemctl restart httpd
# systemctl enable httpd
Test on web browser for access the webserver
As the shown image above, selinux are blocking the /mywebsite directories .
Now check file context type in the /mywebsite direcotories with ls -lZ command.
# ls -lZ /mywebsite/
as the shown image above, the default file context type are default_t. If you want the directory run as html directory you must chages the file context type become to httpd_sys_content_t .
# semanage fcontext -a -t httpd_sys_content_t ‘/mywebsite(/.*)?’
and then apply the chages of semanage with restorecon command
# restorecon -Rv /mywebsite
check again filecontext in the /mywebsite directories.
# ls -lZ /mywebsite
Now test for access via web browser again
Configuration has successfully