简体   繁体   English

Django站点中的ImportError,在守护程序模式下使用mod_wsgi

[英]ImportError in Django site with mod_wsgi in Daemon mode

I am experimenting on running two wsgi applications configured on the same VirtualHost . 我正在尝试运行在同一VirtualHost上配置的两个wsgi应用程序。 One of the apps myapp is the standard hello-world code specified here . myapp应用程序之一是此处指定的标准hello-world代码。 It is loading absolutely fine. 它加载完全正常。 The other app, uiapp is a Django site. 另一个应用程序uiapp是Django网站。 It fails with an ImportError . 它失败,并显示ImportError

I read in wsgi docs that value of python-path is appended to the sys.path , so that's what I have specified in my WSGIDaemonProcess for uiapp . 我在wsgi docs中阅读到, python-path值附加到sys.path ,这就是我在WSGIDaemonProcessuiapp指定的uiapp

I can't figure out if the problem is with the Python code, or the Apache configuration. 我不知道问题出在Python代码还是Apache配置。

This is my virtualhost configuration: 这是我的虚拟主机配置:

    [ . . . ]

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

    [ . . . ]

I have tried to change the python-path for uiapp to /some/path/ui_dir/site_prod , but even that is failing with the same error. 我试图将uiapppython-path uiapp/some/path/ui_dir/site_prod ,但是即使失败,也会出现相同的错误。

The error-log is: 错误日志是:

 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

This is the source for /some/path/ui_dir/site_prod/wsgi.py 这是/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)

Kindly help me figure out what I am doing wrong. 请帮助我弄清楚我在做什么错。

You can't have two WSGIProcessGroup directives in a row like that. 您不能像这样连续两个WSGIProcessGroup指令。 Only the last will be used. 仅使用最后一个。 Thus you are actually sending both applications to the same daemon process group instead of different ones. 因此,您实际上是将两个应用程序发送到同一守护进程组,而不是不同的应用程序。 Having done that and having them in the same process you are hitting the setdefault() issue described in: 完成此操作并使它们处于相同的过程中,您遇到的setdefault()问题描述如下:

Move the WSGIProcessGroup directives inside of the Directory blocks pertaining to the respective WSGI script file locations. 将WSGIProcessGroup指令移至与各自WSGI脚本文件位置有关的Directory块内。

if you check out the previous version of the django docs version 1.3 instead of 1.4 the section How to use Django with Apache and mod_wsgi can be help solve your problem. 如果您查看django docs版本1.3而不是1.4的先前版本,则如何将Django与Apache和mod_wsgi结合使用可以帮助解决您的问题。 I faced the same issue you are facing when I configured the virtualhosts. 配置虚拟主机时,我遇到了与您同样的问题。 So instead of specifying python-path=/some/path/ui_dir in your virtualhost configuration. 因此,不要在您的virtualhost配置中指定python-path=/some/path/ui_dir You can do it in /some/path/ui_dir/site_prod/wsgi.py add the lines below to that file. 您可以在/some/path/ui_dir/site_prod/wsgi.py执行以下操作,将以/some/path/ui_dir/site_prod/wsgi.py添加到该文件中。 Place these lines before the lines at the top of the file before the stuff importing and running django. 在导入和运行django之前,将这些行放在文件顶部的行之前。

code

import sys
path = '/some/path/ui_dir'
if path not in sys.path:
    sys.path.append(path)

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

os.environ['DJANGO_SETTINGS_MODULE'] = 'settings.could.be.somewhere.else'

Try this: 尝试这个:

vhost: 虚拟主机:

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

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

wsgi.py: wsgi.py:

sys.path = ['path/to/project/root'] + sys.path

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM