我试图使我的Flask应用程序在CentOS服务器上工作。基本上,问题是我安装了python 2.6,/usr/bin这是系统默认值,而Python 3.4则安装在/usr/local/bin。我想对mod_wsgi使用Python 3.4 virtualenv或至少Python 3.4解释器来运行我的应用程序。
/usr/bin
/usr/local/bin
我在中创建了virtualenv ~/virtualenvs/flask。
~/virtualenvs/flask
我有这个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
您可以在mod_wsgi文档中找到这两个命令的所有选项。
请注意,您必须具有与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>