简体   繁体   English

使用与系统默认版本不同的 virtualenv 或 Python 运行 mod_wsgi

[英]Run mod_wsgi with virtualenv or Python with version different that system default

I am trying to make my Flask application work on CentOS server.我试图让我的 Flask 应用程序在 CentOS 服务器上工作。 Basically the issue is that I have Python 2.6 installed in /usr/bin which is system default and Python 3.4 installed in /usr/local/bin .基本上问题是我在/usr/bin安装了 Python 2.6,这是系统默认值,Python 3.4 安装在/usr/local/bin I would like to use Python 3.4 virtualenv or at least Python 3.4 interpreter for mod_wsgi to run my application.我想为 mod_wsgi 使用 Python 3.4 virtualenv 或至少 Python 3.4 解释器来运行我的应用程序。

I have created virtualenv in ~/virtualenvs/flask .我在~/virtualenvs/flask创建了 virtualenv 。

I have this WSGI script:我有这个 WSGI 脚本:

import os
import sys
from logging import Formatter, FileHandler

APP_HOME = r"/home/fenikso/Album"


activate_this = os.path.join("/home/fenikso/virtualenvs/flask/bin/activate_this.py")
execfile(activate_this, dict(__file__=activate_this))

sys.path.insert(0, APP_HOME)
os.chdir(APP_HOME)

from app import app

handler = FileHandler("app.log")
handler.setFormatter(Formatter("[%(asctime)s | %(levelname)s] %(message)s"))
app.logger.addHandler(handler)
application = app

And following config in Apache:并在Apache中进行以下配置:

<VirtualHost *:80>
        ServerName album2.site.cz
        Alias /static "/home/fenikso/Album/static"
        Alias /photos "/home/fenikso/Album/photos"
        Alias /thumbs "/home/fenikso/Album/thumbs"
        WSGIScriptAlias / "/home/fenikso/Album/wsgi.py"
        <Directory "/home/fenikso/Album">
            AllowOverride None
            Allow from all
        </Directory>
        <Directory "/home/fenikso/Album/static">
            AllowOverride None
            Allow from all
        </Directory>
        <Directory "/home/fenikso/Album/photos">
            AllowOverride None
            Allow from all
        </Directory>
        <Directory "/home/fenikso/Album/thumbs">
            AllowOverride None
            Allow from all
        </Directory>
</VirtualHost>

However, when trying to run the application, I get an error:但是,在尝试运行该应用程序时,出现错误:

Apache/2.2.15 (Unix) DAV/2 mod_wsgi/3.2 Python/2.6.6 mod_fcgid/2.3.7 PHP/5.3.3 mod_ssl/2.2.15 OpenSSL/1.0.1e-fips SVN/1.6.11 mod_perl/2.0.4 Perl/v5.10.1 configured -- resuming normal operations
mod_wsgi (pid=14627): Target WSGI script '/home/fenikso/Album/wsgi.py' cannot be loaded as Python module.
mod_wsgi (pid=14627): Exception occurred processing WSGI script '/home/fenikso/Album/wsgi.py'.
Traceback (most recent call last):
   File "/home/fenikso/Album/wsgi.py", line 15, in <module>
     from app import app
   File "/home/fenikso/Album/app.py", line 1, in <module>
     from flask import Flask
 ImportError: No module named flask

I have noticed that either Python 2.6 is being ran and my virtualenv is not activated.我注意到 Python 2.6 正在运行并且我的 virtualenv 没有被激活。 What would be the proper way to get this working and still have the Python 2.6 as a system default?什么是让这个工作并且仍然将 Python 2.6 作为系统默认值的正确方法?

You have to add the following line in your apache.conf in order to give the right executable and the path to the virtualenv. 您必须在apache.conf中添加以下行,以便提供正确的可执行文件和virtualenv的路径。

WSGIPythonHome /usr/local/bin
WSGIPythonPath /home/fenikso/virtualenv/lib/python3.4/site-packages

You will find all the options of these two command in the mod_wsgi documentation 您将在mod_wsgi文档中找到这两个命令的所有选项

Be aware that you must have the version of mod_wsgi compatible with the python executable. 请注意,您必须具有与python可执行文件兼容的mod_wsgi版本。 In your case, you probably have to install mod_wsgi3.4 and configure apache to use it instead of the standart mod_wsgi module. 在您的情况下,您可能必须安装mod_wsgi3.4并配置apache才能使用它而不是标准的mod_wsgi模块。

The whole configuration file should be : 整个配置文件应该是:

WSGIPythonHome "/usr/local/bin"
WSGIPythonPath "/home/fenikso/virtualenv/lib/python3.4/site-packages"

<VirtualHost *:80>
        ServerName album2.site.cz
        Alias /static "/home/fenikso/Album/static"
        Alias /photos "/home/fenikso/Album/photos"
        Alias /thumbs "/home/fenikso/Album/thumbs"
        WSGIScriptAlias / "/home/fenikso/Album/wsgi.py"
        <Directory "/home/fenikso/Album">
            AllowOverride None
            Allow from all
        </Directory>
        <Directory "/home/fenikso/Album/static">
            AllowOverride None
            Allow from all
        </Directory>
        <Directory "/home/fenikso/Album/photos">
            AllowOverride None
            Allow from all
        </Directory>
        <Directory "/home/fenikso/Album/thumbs">
            AllowOverride None
            Allow from all
        </Directory>
</VirtualHost>

Look into the WSGIPythonHome and WSGIPythonPath directives. 查看WSGIPythonHome和WSGIPythonPath指令。 It's also possible that you have a python2.6 mod_wsgi installed, mod_wsgi must be compiled for the intended python version and does not support multiple python versions . 你也可能安装了python2.6 mod_wsgi,必须为预期的python版本编译mod_wsgi, 并且不支持多个python版本 So check that your mod_wsgi is py3.4 compatible and set the directives above. 因此,请检查您的mod_wsgi是否兼容py3.4并设置上述指令。

Alternatively, you could run the flask app with a python server like gunicorn and proxypass from apache to gunicorn. 或者,您可以运行使用python服务器的烧瓶应用程序,如gunicorn和从apache到gunicorn的proxypass。

Another option, which I believe is much cleaner, logical, and flexible, is to simply reference the python interpreter from your venv at the beginning of your wsgi file.我认为另一个更简洁、合乎逻辑和灵活的选择是在 wsgi 文件的开头简单地从 venv 引用 python 解释器。 This way, it is easy to change (no fiddling with system config files) and opens the possibility for multiple apps running with different python environments, like so:这样,很容易更改(无需摆弄系统配置文件),并为多个应用程序在不同的 Python 环境中运行提供了可能性,如下所示:

#!/path/to/your/venv/bin/python

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM