2

我正在尝试运行两个配置在同一 wsgi 上的应用程序VirtualHost。其中一个应用程序是此处myapp指定的标准 hello-world 代码。它加载得很好。另一个应用程序是一个 Django 站点。它失败了。uiappImportError

我在wsgi 文档中读到 的值python-path附加到sys.path,所以这就是我在我WSGIDaemonProcess的 for中指定的uiapp

我不知道问题出在 Python 代码还是 Apache 配置上。

这是我的虚拟主机配置:

    [ . . . ]

    # processGroups
    WSGIProcessGroup uiapp
    WSGIProcessGroup myapp

    # configurations for django sites
    WSGIScriptAlias /uiapp "/some/path/ui_dir/site_prod/wsgi.py"
    WSGIScriptAlias /myapp "/some/other/path/myapp.wsgi"

    # daemons
    WSGIDaemonProcess uiapp processes=2 threads=25 display-name=site_prod_wsgi python-path=/some/path/ui_dir
    WSGIDaemonProcess myapp processes=2 threads=25 display-name=helloworld_wsgi

    # doc root for /uiapp
    <Directory "/some/path/ui_dir/site_prod">
        Order allow,deny
        Allow from all
    </Directory>

    # doc root for /myapp
    <Directory "/some/other/path">
        Order allow,deny
        Allow from all
    </Directory>

    [ . . . ]

我试图将 for 更改为python-path,但即使这样也失败并出现相同的错误。uiapp/some/path/ui_dir/site_prod

错误日志是:

 mod_wsgi (pid=32652): Exception occurred processing WSGI script '/some/path/ui_dir/site_prod/wsgi.py'.
 Traceback (most recent call last):
   File "/home/usr/local/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 219, in __call__
     self.load_middleware()
   File "/home/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 39, in load_middleware
     for middleware_path in settings.MIDDLEWARE_CLASSES:
   File "/home/usr/local/lib/python2.7/site-packages/django/utils/functional.py", line 184, in inner
     self._setup()
   File "/home/usr/local/lib/python2.7/site-packages/django/conf/__init__.py", line 42, in _setup
     self._wrapped = Settings(settings_module)
   File "/home/usr/local/lib/python2.7/site-packages/django/conf/__init__.py", line 95, in __init__
     raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
 ImportError: Could not import settings 'site_prod.settings' (Is it on sys.path?): No module named site_prod.settings

这是来源/some/path/ui_dir/site_prod/wsgi.py

import os

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

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

# Apply WSGI middleware here.
# from helloworld.wsgi import HelloWorldApplication
# application = HelloWorldApplication(application)

请帮我弄清楚我做错了什么。

4

4 回答 4

5

你不能像这样连续有两个 WSGIProcessGroup 指令。只有最后一个会被使用。因此,您实际上是将两个应用程序发送到同一个守护进程组而不是不同的。完成此操作并将它们置于同一进程中后,您将遇到 setdefault() 问题,如下所述:

将 WSGIProcessGroup 指令移动到与相应 WSGI 脚本文件位置相关的 Directory 块内。

于 2012-11-23T21:23:35.497 回答
1

如果您查看 django docs 版本 1.3 而不是 1.4 的先前版本,如何将 Django 与 Apache 和 mod_wsgi 一起使用部分可以帮助解决您的问题。我在配置虚拟主机时遇到了同样的问题。因此,不要python-path=/some/path/ui_dir在您的虚拟主机配置中指定。您可以将以/some/path/ui_dir/site_prod/wsgi.py下行添加到该文件中。将这些行放在文件顶部的行之前,在导入和运行 django 的内容之前。

代码

import sys
path = '/some/path/ui_dir'
if path not in sys.path:
    sys.path.append(path)
于 2012-11-23T16:10:25.867 回答
0

...并确保您已在 wsgi.py 中设置 DJANGO_SETTINGS_MODULE

os.environ['DJANGO_SETTINGS_MODULE'] = 'settings.could.be.somewhere.else'
于 2012-11-23T17:52:25.500 回答
0

试试这个:

虚拟主机:

<Virtualhost project.dev>
    DocumentRoot "/full/path/to/project/root/"

    WSGIScriptAlias / /full/path/to/wsgi/file/wsgi.py
</Virtualhost>

wsgi.py:

sys.path = ['path/to/project/root'] + sys.path
于 2012-11-23T18:35:31.917 回答