天天看点

使用自定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)建立网页目录

[[email protected] ~]# mkdir  /webroot
           

2)部署网页文件

[[email protected] ~]# cd  /webroot/
[[email protected] 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]
[[email protected] webroot]# cat  index.html                  //检查网页文件
Default Site.
           

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

1)修改配置文件

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

2)重启系统服务httpd

[[email protected] ~]# systemctl  restart  httpd
           

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

1)未调整网页目录SELinux上下文的情况

为虚拟站点http://server0.example.com/更换了新的网页目录以后,从浏览器访问将会失败,只能看到红帽测试页。

[[email protected] ~]# 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安全上下文。

[[email protected] ~]# chcon  -R  --reference=/var/www  /webroot/
[[email protected] ~]# 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安全上下文,但是从浏览器访问此虚拟站点时仍然会被拒绝,还是智能看到红帽测试页。

还需要修改对应的配置文件,添加内容访问控制:

[[email protected] ~]# 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>
[roo[email protected] ~]# systemctl  restart  httpd             //重启httpd服务
           

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

[[email protected] ~]# cp  -rf  /var/www/html/private/  /webroot/
           

3)最终访问测试

从浏览器能成功房费调整后的虚拟站点http://server0.example.com/。

[[email protected] ~]# elinks  -dump  http://server0.example.com/
   Default Site.
           

继续阅读