12

我目前正在使用通过 Apache 托管在同一域(但在不同端口上提供服务)的两个 Django 应用程序(比如A& )。B我相信我的设置是正确的,但是我在两个站点上随机获得了 500 个。500 on A(例如)主要发生在请求被服务之后B(反之亦然)。

在检查错误日志(例如)时,我A看到wsgiA模块正在尝试访问其他方式也是,B 的 wsgi 引发了一个异常,抱怨缺少 A 的 settings.py 文件]。我不确定他们为什么要寻找其他设置文件,所有视图上的导入(用于 settings.py)都特定于各自的项目。Bsettings.py

这是我的设置:

A正在港口上送达8080B正在港口上送达80

虚拟主机:

<VirtualHost *:8080>

  ServerAdmin x@x.net
  ServerName  string1

  Alias /static/ /home/PATH_TO_PROJECT_A/static/

  <Directory /home/PATH_TO_PROJECT_A/static>
  Order deny,allow
  Allow from all
  </Directory>

  WSGIScriptAlias / /home/PATH_TO_PROJECT_A/wsgi.py

  <Directory /home/PATH_TO_PROJECT_A>
  <Files wsgi.py>
  Order deny,allow
  Allow from all
  </Files>
  </Directory>


  LogLevel warn
  ErrorLog  /SOME_PATH/errorA.log
  CustomLog /SOME_PATH/accessA.log combined
</VirtualHost>


<VirtualHost *:80>
  ServerName string1
  ServerAdmin x@x.net

  Alias /APP_B/static/ /home/PATH_TO_PROJECT_B/static/

  <Directory /home/PATH_TO_PROJECT_B/static>
  Order deny,allow
  Allow from all
  </Directory>

  WSGIScriptAlias /APP_B /home/PATH_TO_PROJECT_B/wsgi.py/

  <Directory /home/PATH_TO_PROJECT_B>
  <Files wsgi.py>
  Order deny,allow
  Allow from all
  </Files>
  </Directory>

  ErrorLog /home/SOME_PATH/error2.log
  CustomLog /home/SOME_PATH/access2.log combined

  # All other files on B:80 (other than /APP_B are served normally
  DocumentRoot /home/foo/public_html/xyz/public

</VirtualHost>

端口.conf:

NameVirtualHost *:8080
Listen 8080
Listen 80

<IfModule mod_ssl.c>
 Listen 443
</IfModule>

<IfModule mod_gnutls.c>
 Listen 443
</IfModule>

'A' 上的 wsgi.py:

import os, sys

sys.path.append('home/PATH_TO_PROJECT_A') #1

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "PROJECT_A.settings") #2

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

在 B 上完全相同,只是对第 1 行和第 2 行进行了更改。

例如,我从error.log说中得到的错误来自A

 [Sun Aug 26 17:01:49 2012] [error] [client x] Traceback (most recent call last):
 [Sun Aug 26 17:01:49 2012] [error] [client x]   File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/wsgi.py", line 219, in __call__
 [Sun Aug 26 17:01:49 2012] [error] [client x]     self.load_middleware()
 [Sun Aug 26 17:01:49 2012] [error] [client x]   File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 39, in load_middleware
 [Sun Aug 26 17:01:49 2012] [error] [client x]     for middleware_path in settings.MIDDLEWARE_CLASSES:
 [Sun Aug 26 17:01:49 2012] [error] [client x]   File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 184, in inner
 [Sun Aug 26 17:01:49 2012] [error] [client x]     self._setup()
 [Sun Aug 26 17:01:49 2012] [error] [client x]   File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 42, in _setup
 [Sun Aug 26 17:01:49 2012] [error] [client x]     self._wrapped = Settings(settings_module)
 [Sun Aug 26 17:01:49 2012] [error] [client x]   File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 95, in __init__
 [Sun Aug 26 17:01:49 2012] [error] [client x]     raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
 [Sun Aug 26 17:01:49 2012] [error] [client x] ImportError: Could not import settings 'PROJECT_B.settings' (Is it on sys.path?): No module named PROJECT_B.settings

如您所见A,错误日志抱怨缺少B' 。settings.py求大神指点,不知道怎么回事。我不明白为什么一个应用程序会寻找另一个应用程序的 settings.py 文件来导入?

这两个应用程序都按预期工作和执行,但它们确实会在服务于 500 的随机请求时中断(如果我再次刷新,它们会被清除)。

谢谢!

4

1 回答 1

15

Django 破坏了生成的 wsgi.py 以在不同的子解释器中在同一进程中运行多个 Django 实例。要么使用 mod_wsgi 守护进程模式并将每个进程委托给一个单独的守护进程组,要么更好,要么更改:

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "PROJECT_A.settings")

到:

os.environ["DJANGO_SETTINGS_MODULE"] = "PROJECT_A.settings"

对于其他 wsgi.py 文件也是如此。

于 2012-08-26T22:45:36.067 回答