4案例4:使用自定web根目錄
4.1 問題
本例要求調整web站點http://server0.example.com的網頁目錄,要求如下:
- 新建目錄/webroot,作爲此站點新的網頁目錄
- 從http://classroom/pub/materials/station.html下載一個文件副本到這個目錄,重命名爲index.html
- 不要對文件index.html的內容作任何修改
- 確保站點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.