一、啟用SELinux策略并安裝httpd服務,改變網站的默認主目錄為/website,添加SELinux文件標簽規則,使網站可訪問
1、修改selinux策略并重啟
[root@localhost ~]# vim /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=enforcing
2、安裝http服務
yum install -y httpd
3、修改默認的Directory 項指定的目錄為/website
[root@localhost ~]vim /etc/httpd/conf/httpd.conf <Directory "/website"> AllowOverride None # Allow open access: Require all granted </Directory> # Further relax access to the default document root: <Directory "/website"> DocumentRoot "/website"
4、添加目錄,創建index.html文件
[root@localhost ~]# mkdir /website
[root@localhost ~]# cd /website/
[root@localhost website]# echo "<center><h1>hello world" > index.html
5、修改index文件selinux標簽
[root@localhost website]# chcon -t httpd_sys_content_t index.html
[root@localhost website]# ls -Z
-rw-r–r–. root root unconfined_u:object_r:httpd_sys_content_t:s0 index.html
6、重啟httpd服務,使用網頁訪問:
[root@localhost website]# systemctl restart httpd [root@localhost website]# ss -tan State Recv-Q Send-Q Local Address:Port Peer Address:Port .......(省略)......... LISTEN 0 128 :::80 :::* .......(省略)......... 注:如遇不能訪問,查看防火墻是否打開,使用iptables -F 命令關閉。
二、修改上述網站的http端口為9527,增加SELinux端口標簽,使網站可訪問
1、編輯httpd配置文件,將80端口替換為9527
[root@localhost website]# vim /etc/httpd/conf/httpd.conf Listen 9527 使用vim內置命令,直接將80端口替換為9527(:%s/80/9527/)
2、添加httpd的端口
(需要使用semanage 命令,如沒有,使用$(yum install -y policycoreutils-python)安裝 1)查看當前http服務端口 [root@localhost website]# semanage port -l | grep http_port_t http_port_t tcp 80, 81, 443, 488, 8008, 8009, 8443, 9000 2)添加http服務端口9527
3、重啟httpd服務,并查看端口
[root@localhost website]# systemctl restart httpd [root@localhost website]# ss -tan State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 :::9527 /http端口變為9527監聽 :::*
4、使用網頁訪問,ip地址加端口號9527(因為web服務默認端口為80,如不手動添加端口,則不能訪問)
三、啟用相關的SELinux布爾值,使上述網站的用戶student的家目錄可通過http訪問
(如要添加家目錄中的網頁能通過http訪問,需將httpd服務selinux限制家目錄e的規則開啟:)
1)查看其相關的規則:
~]# getsebool -a | grep homedir git_cgi_enable_homedirs --> off git_system_enable_homedirs --> off httpd_enable_homedirs --> off //此規則處于off狀態,需要設置為on .....(省略).....
2)將http家目錄規則開啟為on
[root@localhost website]# setsebool -eP httpd_enable_homedirs on //-P選項為永久更改,直接修改進規則庫保存 [root@localhost website]# getsebool -a | grep homedir git_cgi_enable_homedirs --> off git_system_enable_homedirs --> off httpd_enable_homedirs --> on //此時此規則為開啟 (使用命令"semanage boolean -l" 可查看所有SElinux布爾型規則) [root@localhost website]# semanage boolean -l SELinux boolean State Default Description ftp_home_dir (off , off) Allow ftp to home dir smartmon_3ware (off , off) Allow smartmon to 3ware mpd_enable_homedirs (off , off) Allow mpd to enable homedirs xdm_sysadm_login (off , off) Allow xdm to sysadm login .........(省略)........ (使用命令"semanage boolean -l -C " 可查看所有更改過SElinux布爾型規則) [root@localhost website]# semanage boolean -l -C SELinux boolean State Default Description httpd_enable_homedirs (on , on) Allow httpd to enable homedirs
3)修改http服務的配置文件,將其中的禁用家目錄字段開啟
(注:Centos6和Centos7配置文件地址不相同) Centos6配置文件:/etc/httpd/conf/httpd.conf Centos7配置文件:/etc/httpd/conf.d/userdir.conf [root@localhost website]# vim /etc/httpd/conf.d/userdir.conf <IfModule mod_userdir.c> # ....(省略)...... # #UserDir disabled //此項原來為開啟,將其注釋掉, # To enable requests to /~user/ to serve the user's public_html # directory, remove the "UserDir disabled" line above, and uncomment # the following line instead: # UserDir web //此項原來為禁止,將其開啟,意為使用用戶目錄訪問,訪問方式上面的描述已經說明了“To enable requests to /~user/ to serve the user's public_html ”,由于我更改了目錄名稱,“public_html”則就為web
4)在用戶家目錄下創建web目錄,在其中創建index.html文件,更改其他用戶訪問權限,查看權限及標簽信息
[root@localhost ~]# su - li [li@localhost ~]$ mkdir web [li@localhost ~]$ echo "<center><h3>This is user:<h1> li</h1>home" > ./web/index.html [li@localhost ~]$ cd web/ [li@localhost web]$ chmod 711 index.html [li@localhost web]$ ls -Z -rwx--x--x. li li unconfined_u:object_r:httpd_user_content_t:s0 index.html
5)重啟服務,使用格式:"IP地址/~用戶名/web/index.html" 訪問
[root@localhost ~]# systemctl restart httpd
原創文章,作者:Lii,如若轉載,請注明出處:http://www.www58058.com/47604
文章內容較為完整,但是有亂碼,好像不是第一次出現這種情況了哦,需要花點時間找找原因啊。