编程语言
首页 > 编程语言> > 使用virtualenv或Python运行mod_wsgi,其版本与系统默认版本不同

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

作者:互联网

我想让我的Flask应用程序在CentOS服务器上运行.基本上问题是我在/usr/bin中安装了Python 2.6,它是系统默认的,并且在/usr/local/bin中安装了Python 3.4.我想使用Python 3.4 virtualenv或至少Python 3.4解释器来运行我的应用程序mod_wsgi.

我在〜/ virtualenvs / flask中创建了virtualenv.

我有这个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

并在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>

但是,在尝试运行应用程序时,出现错误:

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

我注意到正在运行Python 2.6并且我的virtualenv没有被激活.什么是正确的方法来使这个工作,并仍然将Python 2.6作为系统默认值?

解决方法:

您必须在apache.conf中添加以下行,以便提供正确的可执行文件和virtualenv的路径.

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

您将找到这两个命令in the mod_wsgi documentation的所有选项

请注意,您必须具有与python可执行文件兼容的mod_wsgi版本.在您的情况下,您可能必须安装mod_wsgi3.4并配置apache才能使用它而不是标准的mod_wsgi模块.

整个配置文件应该是:

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>

标签:python,flask,apache,mod-wsgi
来源: https://codeday.me/bug/20190926/1819065.html