Python Web应用开发,大多数都是基于Django,Flask等框架。这些Python框架基本符合WSGI规范,文本总结如何将其部署在Apache中呢?
一、什么是WSGI? <python Web Server Gateway Interface>
WSGI是指web服务器和python web应用或web框架之间的标准接口。以提高web应该在一系列web服务器间的移植性。
eb应用处理请求的具体流程:
1、用户操作操作浏览器发送请求;
2、请求转发至对应的web服务器
3、web服务器将请求转交给web应用程序,web应用程序处理请求
4、web应用将请求结果返回给web服务器,由web服务器返回用户响应结果
5、浏览器收到响应,向用户展示

WSGI是一种标准或一种规范:解决Web服务器需要和web应用程序通信的一种协议和标准。
WSGI规定,Web程序必须有一个可调用对象,且该可调用对象接收两个参数,返回一个可迭代对象:
environ:字典,包含请求的所有信息
start_response:在可调用对象中调用的函数,用来发起响应,参数包括状态码,headers等
def hello(environ, start_response):
status = "200 OK"
response_headers = [('Content-Type', 'text/html')]
start_response(status, response_headers)
path = environ['PATH_INFO'][1:] or 'hello'
return [b'<h1> %s </h1>' % path.encode()]
二、如何使用Apache模块mod_wsgi来运行Python WSGI应用。
2.1、如何安装?(采用编译安装,绑定系统)
>> https://github.com/GrahamDumpleton/mod_wsgi/releases下载它各个版本的源码包。
解压后,配置编译选项, 一般采用默认配置即可,即执行: ./configure
如果要指定Apache和Python环境,那你需要加上”–with-apxs”和”–with-python”选项:
... ./configure --with-apxs=/usr/local/apache/bin/apxs --with-python=/usr/local/bin/python
...
编译并安装 sudo make && make install
在Apache配置文件中载入mod_wsgi.so
重启Apache来启用配置 service httpd restart
测试mod_wsgi
最简单的测试方法自然是Hello World,让我们在Apache的DocumentRoot根目录下创建一个文件”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]...
这里的函数application即为WSGI应用对象,它返回的值就是该应用收到请求后的响应。然后,再打开Apache的配置文件httpd.conf,在其最后加上URL路径映射:
WSGIScriptAlias /test /var/www/test.wsgi
这里我们假设Apache的文档根目录是”/var/www”。
现在你可以打开浏览器,访问一下”http://localhost/test”,如果看到”Hello World!”了,就说明mod_wsgi已经安装成功。
我们可以试试运行Flask应用,当然首先是你本地Python环境已经安装了Flask,我们将”test.wsgi”改为:
from flask import Flask
application = Flask(__name__)
@application.route('/')
def index():
return '<h1>Hello World!</h1>'
注意,这里必须要将Flask应用对象命名为”application”,这样才能被mod_wsgi识别。再用浏览器访问下,是不是能看到大标题”Hello World!”?
使用Python虚拟环境
一般我们会将应用安装在虚拟环境中,这样应用的更新只需改变虚拟环境即可,不会影响到其他应用环境。要使用虚拟环境来运行当前WSGI应用的话,你必须在”.wsgi”文件中先执行虚拟环境的启用脚本,基于上面的代码,我们来做如下改动:
activate_this = '/home/bjhee/virtualenv/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))
from flask import Flask
application = Flask(__name__)
@application.route('/')
def index():
return '<h1>Hello World!</h1>'
上例中,我们的虚拟环境在目录”/home/bjhee/virtualenv”下,你可以在其”/bin”子目录中找到启用脚本”activate_this.py”。在WSGI应用的一开始执行它即可。
补充内容
当我们的Python环境中有模块是以.egg压缩包安装的话,WSGI应用运行时需要将.egg压缩包解开。默认的解压路径很有可能没有访问权限,比如Mac下是”/Library/WebServer/.python-eggs”,因此你需要指定临时解压目录。方法有两种,一是在Apache的httpd.conf文件中,使用”WSGIPythonEggs”配置项,配置参数就是我们的临时目录路径;二是设置系统环境变量”PYTHON_EGG_CACHE”。我们建议采用第二种,并将其写在”.wsgi”文件中,这样就不会影响其他的应用:
activate_this = '/home/bjhee/virtualenv/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))
import os
os.environ['PYTHON_EGG_CACHE'] = '/home/bjhee/.python-eggs'
from flask import Flask
application = Flask(__name__)
@application.route('/')
def index():
return '<h1>Hello World!</h1>'
运行前,请确保临时目录(上例中的”/home/bjhee/.python-eggs”)有访问及写权限。
网友评论