5案例5:部署並測試WSGI站點
5.1問題
本例要求爲站點webapp0.example.com配置提供動態Web內容,要求如下:
- 此虛擬主機偵聽在端口8909
- 測試網頁從以下地址下載,不要在做任何更改http://classroom/pub/material/webinfo.wsgi
- 從瀏覽器訪問http://webapp0.example.com:8909可接收到動態生成的Web頁面
- 此站點必須能被example.com域內的所有系統訪問
5.2方案
爲httpd增加對Python網頁程序的支持,可以安裝mod_wsgi模塊。關於此模塊的配置說明,建議參考軟件包提供的readme文檔。
在SELinux處於Enforcing模式時,若要開放非80、81等常規Web端口,需要調整SELinux保護策略。
5.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次訪問