使用自定web根目录

4案例4:使用自定web根目录
4.1 问题

本例要求调整web站点http://server0.example.com的网页目录,要求如下:

  1. 新建目录/webroot,作为此站点新的网页目录
  2. 从http://classroom/pub/materials/station.html下载一个文件副本到这个目录,重命名为index.html
  3. 不要对文件index.html的内容作任何修改
  4. 确保站点http://server0.example.com仍然可访问
4.2 方案

在SELinux强制启动模式下,增加新的合规网页目录的方法:
1)参照标准目录,重设新目录的属性

chcon  [-R]  --reference=模板目录  新目录

或者
2)将新目录增加到预设的标准web目录范围

semanage  fcontext  -a  -t  httpd_sys_content_t      '新目录(/.*)?' 
4.3 步骤

实现此案例需要按照如下步骤进行。

步骤一:部署网页目录及文档

1)建立网页目录

[root@server0 ~]# mkdir  /webroot

2)部署网页文件

[root@server0 ~]# cd  /webroot/
[root@server0 webroot]# wget  http://classroom/pub/materials/station.html  -O  index.html
.. ..
2016-11-26 20:01:14 (826 KB/s) - ‘index.html’ saved [14/14]
[root@server0 webroot]# cat  index.html                  //检查网页文件
Default Site.

步骤二:调整虚拟站点http://server0.example.com/的配置

1)修改配置文件

[root@server0 ~]# vim  /etc/httpd/conf.d/00-default.conf
<VirtualHost  *:80>
        ServerName  server0.example.com
        DocumentRoot  /webroot
</VirtualHost>
.. ..

2)重启系统服务httpd

[root@server0 ~]# systemctl  restart  httpd

步骤三:确保虚拟站点http://server0.example.com/仍然可以访问

1)未调整网页目录SELinux上下文的情况
为虚拟站点http://server0.example.com/更换了新的网页目录以后,从浏览器访问将会失败,只能看到红帽测试页。

[root@desktop0 ~]# elinks  -dump  http://server0.example.com/
                       Red Hat Enterprise Linux Test Page
                       
   This page is used to test the proper operation of the Apache HTTP server
   after it has been installed. If you can read this page, it means that the
   Apache HTTP server installed at this site is working properly.
.. ..

针对此问题,可以参考目录/var/www的属性为网页目录/webroot设置SELinux安全上下文。

[root@server0 ~]# chcon  -R  --reference=/var/www  /webroot/
[root@server0 ~]# ls  -Z  /webroot/index.html                  //确认结果
-rw-r--r--. root root system_u:object_r:httpd_sys_content_t:s0 /webroot/index.html

2)未配置目录内容访问的情况
尽管已经调整过/webroot的SELinux安全上下文,但是从浏览器访问此虚拟站点时仍然会被拒绝,还是智能看到红帽测试页。
还需要修改对应的配置文件,添加内容访问控制:

[root@server0 ~]# vim  /etc/httpd/conf.d/00-default.conf 
<VirtualHost  *:80>
        ServerName  server0.example.com
        DocumentRoot  /webroot
</VirtualHost>
<Directory  "/webroot">
        Require  all  granted
</Directory>
<Directory  "/webroot/private">
        Require  ip  127.0.0.1  ::1  172.25.0.11
</Directory>
[root@server0 ~]# systemctl  restart  httpd             //重启httpd服务

若要保持原有private子目录,建议也拷贝过来:

[root@server0 ~]# cp  -rf  /var/www/html/private/  /webroot/

3)最终访问测试
从浏览器能成功房费调整后的虚拟站点http://server0.example.com/。

[root@desktop0 ~]# elinks  -dump  http://server0.example.com/
   Default Site.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值