我有两个不相关的 mod_wsgi Django 应用程序,需要从 Apache2 的一个实例中的两个虚拟主机提供服务。
我已经让每个虚拟主机,每个都有自己的目录,settings.py 和 wsgi.py。
我一直看到的错误是:
[Wed Jun 03 16:31:48 2015] [error] [client 1.2.3.4] mod_wsgi (pid=6375): Exception occurred processing WSGI script '/srv/app1/app1/wsgi.py'., referer: http://app1.example.com/admin/mdtconfig/locationtoservice/
[Wed Jun 03 16:31:48 2015] [error] [client 1.2.3.4] Traceback (most recent call last):, referer: http://app1.example.com/admin/mdtconfig/locationtoservice/
[Wed Jun 03 16:31:48 2015] [error] [client 1.2.3.4] File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/wsgi.py", line 219, in __call__, referer: http://app1.example.com/admin/mdtconfig/locationtoservice/
[Wed Jun 03 16:31:48 2015] [error] [client 1.2.3.4] self.load_middleware(), referer: http://app1.example.com/admin/mdtconfig/locationtoservice/
[Wed Jun 03 16:31:48 2015] [error] [client 1.2.3.4] File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 39, in load_middleware, referer: http://app1.example.com/admin/mdtconfig/locationtoservice/
[Wed Jun 03 16:31:48 2015] [error] [client 1.2.3.4] for middleware_path in settings.MIDDLEWARE_CLASSES:, referer: http://app1.example.com/admin/mdtconfig/locationtoservice/
[Wed Jun 03 16:31:48 2015] [error] [client 1.2.3.4] File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 184, in inner, referer: http://app1.example.com/admin/mdtconfig/locationtoservice/
[Wed Jun 03 16:31:48 2015] [error] [client 1.2.3.4] self._setup(), referer: http://app1.example.com/admin/mdtconfig/locationtoservice/
[Wed Jun 03 16:31:48 2015] [error] [client 1.2.3.4] File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 42, in _setup, referer: http://app1.example.com/admin/mdtconfig/locationtoservice/
[Wed Jun 03 16:31:48 2015] [error] [client 1.2.3.4] self._wrapped = Settings(settings_module), referer: http://app1.example.com/admin/mdtconfig/locationtoservice/
[Wed Jun 03 16:31:48 2015] [error] [client 1.2.3.4] File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 95, in __init__, referer: http://app1.example.com/admin/mdtconfig/locationtoservice/
[Wed Jun 03 16:31:48 2015] [error] [client 1.2.3.4] raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e)), referer: http://app1.example.com/admin/mdtconfig/locationtoservice/
[Wed Jun 03 16:31:48 2015] [error] [client 1.2.3.4] ImportError: Could not import settings 'app0.settings' (Is it on sys.path?): No module named app0.settings, referer: http://app1.example.com/admin/mdtconfig/locationtoservice/
app0.wsgi:
import os
import sys
sys.path.append('/srv/app0')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "app0.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
app1.wsgi:
import sys
sys.path.append('/srv/app1')
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "app1.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
这两个应用程序怎么会变得混乱?
来自 app1 的页面请求抱怨它无法从 app0 加载设置 - 为什么还要尝试?
更新:
我特别担心每个 wsgi.py 都将 DJANGO_SETTINGS_MODULE 设置为它自己的设置名称 - 这是问题的原因吗?
读:
不使用
os.environ.setdefault()
,使用os.environ[]
。也使用 mod_wsgi 的守护模式而不是嵌入模式。
要在更新后添加此内容,环境变量的设置通常不是问题,因为您的两个应用程序将在进程的不同子解释器上下文中运行。它失败的地方是因为您正在使用它,因为
os.environ.setdefault()
如果已经设置了环境变量,它将不会做任何事情。环境变量实际上是进程范围的,这意味着首先设置它的 WSGI 脚本文件将获胜。使用时不会出现此问题,os.environ[]
因为它将覆盖子解释器上下文的环境变量并忽略从进程范围级别继承的环境变量,因此请阅读博客文章并按照说明进行操作。