我目前正在使用两个Django应用程序(比如A
& B
)通过Apache托管在同一个域上(但在不同的端口上提供服务)。我相信我的设置是正确的,但我在两个网站上随机获得500。 A
上的500(例如)(主要是在B
上提供请求后发生(反之亦然)。
在检查错误日志(比如说A
)时,我看到A
的wsgi模块正在尝试访问B
的{{1}}文件(这显然不会发生在那里,因为项目路径是不同的)[并且这确实发生了另一种方式,B的wsgi引发了一个例外,抱怨缺少A的settings.py文件]。我不确定他们为什么要查找其他设置文件,所有视图的导入(对于settings.py)都是特定于各个项目的。
这是我的设置:
settings.py
正在端口A
上投放,8080
正在端口B
上投放。
虚拟主机:
80
ports.conf:
<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>
wsgi.py在'A'上:
NameVirtualHost *:8080
Listen 8080
Listen 80
<IfModule mod_ssl.c>
Listen 443
</IfModule>
<IfModule mod_gnutls.c>
Listen 443
</IfModule>
在B上完全相同,改变了第1行和第2行。
我从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()
得到的错误例如来自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
的{{1}}丢失了。请说清楚,我不知道出了什么问题。我不明白为什么一个应用程序会寻找另一个要导入的settings.py文件?
两个应用程序都按预期工作和执行,但是它们会在服务500的随机请求中中断(如果我再次刷新则会被清除)。
谢谢!
答案 0 :(得分: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文件类似。