在非标准配置中为 Apache HTTP 服务器自定义 SELinux 策略

一.下载软件包

[root@localhost ~]# yum install policycoreutils-python-utils -y
[root@localhost ~]# yum install setroubleshoot-server -y
注意:
下载前需要先挂载,因为我之前的实验已经下载了httpd软件包,在这里就没有下载了,没有的需要下载。
在这里插入图片描述
这样就下载ok了。

二.创建目录,写入内容

[root@localhost ~]# mkdir /www/test
[root@localhost ~]# cd /www
[root@localhost www]# echo welcome to test >test/index.html

在这里插入图片描述

三.写配置文件

[root@localhost ~]# cd /etc/httpd/conf.d
[root@localhost conf.d]# vim host.conf

<directory /www>
allowoverride
require all granted
</directory>
listen 9091
<virtualhost 0.0.0.0:9091>
documentroot /www/test
servername 192.168.24.129
</virtualhost>

在这里插入图片描述

在这里插入图片描述

四.启动httpd服务并检查状态

[root@localhost conf.d]# systemctl start httpd
在这里插入图片描述

会有报错。提示为不能绑定端口。

五.SELinux策略运行在9091端口

[root@localhost conf.d]# semanage port -l | grep http

在这里插入图片描述

SELinux策略在httpd的80端口上运行的,我们现在需要让它在9091上面运行:
[root@localhost conf.d]# semanage port -a -t http_port_t -p tcp 9091
[root@localhost conf.d]# semanage port -l | grep http
在这里插入图片描述

六.重启服务

[root@localhost conf.d]# systemctl start httpd
[root@localhost conf.d]# systemctl status httpd

在这里插入图片描述
服务就启用在9091和80端口

七.排查内容无法访问的原因

[root@localhost conf.d]# wget 192.168.24.129:9091
访问时报错
在这里插入图片描述
[root@localhost conf.d]# sealert -l “*” | grep preventing 排查原因
在这里插入图片描述
最后一行提示我们说,需要让/www/test/index.html 获取到服务器访问权限。

也可以使用matchpathcon工具比较标准SELinux类型和新路径
[root@localhost conf.d]# matchpathcon /var/www/html /www/test (注意空格)
在这里插入图片描述
/var/www/html是默认路径,也就是允许 Apache(web 服务器进程作为 httpd_t 运行)访问的路径。/www/test 是我们定义的新路径。
通过比对发现:/var/www/html 的标签是httpd_sys_content_t。/www/test 标签是default_t。标签对不上就不能访问,所以我们接下来需要修改标签

八.修改标签

将新 /var/test_www/html/ 内容目录的 SELinux 类型改为默认 /var/www/html 目录的类型:
[root@localhost conf.d]# semanage fcontext -a -e /var/www/html /www/test
[root@localhost conf.d]# matchpathcon /var/www/html /www/test
在这里插入图片描述

九.递归重新标记 /www/test 目录

[root@localhost conf.d]# restorecon -Rv /www/test
[root@localhost conf.d]# ll /www/test -Z

在这里插入图片描述

十.验证:

  1. 检查 httpd 服务是否正在运行
    [root@localhost conf.d]# systemctl status httpd
    在这里插入图片描述

  2. 验证 Apache HTTP 服务器提供的内容是否可以访问
    [root@localhost conf.d]# wget 192.168.24.129:9091

在这里插入图片描述
[root@localhost conf.d]# wget 192.168.24.129:9091/index.html
在这里插入图片描述
成功访问到。这个实验就结束了。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值