我想让我的Flask应用程序在CentOS服务器上运行。基本上问题是我在/usr/bin
中安装了Python 2.6,它是/usr/local/bin
中安装的系统默认和Python 3.4。我想使用Python 3.4 virtualenv或至少Python 3.4解释器来运行我的应用程序。
我在~/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作为系统默认值?
答案 0 :(得分:9)
您必须在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>
答案 1 :(得分:2)
查看WSGIPythonHome and WSGIPythonPath指令。您也可能安装了python2.6 mod_wsgi,必须为预期的python版本和does not support multiple python versions编译mod_wsgi。因此,请检查您的mod_wsgi是否与py3.4兼容并设置上述指令。
或者,您可以使用python服务器运行烧瓶应用程序,如gunicorn和proxypass,从apache到gunicorn。