在Apache中运行Python WSGI应用

我们介绍如何使用Apache模块mod_wsgi来运行Python WSGI应用。

安装mod_wsgi

我们假设你已经有了Apache和Python环境,在Linux或者Mac上,那第一步自然是安装。在Ubuntu或Debian环境中,你可以使用apt-get命令来安装:

sudo apt-get install libapache2-mod-wsgi

sudo apt-get install libapache2-mod-wsgi-py3

不过我们建议采用编译安装,这样在任何系统中都可以安装成功,具体步骤如下:

  • 下载源码包

mod_wsgi的源码托管在Github上,你可以从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

让我们打开Apache的配置文件httpd.conf,默认是在:

$ sudo vi /etc/httpd/conf/httpd.conf   # For Linux
$ sudo vi /etc/apache2/httpd.conf      # For Mac

在所有”Load Module”配置项的最后,加上载入mod_wsgi的配置,注意Linux和Mac的模块加载路径不同:

LoadModule wsgi_module modules/mod_wsgi.so   # For Linux
# LoadModule wsgi_module libexec/apache2/mod_wsgi.so   # For Mac
  • 重启Apache来启用配置
$ sudo service httpd restart       # For Linux
$ sudo service apachectl restart   # For Mac

测试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”)有访问及写权限。

 

posted @ 2017-06-22 09:13  Erick-LONG  阅读(8052)  评论(0编辑  收藏  举报