一个 Apache 上的两个 mod_wsgi Django 应用程序正在尝试使用彼此的配置文件

一个 Apache 上的两个 mod_wsgi Django 应用程序正在尝试使用彼此的配置文件

我有两个不相关的 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/

应用程序0.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()

应用程序1.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 设置为它自己的设置名称 - 这是问题的原因吗?

答案1

读:

不要使用os.environ.setdefault(),使用os.environ[]

也使用 mod_wsgi 的守护进程模式而不是嵌入模式。


在您更新后,环境变量的设置通常不是问题,因为您的两个应用程序将在进程的不同子解释器上下文中运行。失败的原因是因为您正在使用,os.environ.setdefault()因为如果环境变量已经设置,这将不起作用。该环境变量实际上是进程范围的,这意味着首先设置它的 WSGI 脚本文件将获胜。当您使用时不会发生此问题,os.environ[]因为它将覆盖子解释器上下文的环境变量并忽略从进程范围级别继承的环境变量,因此请阅读博客文章并按照说明进行操作。

相关内容