我写了一个Flask应用程序,使用了一个可以通过环境变量配置的外部库。注意:这个外部库是我自己写的,所以如果需要的话,我可以进行更改。当从命令行运行并使用以下命令运行Flask服务器时:
# env = python virtual environment
ENV_VAR=foo ./env/bin/python myapp/webui.py
一切都按预期运行。但是在部署到 Apache 并使用 SetEnv
后,它就不再起作用了。事实上,将 os.environ
打印到 stderr
(以便它出现在 Apache 日志中)可以发现,wsgi
进程似乎处在一个非常不同的环境中(例如,os.environ['PWD']
看起来相差很远。实际上,它指向我的开发文件夹。
为了帮助确定问题,以下是应用程序的相关部分,作为一个独立的 hello-world 应用程序。错误输出和观察结果在帖子的最后。
应用程序文件夹布局:
Python 应用程序:
.
├── myapp.ini
├── setup.py
└── testenv
├── __init__.py
├── model
│ └── __init__.py
└── webui.py
Apache文件夹 (/var/www/michel/testenv
):
.
├── env
│ ├── [...]
├── logs
│ ├── access.log
│ └── error.log
└── wsgi
└── app.wsgi
myapp.ini
[app]
somevar=somevalue
setup.py
from setuptools import setup, find_packages
setup(
name="testenv",
version='1.0dev1',
description="A test app",
long_description="Hello World!",
author="Some Author",
author_email="author@example.com",
license="BSD",
include_package_data=True,
install_requires = [
'flask',
],
packages=find_packages(exclude=["tests.*", "tests"]),
zip_safe=False,
)
测试环境/初始化.py
# empty
测试环境/模型/init.py
from os.path import expanduser, join, exists
from os import getcwd, getenv, pathsep
import logging
import sys
__version__ = '1.0dev1'
LOG = logging.getLogger(__name__)
def find_config():
"""
Searches for an appropriate config file. If found, return the filename, and
the parsed search path
"""
path = [getcwd(), expanduser('~/.mycompany/myapp'), '/etc/mycompany/myapp']
env_path = getenv("MYAPP_PATH")
config_filename = getenv("MYAPP_CONFIG", "myapp.ini")
if env_path:
path = env_path.split(pathsep)
detected_conf = None
for dir in path:
conf_name = join(dir, config_filename)
if exists(conf_name):
detected_conf = conf_name
break
return detected_conf, path
def load_config():
"""
Load the config file.
Raises an OSError if no file was found.
"""
from ConfigParser import SafeConfigParser
conf, path = find_config()
if not conf:
raise OSError("No config file found! Search path was %r" % path)
parser = SafeConfigParser()
parser.read(conf)
LOG.info("Loaded settings from %r" % conf)
return parser
try:
CONF = load_config()
except OSError, ex:
# Give a helpful message instead of a scary stack-trace
print >>sys.stderr, str(ex)
sys.exit(1)
测试环境/web界面.py
from testenv.model import CONF
from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
return "Hello World %s!" % CONF.get('app', 'somevar')
if __name__ == '__main__':
app.debue = True
app.run()
Apache配置
<VirtualHost *:80>
ServerName testenv-test.my.fq.dn
ServerAlias testenv-test
WSGIDaemonProcess testenv user=michel threads=5
WSGIScriptAlias / /var/www/michel/testenv/wsgi/app.wsgi
SetEnv MYAPP_PATH /var/www/michel/testenv/config
<Directory /var/www/michel/testenv/wsgi>
WSGIProcessGroup testenv
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
ErrorLog /var/www/michel/testenv/logs/error.log
LogLevel warn
CustomLog /var/www/michel/testenv/logs/access.log combined
</VirtualHost>
app.wsgi
->应用程序.wsgi
activate_this = '/var/www/michel/testenv/env/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))
from os import getcwd
import logging, sys
from testenv.webui import app as application
# You may want to change this if you are using another logging setup
logging.basicConfig(stream=sys.stderr, level=logging.DEBUG)
LOG = logging.getLogger(__name__)
LOG.debug('Current path: {0}'.format(getcwd()))
# Application config
application.debug = False
# vim: set ft=python :
错误和观察
这是 Apache 错误日志的输出。
[Thu Jan 26 10:48:15 2012] [error] No config file found! Search path was ['/home/users/michel', '/home/users/michel/.mycompany/myapp', '/etc/mycompany/myapp']
[Thu Jan 26 10:48:15 2012] [error] [client 10.115.192.101] mod_wsgi (pid=17946): Target WSGI script '/var/www/michel/testenv/wsgi/app.wsgi' cannot be loaded as Python module.
[Thu Jan 26 10:48:15 2012] [error] [client 10.115.192.101] mod_wsgi (pid=17946): SystemExit exception raised by WSGI script '/var/www/michel/testenv/wsgi/app.wsgi' ignored.
[Thu Jan 26 10:48:15 2012] [error] [client 10.115.192.101] Traceback (most recent call last):
[Thu Jan 26 10:48:15 2012] [error] [client 10.115.192.101] File "/var/www/michel/testenv/wsgi/app.wsgi", line 10, in <module>
[Thu Jan 26 10:48:15 2012] [error] [client 10.115.192.101] from testenv.webui import app as application
[Thu Jan 26 10:48:15 2012] [error] [client 10.115.192.101] File "/var/www/michel/testenv/env/lib/python2.6/site-packages/testenv-1.0dev1-py2.6.egg/testenv/webui.py", line 1, in <module>
[Thu Jan 26 10:48:15 2012] [error] [client 10.115.192.101] from testenv.model import CONF
[Thu Jan 26 10:48:15 2012] [error] [client 10.115.192.101] File "/var/www/michel/testenv/env/lib/python2.6/site-packages/testenv-1.0dev1-py2.6.egg/testenv/model/__init__.py", line 51, in <module>
[Thu Jan 26 10:48:15 2012] [error] [client 10.115.192.101] sys.exit(1)
[Thu Jan 26 10:48:15 2012] [error] [client 10.115.192.101] SystemExit: 1
我的第一个观察是环境变量MYAPP_PATH
在os.environ
中没有出现(尽管这在输出中不可见,但我测试过了,它确实不存在!)。因此,“解析器”将回退到默认路径。我的第二个观察是配置文件的搜索路径将
/home/users/michel
列为os.getcwd()
的返回值。实际上,我期望得到的是/var/www/michel/testenv
中的某些内容。我的直觉告诉我,我所做的配置解析方式不正确。主要是因为代码在导入时执行。这让我想到,也许配置解析代码是在WSGI环境正确设置之前执行的。我正在追求正确的方向吗?
短暂的讨论/旁观问题
在这种情况下,你会怎样进行配置解析?考虑到“model”子文件夹实际上是一个外部模块,应该在非WSGI应用程序中也起作用,并且应该提供一种配置数据库连接的方法。
就个人而言,我喜欢搜索配置文件的方式,同时仍然能够对其进行覆盖。只是代码在导入时执行的事实让我的蜘蛛感觉异常强烈。其背后的逻辑是:通过使用此模块的其他开发人员完全隐藏了配置处理(抽象屏障),它“只是工作”。他们只需要导入模块(当然还要有现有的配置文件),就可以直接开始而不知道任何数据库细节。这也为他们提供了一种轻松的方法来使用不同的数据库(dev/test/deployment)并轻松地在它们之间切换。
现在,在mod_wsgi中不再如此 :(
更新:
刚才,为了测试我上面的想法,我将
webui.py
更改如下:import os
from flask import Flask, jsonify
app = Flask(__name__)
@app.route('/')
def index():
return jsonify(os.environ)
if __name__ == '__main__':
app.debue = True
app.run()
网页上的输出如下:{
LANG: "C",
APACHE_RUN_USER: "www-data",
APACHE_PID_FILE: "/var/run/apache2.pid",
PWD: "/home/users/michel/tmp/testenv",
APACHE_RUN_GROUP: "www-data",
PATH: "/usr/local/bin:/usr/bin:/bin",
HOME: "/home/users/michel/"
}
这显示了与其他调试方法所看到的相同环境。因此,我的初步想法是错误的。但现在我意识到了更奇怪的事情。 os.environment ['PWD']
设置为我拥有开发文件的文件夹。这根本不是应用程序运行的位置。更奇怪的是,os.getcwd()
返回 /home/users/michel
?这与我在 os.environ
中看到的不一致。它不应该与 os.environ ['PWD']
相同吗?最重要的问题仍然是:为什么没有在
os.environ
中找到由apache的 SetEnv
(在这种情况下为MYAPP_PATH
)设置的值?