flask 应用程序在加载 wsgi 时失败,但在开发服务器上运行良好

flask app fails when loaded with wsgi, but works fine with dev server

当我 运行 像这样时,我的 Flask 应用程序与开发服务器配合得很好:

python manage.py runserver

但是当我尝试使用 wsgi 运行 它时,我得到了各种 "module not installed" 错误,甚至语法错误。奇怪的是,每次我点击应用程序路线时,它都会显示不同的错误。

关于一些错误,从 apache 错误日志的输出中,我可以看到它正在尝试 运行 2.7 版本的软件包,而我正在使用 3.4.

File "/usr/lib/python2.7/dist-packages/requests/__init__.py", line 55, in <module>

我不知道这是为什么,python 3 是服务器上的默认值,

$ python --version
Python 3.4.2

是系统上每个用户的输出。我 使用 virtualenv,但我确实按照以下说明使用 virtualenv 设置 mod_wsgi:http://flask.pocoo.org/docs/0.11/deploying/mod_wsgi/

我的 wsgi 文件如下所示:

import sys

activate_this = '/home/flask-dev/es_app/venv/bin/activate_this.py'
with open(activate_this) as file_:
    exec(file_.read(), dict(__file__=activate_this))

print(sys.path)

from searchapp import app as application

当我 运行 它像 'python run.wsgi':

时 print(sys.path) 输出这个
['/home/flask-dev/es_app/venv/lib/python3.4/site-packages', '/home/flask-dev/es_app', '/usr/lib/python3.4', '/usr/lib/python3.4/plat-x86_64-linux-gnu', '/usr/lib/python3.4/lib-dynload', '/usr/lib/python3/dist-packages']

即看不到 2.7 包的路径。此外,所有必需的模块 安装在我的 virtualenv 中,这就是为什么当我 运行 开发服务器时它工作正常。

完全被这个难住了。任何帮助表示赞赏。

您需要告诉 Apache 它应该使用哪个 Python 可执行文件来启动 python 进程。如果没有显式配置,Apache 可能会使用系统路径中出现的第一个 'python'。 (其实没有,看下面的评论。使用了分发默认值python。

不幸的是,您似乎需要重新编译 mod_wsgi,因为 python exe 只能由 configure 脚本更改。

http://modwsgi.readthedocs.io/en/develop/user-guides/installation-issues.html#multiple-python-versions

你的情况:./configure --with-python=/home/flask-dev/es_app/venv/bin/python

本应简单的事情却需要大量工作。通常我只是在反向代理模式下使用 Apache(感谢 mod_proxy),并将其指向外部 WSGI 服务器,如 Gunicorn(http://gunicorn.org/) or Waitress (http://docs.pylonsproject.org/projects/waitress/en/latest/)。