獨立Web站點的快速部署
1.1 問題
本例要求為 http://server0.example.com 配置Web站點,要求如下:
- 從http://classroom/pub/materials/station.html下載一個主頁文件,將其重命名為 index.html
- 將此文件拷貝到站點的 DocumentRoot 目錄下,不要對文件 index.html 的內容作任何修改
- 使用 elinks 或firefox 瀏覽上述Web站點
1.2 方案
Web網站服務端:軟件包httpd、系統服務httpd
Web網站瀏覽器:軟件包elinks或fireox
傳輸協議及端口:TCP 80
Web網站服務端配置文件:
- /etc/httpd/conf/httpd.conf
- /etc/httpd/conf.d/*.conf
默認首頁文件:index.html
httpd網站文檔的默認根目錄:/var/www/html
URL(Uniform Resource Locator,統一資源定位器)網址的基本組成:
- http://服務器地址[:端口號]/目錄/文件名
對於需要驗證的FTP資源,還需要指定用戶名密碼信息:
- ftp://用戶名:密碼@服務器地址[:端口號]/目錄/文件名
1.3 步驟
實現此案例需要按照如下步驟進行。
步驟一:構建及部署網站服務器
1)安裝軟件包httpd
- [root@server0 ~]# yum -y install httpd
- .. ..
2)部署網頁
- [root@server0 ~]# cd /var/www/html/ //進入網頁目錄
- [root@server0 html]# wget http://classroom/pub/materials/station.html -O index.html //下載網頁
- .. ..
- 2016-11-26 19:33:49 (1.36 MB/s) - ‘index.html’ saved [14/14]
- [root@server0 html]# cat index.html //檢查網頁文件
- Default Site.
3)啟動系統服務httpd,並設置開機自啟
- [root@server0 html]# systemctl restart httpd
- [root@server0 html]# systemctl enable httpd
- ln -s '/usr/lib/systemd/system/httpd.service' '/etc/systemd/system/multi-user.target.wants/httpd.service'
步驟二:訪問網站服務器
1)使用elinks瀏覽器查看
Elinks瀏覽器可以在命令行模式顯示出網頁文本,經常用來測試網站的可用性。
- [root@desktop0 ~]# yum -y install elinks //安裝elinks
- .. ..
- [root@desktop0 ~]# elinks -dump http://server0.example.com/ //訪問指定網址
- Default Site.
2)使用firefox瀏覽器查看
Firefox瀏覽器支持更多網頁特性,是訪問復雜網頁、網址的優秀工具。
在桌面終端直接運行“firefox http://server0.examle.com/”,或者通過菜單快捷方式打開Firefox瀏覽器再輸入對應網址,都可以看到目標網頁(如圖-1所示)。

圖-1
2 案例2:虛擬Web主機的部署
2.1 問題
本例要求為server0擴展Web站點,新建虛擬主機 http://www0.example.com,具體要求如下:
- 設置 DocumentRoot 為 /var/www/virtual
- 從 http://classroom/pub/materials/www.html 下載主頁文件,並重命名為 index.html
- 不要對文件 index.html 的內容作任何修改,將其放到此虛擬主機的 DocumentRoot 目錄下
- 確保 fleyd 用戶能在 /var/www/virtual 目錄建文件
- 確保站點 http://server0.example.com 仍然可用
2.2 方案
單一網站平台(比如172.25.0.11):
- 多個域名 ---> 相同的網頁內容
- 配置文件:/etc/httpd/conf/httpd.conf
- 網頁目錄定義:DocumentRoot /var/www/html
虛擬主機平台(比如172.25.0.11):
- 在同一套httpd平台上跑很多個網站
- 多個域名 ---> 不同的網頁內容
- 網頁目錄由<VirtualHost ...>區段配置定義
多個虛擬主機站點的典型設置(/etc/httpd/conf.d/*.conf):
- <VirtualHost *:80>
- ServerName 網站1的FQDN
- DocumentRoot 網站1的網頁根目錄
- </VirtualHost>
- <VirtualHost *:80>
- ServerName 網站2的FQDN
- DocumentRoot 網站2的網頁根目錄
- </VirtualHost>
- .. ..
2.3 步驟
實現此案例需要按照如下步驟進行。
步驟一:部署網頁文檔
1)建立網頁目錄
- [root@server0 ~]# mkdir /var/www/virtual
- [root@server0 ~]# useradd fleyd
- [root@server0 ~]# setfacl -m u:fleyd:rwx /var/www/virtual/
2)部署網頁文件
- [root@server0 ~]# cd /var/www/virtual/
- [root@server0 virtual]# wget http://classroom/pub/materials/www.html -O index.html
- .. ..
- 100%[=====================>] 14 --.-K/s in 0s
- 2016-11-26 20:01:14 (826 KB/s) - ‘index.html’ saved [14/14]
- [root@server0 virtual]# cat index.html //檢查網頁文件
- Virtual Site.
步驟二:配置虛擬主機 http://www0.example.com/
1)為新站點創建獨立的配置文件
- [root@server0 virtual]# vim /etc/httpd/conf.d/01-www0.conf
- <VirtualHost *:80>
- ServerName www0.example.com
- DocumentRoot /var/www/virtual
- </VirtualHost>
- [root@server0 virtual]# httpd -t //確保語法檢查OK
- Syntax OK
2)重啟系統服務httpd
- [root@server0 virtual]# systemctl restart httpd
步驟三:訪問虛擬主機 http://www0.example.com/
訪問此虛擬站點,可以看到預期的網頁內容:
- [root@desktop0 ~]# elinks -dump http://www0.example.com/
- Virtual Site.
步驟四:完善原始站點 http://server0.example.com/
需要注意的是,原始的獨立站點可能出現異常,訪問時並不是原始的網頁:
- [root@desktop0 ~]# elinks -dump http://server0.example.com/
- Virtual Site.
原因是一旦啟用虛擬站點機制以后:
- 外部的 DocumentRoot、ServerName 會被忽略
- 第1個虛擬站點被視為默認站點,若客戶機請求的URL不屬於任何已知站點,則由第1個站點響應
若要解決此異常,需要將原始站點轉換為第一個虛擬主機,啟用順序的設置可以通過文件名開頭的數字來實現。
1)為原始站點建立虛擬主機配置
- [root@server0 ~]# vim /etc/httpd/conf.d/00-default.conf
- <VirtualHost *:80>
- ServerName server0.example.com
- DocumentRoot /var/www/html
- </VirtualHost>
2)重啟系統服務httpd
- [root@server0 virtual]# systemctl restart httpd
3)訪問兩個虛擬站點,確保各自的網頁內容正確
- [root@desktop0 ~]# elinks -dump http://server0.example.com/
- Default Site.
- [root@desktop0 ~]# elinks -dump http://www0.example.com/
- Virtual Site.
3 案例3:配置網頁內容訪問
3.1 問題
本例要求在 Web 網站 http://server0.example.com 的 DocumentRoot 目錄下創建一個名為 private 的子目錄,要求如下:
- 從 http://classroom/pub/materials/private.html 下載一個文件副本到這個目錄,重命名為 index.html
- 不要對文件 index.html 的內容作任何修改
- 從 server0 上,任何人都可以瀏覽 private 的內容,但是從其他系統不能訪問這個目錄的內容
3.2 方案
配置Web內容的訪問控制需要添加Directory區段,主要形式可參考
- <Directory "父目錄路徑">
- Require all denied //上層目錄拒絕任何訪問
- </Directory>
- <Directory "子目錄1路徑">
- Require all granted //子目錄1允許任何訪問
- </Directory>
- <Directory "子目錄2路徑">
- Require ip IP或網段地址 .. .. //子目錄2允許少數客戶機
- </Directory>
3.3 步驟
實現此案例需要按照如下步驟進行。
步驟一:部署網頁子目錄及文檔
1)建立子目錄
- [root@server0 ~]# mkdir /var/www/html/private
2)部署網頁
- [root@server0 ~]# cd /var/www/html/private
- [root@server0 private]# wget http://classroom/pub/materials/private.html -O index.html
- .. ..
- 2016-11-26 20:30:28 (1.90 MB/s) - ‘index.html’ saved [14/14]
- [root@server0 private]# cat index.html //檢查網頁文件
- Private Site.
步驟二:為指定的網頁子目錄限制訪問
在httpd服務的標准配置中,根目錄 / 默認拒絕任何訪問,但網頁目錄/var/www/默認允許任何訪問。因此,只需要為個別子目錄增加訪問控制即可。
1)調整虛擬站點server0.example.com的配置文件
- [root@server0 ~]# vim /etc/httpd/conf.d/00-default.conf
- .. ..
- <Directory "/var/www/html/private">
- Require ip 127.0.0.1 ::1 172.25.0.11
- </Directory>
2)重啟系統服務httpd
- [root@server0 ~]# systemctl restart httpd
步驟三:測試目錄訪問限制
1)從desktop0上訪問http://server0.example.com/private/被拒絕
- [root@desktop0 ~]# elinks -dump http://server0.example.com/private/
- Forbidden
- You don't have permission to access /private/ on this server.
2)從desktop0上訪問http://server0.example.com/仍然是正常的
- [root@desktop0 ~]# elinks -dump http://server0.example.com/
- Default Site.
3)從server0本機上訪問http://server0.example.com/private/也不受限制
- [root@server0 ~]# elinks -dump http://server0.example.com/private/
- Private Site.
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.
5 案例5:配置安全Web服務
5.1 問題
本例要求為站點 http://server0.example.com 配置TLS加密
- 一個已簽名證書從以下地址獲取 http://classroom/pub/tls/certs/server0.crt
- 此證書的密鑰從以下地址獲取 http://classroom/pub/tls/private/server0.key
- 此證書的簽名授權信息從以下地址獲取http://classroom/pub/example-ca.crt
5.2 方案
安全Web傳輸協議及端口:TCP 443
訪問HTTP站點(未加密):http://server0.example.com/
訪問HTTPS站點(加密):https://server0.example.com/
為httpd服務端實現TLS加密的條件:1)啟用一個 mod_ssl 模塊;2)提供加密的素材:網站服務器的數字證書、網站服務器的私鑰、根證書(證書頒發機構的數字證書)
TLS證書部署位置:/etc/pki/tls/certs/*.crt
TLS私鑰部署位置:/etc/pki/tls/private/*.key
5.3 步驟
實現此案例需要按照如下步驟進行。
步驟一:配置HTTPS網站服務器
1)安裝mod_ssl模塊軟件包
- [root@server0 ~]# yum -y install mod_ssl
- .. ..
2)部署密鑰、證書等素材
- [root@server0 ~]# cd /etc/pki/tls/certs/
- [root@server0 certs]# wget http://classroom/pub/example-ca.crt
- .. ..
- 2016-11-27 01:04:51 (116 MB/s) - ‘example-ca.crt’ saved [1220/1220]
- [root@server0 certs]# wget http://classroom/pub/tls/certs/server0.crt
- .. ..
- 2016-11-27 01:04:06 (62.1 MB/s) - ‘server0.crt’ saved [3505/3505]
- [root@server0 certs]# ls *.crt //確認部署結果
- ca-bundle.crt example-ca.crt server0.crt
- ca-bundle.trust.crt localhost.crt
- [root@server0 certs]# cd /etc/pki/tls/private/
- [root@server0 private]# wget http://classroom/pub/tls/private/server0.key
- .. ..
- 2016-11-27 01:07:09 (39.0 MB/s) - ‘server0.key’ saved [916/916]
3)為SSL加密網站配置虛擬主機
- [root@server0 ~]# vim /etc/httpd/conf.d/ssl.conf
- Listen 443 https
- .. ..
- <VirtualHost _default_:443>
- DocumentRoot "/var/www/html" //網頁目錄
- ServerName server0.example.com:443 //站點的域名
- .. ..
- SSLCertificateFile /etc/pki/tls/certs/server0.crt //網站證書
- .. ..
- SSLCertificateKeyFile /etc/pki/tls/private/server0.key //網站私鑰
- .. ..
- SSLCACertificateFile /etc/pki/tls/certs/example-ca.crt //根證書
4)重啟系統服務httpd
- [root@server0 ~]# systemctl restart httpd
- [root@server0 ~]# netstat -antpu | grep httpd //確認已監聽80、443端口
- tcp6 0 0 :::443 :::* LISTEN 7954/httpd
- tcp6 0 0 :::80 :::* LISTEN 7954/httpd
步驟二:驗證HTTPS加密訪問
使用firefox瀏覽器訪問加密站點https://server0.example.com/,可以看到頁面提示未信任連接“Untrusted Connection”(如圖-2所示)。

圖-2
若要繼續訪問,需要在頁面下方單擊超鏈接“I Understand the Risks”,表示用戶已理解相關風險。然后在展開的頁面內點擊“Add Exception”按鈕(如圖-3所示)。

圖-3
彈出添加安全例外對話窗口(如圖-4所示),單擊界面左下角的“Confirm Security Exception”按鈕確認安全例外。

圖-4
確認成功后即可看到對應的網頁內容(如圖-5所示)。

圖-5
6 案例6:部署並測試WSGI站點
6.1 問題
本例要求為站點 webapp0.example.com 配置提供動態Web內容,要求如下:
- 此虛擬主機偵聽在端口8909
- 測試網頁從以下地址下載,不要作任何更改http://classroom/pub/materials/webinfo.wsgi
- 從瀏覽器訪問 http://webapp0.example.com:8909 可接收到動態生成的 Web 頁面
- 此站點必須能被 example.com 域內的所有系統訪問
6.2 方案
為httpd增加對Python網頁程序的支持,可以安裝mod_wsgi模塊。關於此模塊的配置說明,建議參考軟件包提供的readme文檔。
在SELinux處於Enforcing模式時,若要開放非80、81等常規Web端口,需要調整SELinux保護策略。
6.3 步驟
實現此案例需要按照如下步驟進行。
步驟一:部署動態網頁文檔
1)創建網頁目錄
- [root@server0 ~]# mkdir /var/www/webapp0
2)部署webinfo.wsgi網頁程序
- [root@server0 ~]# cd /var/www/webapp0
- [root@server0 webapp0]# wget http://classroom/pub/materials/webinfo.wsgi
- .. ..
- 2016-11-27 01:52:26 (16.0 MB/s) - ‘webinfo.wsgi’ saved [397/397]
- [root@server0 webapp0]# cat webinfo.wsgi //檢查下載文件
- #!/usr/bin/env python
- import time
- .. ..
步驟二:配置新的虛擬主機http://webapp0.example.com:8909/
1)安裝mod_wsgi模塊軟件包
- [root@server0 ~]# yum -y install mod_wsgi
- .. ..
2)為新虛擬主機建立配置
- [root@server0 ~]# vim /etc/httpd/conf.d/02-webapp0.conf
- Listen 8909
- <VirtualHost *:8909>
- DocumentRoot /var/www/webapp0
- ServerName webapp0.example.com
- WSGIScriptAlias / /var/www/webapp0/webinfo.wsgi
- </VirtualHost>
3)調整SELinux策略,允許Web服務使用8909端口
列出當前許可的Web端口:
- [root@server0 ~]# semanage port -l | grep ^http_port
- http_port_t tcp 80, 81, 443, 488, 8008, 8009, 8443, 9000
添加新的Web端口:
- [root@server0 ~]# semanage port -a -t http_port_t -p tcp 8909
- [root@server0 ~]#
確認配置結果:
- [root@server0 ~]# semanage port -l | grep ^http_port
- http_port_t tcp 8909, 80, 81, 443, 488, 8008, 8009, 8443, 9000
4)重啟系統服務httpd
- [root@server0 ~]# systemctl restart httpd
- [root@server0 ~]# netstat -antpu | grep httpd //確認已監聽8909端口
- tcp6 0 0 :::443 :::* LISTEN 2477/httpd
- tcp6 0 0 :::8909 :::* LISTEN 2477/httpd
- tcp6 0 0 :::80 :::* LISTEN 2477/httpd
步驟三:測試動態網頁效果
使用elinks或firefox訪問此動態站點http://webapp0.example.com:8909/。
多刷新訪問幾次,每次看到的是動態網頁內容,內容並不固定。
- [root@desktop0 ~]# elinks -dump http://webapp0.example.com:8909/
- UNIX EPOCH time is now: 1480184916.52 //第1次訪問
- [root@desktop0 ~]# elinks -dump http://webapp0.example.com:8909/
- UNIX EPOCH time is now: 1480184919.21 //第2次訪問
- [root@desktop0 ~]# elinks -dump http://webapp0.example.com:8909/
- UNIX EPOCH time is now: 1480184951.99 //第3次訪問