在Apache上使用Django和mod_wsgi的多个站点。

时间:2022-10-28 23:05:55

I'm currently using two Django applications (say A & B) hosted on the same domain (but being served on different ports) through Apache. I believe my setup is correct, but I'm randomly getting 500s on both the sites. The 500 on A (say for example) occurs mostly after a request has been served on B (and vice versa).

我目前正在使用两个Django应用程序(比如A和B),它们通过Apache驻留在同一个域中(但是在不同的端口上)。我相信我的设置是正确的,但是我在两个网站上随机得到500。A上的500(例如)通常发生在B上的请求被送达之后(反之亦然)。

Upon inspecting the error log (of say A for instance), I see that A's wsgi module is trying to access B's settings.py file (which obviously doesn't happen to be there, since the project paths are different) [and this does happen the other way too, B's wsgi raises an exception complaining of missing A's settings.py file]. I'm not sure why they'd look for the other settings file, the imports (for settings.py) on all views are specific to the respective projects.

在检查错误日志(例如A)时,我看到A的wsgi模块试图访问B的设置。py文件(显然不存在,因为项目路径是不同的)[这也以另一种方式发生,B的wsgi提出了一个异常,抱怨缺少A的设置。py文件)。我不确定他们为什么要寻找其他的设置文件,所有视图上的导入(用于settings.y)都是特定于各自项目的。

Here's my setup:

这是我的设置:

A is being served on port 8080, B is being served on port 80.

A在8080端口,B在80端口。

VirtualHost:

虚拟主机:

<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>

ports.conf:

ports.conf:

NameVirtualHost *:8080
Listen 8080
Listen 80

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

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

wsgi.py on 'A':

wsgi。py A:

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()

And exactly the same on B, with changes to line #1 and #2.

B上也是一样,第一行和第二行有变化。

Error I get from the error.log say for instance from A:

我从错误中得到错误。比如从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

As you see A's error log complains that B's settings.py is missing. Please shed some light, I've no idea what's wrong. I don't see why one app would look for the other's settings.py file to import?

当你看到A的错误日志抱怨B的设置。py是失踪。请给我点明,我不知道怎么了。我不明白为什么一个应用会去寻找另一个的设置。py文件导入?

Both the applications work and execute as expected, but they do break at random requests serving the 500 (which gets cleared if I refresh again).

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

Thank you!

谢谢你!

1 个解决方案

#1


15  

Django breaks the geneated wsgi.py for running multiple Django instances in same process in different sub interpreters. Either use mod_wsgi daemon mode and delegate each to a separate daemon process group, is better anyway, or change:

Django破坏了geneated wsgi。py用于在不同的子解释器中同一个进程中运行多个Django实例。或者使用mod_wsgi守护进程模式并将它们分别委托给一个单独的守护进程进程组,无论如何都更好,或者更改:

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

to:

:

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

Similarly for other wsgi.py file.

为其他wsgi类似。py文件。

#1


15  

Django breaks the geneated wsgi.py for running multiple Django instances in same process in different sub interpreters. Either use mod_wsgi daemon mode and delegate each to a separate daemon process group, is better anyway, or change:

Django破坏了geneated wsgi。py用于在不同的子解释器中同一个进程中运行多个Django实例。或者使用mod_wsgi守护进程模式并将它们分别委托给一个单独的守护进程进程组,无论如何都更好,或者更改:

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

to:

:

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

Similarly for other wsgi.py file.

为其他wsgi类似。py文件。