Apache配置WSGI
什么是WSGI
WSGI被稱作web服務器網關接口,在筆者看來其實是基於CGI標准針對Python語言做了一些改進,其主要功能是規范了web 服務器與Pythonj應用程序之間的交互方式,為Python在web開發方面提供了便利而已。關於WSGI原生開發可以閱讀參考部分的第一個鏈接。本文主要講解如何配置WSGI,從而使得Apache服務器能夠支持Python程序。
操作環境
操作系統:ubuntu 16
Apache服務器:Apache 2.4.18
Python:2.7.12
安裝與加載
安裝WSGI模塊
# sudo apt install libapache2-mod-wsgi
啟用WSGI模塊
# sudo a2enmod wsgi
配置WSGI
編輯文件/etc/apache2/mods-enabled/wsgi.conf
<IfModule mod_wsgi.c> WSGIScriptAlias /test /var/www/html/test.wsgi #添加該行
</IfModule>
編寫測試腳本
在/var/www/html目錄下創建test.wsgi文件,在文件中添加以下代碼
def application(environ, start_response):
status = '200 OK'
output = 'Hello World!'
response_headers = [('Content-type', 'text/plain'),
('Content-Length', str(len(output)))]
start_response(status, response_headers)
return [output]
訪問
在瀏覽器中訪問http://地址/test ,可以看到頁面上顯示“Hello World!”。