我目前使用两个 Django 应用程序(假设为 A
和 B
)通过 Apache 在同一域名下运行(但是在不同端口上提供服务)。我认为我的设置是正确的,但是我在这两个站点中随机收到 500 错误。例如,A
上的 500 错误通常发生在已经在 B
上处理请求之后(反之亦然)。
在检查错误日志时(例如,A
的日志),我发现 A
的 WSGI 模块正在尝试访问 B
的 settings.py
文件(显然该文件不存在,因为项目路径不同)[B 的 WSGI 报错,抱怨缺少 A 的 settings.py 文件也会发生类似的错误]。我不确定为什么它们要寻找其他的设置文件,因为所有视图中的导入(针对 settings.py)都是特定于各自的项目的。
这是我的设置:
A
在端口 8080
上提供服务,B
在端口 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>
ports.conf:
NameVirtualHost *:8080
Listen 8080
Listen 80
<IfModule mod_ssl.c>
Listen 443
</IfModule>
<IfModule mod_gnutls.c>
Listen 443
</IfModule>
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()
在B上也完全相同,只是对第一行和第二行进行了更改。
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 时会出现错误(如果我再次刷新则会解决)。
谢谢!