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 13:47
下一篇 2016-09-19 13:47

相關推薦

  • Shell腳本編程中的if、case、for、while、until命令

    為何要學編程中的if、case、for、while、until命令?        作為一個運維工程師,總會聽到自動化,實際自動化離不開編寫shell腳本,而shell腳本中卻又離不開編程中的if、case、for、while、until, 這些關鍵字。 了解編程中的if、case、for、while、until命令,…

    Linux干貨 2016-08-19
  • shell 腳本中數組的總結

    描述:   變量是存儲單個元素的內存空間,而數組是多個變量的集合,是一個連續的空間;但整個數組只能有 的名字。   數組內的數據都有指定的索引,從而找到數組內所指定的數據。索引的編號是從0開始的,依次遞增(0,1,2,3,…),這種方式叫數值索引。格式為:數組名[索引];${ARRAY_NAME[INDEX]}。索引也支持自定…

    Linux干貨 2016-08-29
  • 文本處理及正則表達式

    文本處理工具:     more:分頁查看文件     less:分頁控制顯示文件     head 查看文件的前幾行         -n 3   顯示前三行      &nbs…

    Linux干貨 2017-05-31
  • 文件權限特殊管理之ACL

    ACL:access control list 權限訪問列表 應用范圍:所屬主和所屬組和其他用戶之外的他叔用戶 ACL是Linux系統權限額外支持的一項功能,需要文件系統的支持,例如:ReiserFS , EXT2 , EXT3 , EXT4 , JFS , XFS等都支持ACL功能 centos7 中:安裝系統分區和裝完系統之后自己手動添加的分區自動支持a…

    Linux干貨 2016-08-05
  • Linux進程管理

    一:進程的概念     1)進程:process,運行中的程序的一個副本的某部分,之所以說是副本的一部分是因為一個程序可以多個用戶同時以不同格式運行,如兩個用戶都在運行ls,一個運行的是ls -h ,另一個運行的是ls -l,那么這兩個用戶運行的程序代碼肯定是不一樣的而且不是程序的所有代碼;進程有生命周期; &nbsp…

    2017-03-26
  • Linux發展史

    Linux發展史 一、歷史 (一)、Unix 1、Unix操作系統是一個強大的多用戶、多任務操作系統,支持多種處理器架構。 2、最早由KenThompson(湯普遜)、Dennis Ritchie(丹尼斯·里奇,C語言之父,Unix之父)和Douglas McIlroy(麥克羅伊,程式設計師)于1969年在AT&T的貝爾實驗室開發。 3、目前它的商標…

    Linux干貨 2016-10-14

評論列表(1條)

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

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

欧美性久久久久