1. 程式人生 > >06: HTTP服務基礎 網頁內容訪問 部署動態網站 總結和答疑

06: HTTP服務基礎 網頁內容訪問 部署動態網站 總結和答疑

Top

NSD ENGINEER DAY06

  1. 案例1:獨立Web站點的快速部署
  2. 案例2:虛擬Web主機的部署
  3. 案例3:配置網頁內容訪問
  4. 案例4:使用自定Web根目錄
  5. 案例5:部署並測試WSGI站點

1 案例1:獨立Web站點的快速部署

1.1 問題

本例要求為 http://server0.example.com 配置Web站點,要求如下:

  1. 從http://classroom/pub/materials/station.html下載一個主頁檔案,將其重新命名為 index.html
  2. 將此檔案拷貝到站點的 DocumentRoot 目錄下,不要對檔案 index.html 的內容作任何修改
  3. 使用 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,統一資源定位器)網址的基本組成:

 
  1. http://伺服器地址[:埠號]/目錄/檔名

對於需要驗證的FTP資源,還需要指定使用者名稱密碼資訊:

 
  1. ftp://使用者名稱:密碼@伺服器地址[:埠號]/目錄/檔名

1.3 步驟

實現此案例需要按照如下步驟進行。

步驟一:構建及部署網站伺服器

1)安裝軟體包httpd

 
  1. [[email protected] ~]# yum -y install httpd
  2. .. ..

2)部署網頁

 
  1. [[email protected] ~]# cd /var/www/html/                     //進入網頁目錄
  2. [[email protected] html]# wget http://classroom/pub/materials/station.html -O index.html                                                 //下載網頁
  3. .. ..
  4. 2016-11-26 19:33:49 (1.36 MB/s) - ‘index.html’ saved [14/14]
  5.  
  6. [[email protected] html]# cat index.html                     //檢查網頁檔案
  7. Default Site.

3)啟動系統服務httpd,並設定開機自啟

 
  1. [[email protected] html]# systemctl restart httpd
  2. [[email protected] html]# systemctl enable httpd
  3. ln -s '/usr/lib/systemd/system/httpd.service' '/etc/systemd/system/multi-user.target.wants/httpd.service'

步驟二:訪問網站伺服器

1)使用elinks瀏覽器檢視

Elinks瀏覽器可以在命令列模式顯示出網頁文字,經常用來測試網站的可用性。

 
  1. [[email protected] ~]# yum -y install elinks                     //安裝elinks
  2. .. ..
  3. [[email protected] ~]# elinks -dump http://server0.example.com/     //訪問指定網址
  4. Default Site.

2)使用firefox瀏覽器檢視

Firefox瀏覽器支援更多網頁特性,是訪問複雜網頁、網址的優秀工具。

在桌面終端直接執行“firefox http://server0.examle.com/”,或者通過選單快捷方式開啟Firefox瀏覽器再輸入對應網址,都可以看到目標網頁(如圖-1所示)。

圖-1

2 案例2:虛擬Web主機的部署

2.1 問題

本例要求為server0擴充套件Web站點,新建虛擬主機 http://www0.example.com,具體要求如下:

  1. 設定 DocumentRoot 為 /var/www/virtual
  2. 從 http://classroom/pub/materials/www.html 下載主頁檔案,並重命名為 index.html
  3. 不要對檔案 index.html 的內容作任何修改,將其放到此虛擬主機的 DocumentRoot 目錄下
  4. 確保 fleyd 使用者能在 /var/www/virtual 目錄建檔案
  5. 確保站點 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):

 
  1. <VirtualHost *:80>
  2.      ServerName 網站1的FQDN
  3.      DocumentRoot 網站1的網頁根目錄
  4. </VirtualHost>
  5. <VirtualHost *:80>
  6.      ServerName 網站2的FQDN
  7.      DocumentRoot 網站2的網頁根目錄
  8. </VirtualHost>
  9. .. ..

2.3 步驟

實現此案例需要按照如下步驟進行。

步驟一:部署網頁文件

1)建立網頁目錄

 
  1. [[email protected] ~]# mkdir /var/www/virtual
  2. [[email protected] ~]# useradd fleyd
  3. [[email protected] ~]# setfacl -m u:fleyd:rwx /var/www/virtual/

2)部署網頁檔案

 
  1. [[email protected] ~]# cd /var/www/virtual/
  2. [[email protected] virtual]# wget http://classroom/pub/materials/www.html -O index.html
  3. .. ..
  4. 100%[=====================>] 14 --.-K/s in 0s
  5.  
  6. 2016-11-26 20:01:14 (826 KB/s) - ‘index.html’ saved [14/14]
  7. [[email protected] virtual]# cat index.html                 //檢查網頁檔案
  8. Virtual Site.

步驟二:配置虛擬主機 http://www0.example.com/

1)為新站點建立獨立的配置檔案

 
  1. [[email protected] virtual]# vim /etc/httpd/conf.d/01-www0.conf
  2. <VirtualHost *:80>
  3. ServerName www0.example.com
  4. DocumentRoot /var/www/virtual
  5. </VirtualHost>
  6. [[email protected] virtual]# httpd -t                             //確保語法檢查OK
  7. Syntax OK

2)重啟系統服務httpd

 
  1. [[email protected] virtual]# systemctl restart httpd

步驟三:訪問虛擬主機 http://www0.example.com/

訪問此虛擬站點,可以看到預期的網頁內容:

 
  1. [[email protected] ~]# elinks -dump http://www0.example.com/
  2. Virtual Site.

步驟四:完善原始站點 http://server0.example.com/

需要注意的是,原始的獨立站點可能出現異常,訪問時並不是原始的網頁:

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

原因是一旦啟用虛擬站點機制以後:

  • 外部的 DocumentRoot、ServerName 會被忽略
  • 第1個虛擬站點被視為預設站點,若客戶機請求的URL不屬於任何已知站點,則由第1個站點響應

若要解決此異常,需要將原始站點轉換為第一個虛擬主機,啟用順序的設定可以通過檔名開頭的數字來實現。

1)為原始站點建立虛擬主機配置

 
  1. [[email protected] ~]# vim /etc/httpd/conf.d/00-default.conf
  2. <VirtualHost *:80>
  3. ServerName server0.example.com
  4. DocumentRoot /var/www/html
  5. </VirtualHost>

2)重啟系統服務httpd

 
  1. [[email protected] virtual]# systemctl restart httpd

3)訪問兩個虛擬站點,確保各自的網頁內容正確

 
  1. [[email protected] ~]# elinks -dump http://server0.example.com/
  2. Default Site.
  3. [[email protected] ~]# elinks -dump http://www0.example.com/
  4. Virtual Site.

3 案例3:配置網頁內容訪問

3.1 問題

本例要求在 Web 網站 http://server0.example.com 的 DocumentRoot 目錄下建立一個名為 private 的子目錄,要求如下:

  1. 從 http://classroom/pub/materials/private.html 下載一個檔案副本到這個目錄,重新命名為 index.html
  2. 不要對檔案 index.html 的內容作任何修改
  3. 從 server0 上,任何人都可以瀏覽 private 的內容,但是從其他系統不能訪問這個目錄的內容

3.2 方案

配置Web內容的訪問控制需要新增Directory區段,主要形式可參考

 
  1. <Directory "父目錄路徑">
  2. Require all denied                                 //上層目錄拒絕任何訪問
  3. </Directory>
  4. <Directory "子目錄1路徑">
  5. Require all granted                             //子目錄1允許任何訪問
  6. </Directory>
  7. <Directory "子目錄2路徑">
  8. Require ip IP或網段地址 .. ..                     //子目錄2允許少數客戶機
  9. </Directory>

3.3 步驟

實現此案例需要按照如下步驟進行。

步驟一:部署網頁子目錄及文件

1)建立子目錄

 
  1. [[email protected] ~]# mkdir /var/www/html/private

2)部署網頁

 
  1. [[email protected] ~]# cd /var/www/html/private
  2. [[email protected] private]# wget http://classroom/pub/materials/private.html -O index.html
  3. .. ..
  4. 2016-11-26 20:30:28 (1.90 MB/s) - ‘index.html’ saved [14/14]
  5.  
  6. [[email protected] private]# cat index.html                 //檢查網頁檔案
  7. Private Site.

步驟二:為指定的網頁子目錄限制訪問

在httpd服務的標準配置中,根目錄 / 預設拒絕任何訪問,但網頁目錄/var/www/預設允許任何訪問。因此,只需要為個別子目錄增加訪問控制即可。

1)調整虛擬站點server0.example.com的配置檔案

 
  1. [[email protected] ~]# vim /etc/httpd/conf.d/00-default.conf
  2. .. ..
  3. <Directory "/var/www/html/private">
  4. Require ip 127.0.0.1 ::1 172.25.0.11
  5. </Directory>

2)重啟系統服務httpd

 
  1. [[email protected] ~]# systemctl restart httpd

步驟三:測試目錄訪問限制

1)從desktop0上訪問http://server0.example.com/private/被拒絕

 
  1. [[email protected] ~]# elinks -dump http://server0.example.com/private/
  2. Forbidden
  3.  
  4. You don't have permission to access /private/ on this server.

2)從desktop0上訪問http://server0.example.com/仍然是正常的

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

3)從server0本機上訪問http://server0.example.com/private/也不受限制

 
  1. [[email protected] ~]# elinks -dump http://server0.example.com/private/
  2. Private Site.

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)參照標準目錄,重設新目錄的屬性

 
  1. chcon [-R] --reference=模板目錄 新目錄

或者

2)將新目錄增加到預設的標準Web目錄範圍

 
  1. semanage fcontext -a -t httpd_sys_content_t '新目錄(/.*)?'

4.3 步驟

實現此案例需要按照如下步驟進行。

步驟一:部署網頁目錄及文件

1)建立網頁目錄

 
  1. [[email protected] ~]# mkdir /webroot

2)部署網頁檔案

 
  1. [[email protected] ~]# cd /webroot/
  2. [[email protected] webroot]# wget http://classroom/pub/materials/station.html -O index.html
  3. .. ..
  4. 2016-11-26 20:01:14 (826 KB/s) - ‘index.html’ saved [14/14]
  5. [[email protected] webroot]# cat index.html                 //檢查網頁檔案
  6. Default Site.

步驟二:調整虛擬站點http://server0.example.com/的配置

1)修改配置檔案

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

2)重啟系統服務httpd

 
  1. [[email protected] ~]# systemctl restart httpd

步驟三:確保虛擬站點http://server0.example.com/仍然可以訪問

1)未調整網頁目錄SELinux上下檔案的情況

為虛擬站點http://server0.example.com/更換了新的網頁目錄以後,從瀏覽器訪問將會失敗,只能看到紅帽測試頁。

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

針對此問題,可以參考目錄/var/www的屬性為網頁目錄/webroot設定SELinux安全上下文。

 
  1. [[email protected] ~]# chcon -R --reference=/var/www /webroot/
  2. [[email protected] ~]# ls -Z /webroot/index.html                 //確認結果
  3. -rw-r--r--. root root system_u:object_r:httpd_sys_content_t:s0 /webroot/index.html

2)未配置目錄內容訪問的情況

儘管已經調整過/webroot的SELinux安全上下文,但是從瀏覽器訪問此虛擬站點時仍然會被拒絕,還是隻能看到紅帽測試頁。

還需要修改對應的配置檔案,新增內容訪問控制:

 
  1. [[email protected] ~]# vim /etc/httpd/conf.d/00-default.conf
  2. <VirtualHost *:80>
  3. ServerName server0.example.com
  4. DocumentRoot /webroot
  5. </VirtualHost>
  6. <Directory "/webroot">
  7. Require all granted
  8. </Directory>
  9. <Directory "/webroot/private">
  10. Require ip 127.0.0.1 ::1 172.25.0.11
  11. </Directory>
  12.  
  13. [[email protected] ~]# systemctl restart httpd             //重啟httpd服務

若要保持原有private子目錄,建議也拷貝過來:

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

3)最終訪問測試

從瀏覽器能成功訪問調整後的虛擬站點http://server0.example.com/。

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

5 案例5:部署並測試WSGI站點

5.1 問題

本例要求為站點 webapp0.example.com 配置提供動態Web內容,要求如下:

  1. 此虛擬主機偵聽在埠8909
  2. 測試網頁從以下地址下載,不要作任何更改http://classroom/pub/materials/webinfo.wsgi
  3. 從瀏覽器訪問 http://webapp0.example.com:8909 可接收到動態生成的 Web 頁面
  4. 此站點必須能被 example.com 域內的所有系統訪問

5.2 方案

為httpd增加對Python網頁程式的支援,可以安裝mod_wsgi模組。關於此模組的配置說明,建議參考軟體包提供的readme文件。

在SELinux處於Enforcing模式時,若要開放非80、81等常規Web埠,需要調整SELinux保護策略。

5.3 步驟

實現此案例需要按照如下步驟進行。

步驟一:部署動態網頁文件

1)建立網頁目錄

 
  1. [[email protected] ~]# mkdir /var/www/webapp0

2)部署webinfo.wsgi網頁程式

 
  1. [[email protected] ~]# cd /var/www/webapp0
  2. [[email protected] webapp0]# wget http://classroom/pub/materials/webinfo.wsgi
  3. .. ..
  4. 2016-11-27 01:52:26 (16.0 MB/s) - ‘webinfo.wsgi’ saved [397/397]
  5.  
  6. [[email protected] webapp0]# cat webinfo.wsgi                 //檢查下載檔案
  7. #!/usr/bin/env python
  8. import time
  9. .. ..

步驟二:配置新的虛擬主機http://webapp0.example.com:8909/

1)安裝mod_wsgi模組軟體包

 
  1. [[email protected] ~]# yum -y install mod_wsgi
  2. .. ..

2)為新虛擬主機建立配置

 
  1. [[email protected] ~]# vim /etc/httpd/conf.d/02-webapp0.conf
  2. Listen 8909
  3. <VirtualHost *:8909>
  4. DocumentRoot /var/www/webapp0
  5. ServerName webapp0.example.com
  6. WSGIScriptAlias / /var/www/webapp0/webinfo.wsgi
  7. </VirtualHost>

3)調整SELinux策略,允許Web服務使用8909埠

列出當前許可的Web埠:

 
  1. [[email protected] ~]# semanage port -l | grep ^http_port
  2. http_port_t tcp 80, 81, 443, 488, 8008, 8009, 8443, 9000

新增新的Web埠:

 
  1. [[email protected] ~]# semanage port -a -t http_port_t -p tcp 8909
  2. [[email protected] ~]#

確認配置結果:

 
  1. [[email protected] ~]# semanage port -l | grep ^http_port
  2. http_port_t tcp 8909, 80, 81, 443, 488, 8008, 8009, 8443, 9000

4)重啟系統服務httpd

 
  1. [[email protected] ~]# systemctl restart httpd
  2. [[email protected] ~]# netstat -antpu | grep httpd         //確認已監聽8909埠
  3. tcp6 0 0 :::443 :::* LISTEN 2477/httpd
  4. tcp6 0 0 :::8909 :::* LISTEN 2477/httpd
  5. tcp6 0 0 :::80 :::* LISTEN 2477/httpd

步驟三:測試動態網頁效果

使用elinks或firefox訪問此動態站點http://webapp0.example.com:8909/。

多重新整理訪問幾次,每次看到的是動態網頁內容,內容並不固定。

 
  1. [[email protected] ~]# elinks -dump http://webapp0.example.com:8909/
  2. UNIX EPOCH time is now: 1480184916.52                     //第1次訪問
  3. [[email protected] ~]# elinks -dump http://webapp0.example.com:8909/
  4. UNIX EPOCH time is now: 1480184919.21                     //第2次訪問
  5. [[email protected] ~]# elinks -dump http://webapp0.example.com:8909/
  6. UNIX EPOCH time is now: 1480184951.99                     //第3次訪問