SElinux配置httpd

一、啟用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 命令關閉。

blob.png

二、修改上述網站的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,如不手動添加端口,則不能訪問)

blob.png

三、啟用相關的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

SElinux配置httpd

原創文章,作者:Lii,如若轉載,請注明出處:http://www.www58058.com/47604

(4)
LiiLii
上一篇 2016-09-19
下一篇 2016-09-19

相關推薦

  • 系統管理至grub故障排錯及自建linux

    第二章    系統啟動故障排除     1、grub配置文件寫錯,無法進入系統     步驟:(修復完成后記得修改配置文件為正確的文件)     方法一:進入啟動菜單項后,修改菜單項為正確的內容,然后…

    Linux干貨 2016-09-13
  • awk的基本原理

    awk的工作原理 一次讀取一行文本,按輸入分隔符進行切片,切成多個組成部分,將每片直接保存在內建的變量中,$1,$2,$3….,引用指定的變量,可以顯示指定斷,或者多個斷。如果需要顯示全部的,需要使用$0來引用??梢詫蝹€片斷進行判斷,也可以對所有斷進行循環判斷。其默認分隔符為空格 awk的基本用法格式awk [options…

    Linux干貨 2017-07-11
  • 馬哥教育N22期第五周作業

    1、顯示當前系統上root、fedora或user1用戶的默認shell; [root@localhost ~]# egrep "^root|fedora|user1" /etc/passwd root:x:0:0:root:/root:/bin/bash fedora:x:1002:1002::/…

    Linux干貨 2016-09-15
  • OPenSSL

    OPenSSL   OpenSSL 是一個安全套接字層密碼庫,囊括主要的密碼算法、常用的密鑰和證書封裝管理功能及SSL協議,并提供豐富的應用程序供測試或其它目的使用.   SSL是Secure Sockets Layer(安全套接層協議)的縮寫,可以在Internet上提供秘密性傳輸。Netscape公司在推出第一個Web瀏覽器的同時,提出了SSL協議標準?!?/p>

    Linux干貨 2016-11-07
  • Linux的用戶,組及文件權限管理

    Linux用戶與組的創建,刪除,屬性修改,文件權限管理

    Linux干貨 2018-02-24
  • Linux里的用戶與組

    任何事務的進行都離不開管理,脫離了管理的系統將會是一團亂麻。今天就來講講Linux里的用戶與組的管理 首先,用戶與組不會憑空出現,必須得是系統本身或人為創建的 。     所以,系統創建的就叫系統用戶.系統組,用戶創建的就是普通用戶.普通組。 useradd  創建用戶 -u 創建用戶并指定用戶的UID -g…

    2017-07-30

評論列表(1條)

  • 馬哥教育
    馬哥教育 2016-09-20 13:55

    文章內容較為完整,但是有亂碼,好像不是第一次出現這種情況了哦,需要花點時間找找原因啊。

欧美性久久久久