问题描述
我试图让我的Flask应用程序在CentOS服务器上工作。基本上问题是我已经安装了Python 2.6安装在 / usr / bin
这是系统默认和Python 3.4安装在 / usr / local / bin
。我想使用Python 3.4 virtualenv或者至少Python 3.4解释器来运行我的应用程序。$ b
我已经在〜/ virtualenvs中创建了virtualenv /烧瓶
。
我有这个WSGI脚本:
$ p $ import os
从日志导入sys
导入Formatter,FileHandler
$ b $ 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)
$ b $从应用程序导入应用程序
$ b $处理程序= FileHandler(app.log)
handler.setFormatter( Formatter([%(asctime)s |%(levelname)s]%(message)s))
app.logger.addHandler(handler)
application = app
$ p
以下是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
<目录/ home / fenikso / Album>
AllowOverride None
允许所有
< / Directory>
<目录/ home / fenikso / Album / static>
AllowOverride None
允许所有
< / Directory>
<目录/ home / fenikso / Album / photos>
AllowOverride None
允许所有
< / Directory>
<目录/ home / fenikso / Album / thumbs>
AllowOverride None
允许所有
< / 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 - 恢复正常操作
mod_wsgi(pid = 14627):目标WSGI脚本'/ home / fenikso / Album /wsgi.py不能作为Python模块加载。
mod_wsgi(pid = 14627):处理WSGI脚本/home/fenikso/Album/wsgi.py时发生异常。
Traceback(最近一次调用的最后一个):
在< module>文件中的第15行/home/fenikso/Album/wsgi.py
从应用程序导入应用程序
在< module>文件中的/home/fenikso/Album/app.py行1,
from flask import Flask
ImportError:没有名为flask的模块
我注意到Python 2.6正在运行,而我的virtualenv没有激活。什么是正确的方式来得到这个工作,仍然有Python 2.6作为系统默认?
您必须添加为了给出正确的可执行文件和virtualenv的路径,在你的apache.conf文件中包含以下代码:
WSGIPythonHome / usr / local / bin
WSGIPythonPath /home/fenikso/virtualenv/lib/python3.4/site-packages
您可以在mod_wsgi文档中找到这两个命令的所有选项
a>请注意,您必须具有与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
<目录/ home / fenikso / Album>
AllowOverride None
允许所有
< / Directory>
<目录/ home / fenikso / Album / static>
AllowOverride None
允许所有
< / Directory>
<目录/ home / fenikso / Album / photos>
AllowOverride None
允许所有
< / Directory>
<目录/ home / fenikso / Album / thumbs>
AllowOverride None
允许所有
< / Directory>
< / VirtualHost>
I am trying to make my Flask application work on CentOS server. 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
. I would like to use Python 3.4 virtualenv or at least Python 3.4 interpreter for mod_wsgi to run my application.
I have created virtualenv in ~/virtualenvs/flask
.
I have this WSGI script:
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:
<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. What would be the proper way to get this working and still have the Python 2.6 as a system default?
You have to add the following line in your apache.conf in order to give the right executable and the path to the 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
Be aware that you must have the version of mod_wsgi compatible with the python executable. In your case, you probably have to install mod_wsgi3.4 and configure apache to use it instead of the standart mod_wsgi module.
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>
这篇关于使用virtualenv或Python运行mod_wsgi,其版本与系统默认的版本不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!