2

I have recently set up a web server, which currently hosts a couple of static page websites, and two django projects.

the two django projects are 'abc' and 'xyz' and are in separate directories respectively, in the home folder. Each have their own wsgi script which points to their respective settings.py file.

Recently though, I have been noticing a few 500 errors on 'xyz'. Usually a refresh will correct the problem, but this isn't acceptable, so I checked the apache error.log, and noticed that sometimes when I hit 'xyz' there is an exception raised about cannot find abc.settings in the xyz project. Somehow these two projects are crossing over and interfering with each other. I have not worked on abc enough yet to know if the problem is the same the other way around. Below is my exception.

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

Any help/advice would be greatly appreciated. Cheers

4

4 に答える 4

5

あなたが直面している問題は、mod_wsgiが各Djangoアプリに独自のPythonインタープリターを提供する一方で、Djangoが設定モジュールの名前を格納するのと同じOS環境を共有していることです。私が見つけた回避策は、WSGIアプリケーションオブジェクトを作成する前に、Djangoが設定モジュールを検索する環境変数の名前を変更することでした。

少し変更したwsgi.pyは次のようになります。

import os

# change the env variable where django looks for the settings module
import django.conf
django.conf.ENVIRONMENT_VARIABLE = "DJANGO_SECOND_SETTINGS_MODULE"

os.environ.setdefault("DJANGO_SECOND_SETTINGS_MODULE", "second.settings")

# This application object is used by any WSGI server configured to use this
# file. This includes Django's development server, if the WSGI_APPLICATION
# setting points here.
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
于 2012-08-05T14:09:01.327 に答える
3

The default configuration for mod_wsgi has the two Django sites running in separate sub interpreters of the same process. Unfortunately Django changed their generated wsgi.py in 1.4 and the new file breaks with this default behaviour of mod_wsgi. To fix the issue if using Django 1.4, go into the wsgi.py file and change:

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

to:

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

That is, don't use os.environ.setdefault() because it will not do anything when variable has already been set by other Django site in the other sub interpreter due to the way environment variables leak between sub interpreters.

Better still, use mod_wsgi daemon mode and create distinct daemon process groups for each site and delegate them to run in different sets of processes. This can solve a similar issue with older Django versions where a not quite correct Apache configuration for VirtualHosts has been used.

于 2012-08-06T00:15:11.577 に答える
2

現在、2つのプロジェクトに別々の仮想環境を使用しているようには見えません。そうでない場合は、使用して、その後も問題が解決するかどうかを確認することを強くお勧めします。同じApacheインスタンスを引き続き使用できますが、Djangoの2つの別々のインスタンスを実行します(プロジェクトの他のすべての要件は、異なる場合と異なる場合があります)。これは通常、Djangoプロジェクトに推奨されるアプローチです。

仮想環境について知らない場合は、virtualenvとDjangoの使用に関するクイックスタートチュートリアルがあります。また、DougHellmanによる非常に優れたVirtualenvラッパーを使用することをお勧めします。お役に立てれば!

于 2012-07-09T10:07:29.730 に答える
0

Are you using memcached for the cache, or any other caching method where the two instances could write in the same cache file? That could explain why the two environments get mixed up. In that case, simply add a KEY_PREFIX variables in your settings' CACHES dictionary.

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
        'LOCATION': '127.0.0.1:11211',
        'KEY_PREFIX': 'scalamoosh'
    }
}
于 2012-07-18T13:07:37.217 に答える