DJANGO-REST,MOD_WSGI和APACHE集成问题

时间:2014-09-28 10:02:15

标签: apache mod-wsgi django-rest-framework

我一直在尝试使用MOD_WSGI将DJANGO-REST WS集成到apache中,但经过长时间尝试应用所有调试技术和不同的wsgi脚本后,我将不胜感激。

服务器版本:Apache / 2.2.22(Ubuntu) Python 2.7.3(默认,2014年2月27日,19:58:35) MOD_WSGI版本:3.3

目前我的WSGI文件如下:

"""
WSGI config for zighome_rest project.

It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/1.6/howto/deployment/wsgi/
"""

import os, sys
sys.path.append('/usr/local/ZigHomeWS')
sys.path.append('/usr/local/ZigHomeWS/zighome_rest')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "zighome_rest.settings")

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

我的虚拟主机配置如下:

<VirtualHost *:80>
    ServerAdmin my.email@gmail.com
    ServerName my.server.name

    DocumentRoot /usr/local/DummyApp

        WSGIDaemonProcess ZigHomeWS python-path=/usr/local/ZigHomeWS/zighome_rest:/usr/lib/python2.7:/usr/local/lib/python2.7/site-packages/django
        WSGIProcessGroup ZigHomeWS
        WSGIApplicationGroup %{GLOBAL}

        WSGIScriptAlias /ZigHomeWS /usr/local/ZigHomeWS/zighome_rest/zighome_rest/WSGI/django.wsgi
        WSGIPassAuthorization On

        <Directory /usr/local/ZigHomeWS/zighome_rest>
            <Files django.wsgi>
                Order deny,allow
                Allow from all
            </Files>
        </Directory>

        <Directory /usr/local/DummyApp>
            Order deny,allow
            Allow from all
        </Directory>

        ErrorLog /var/log/apache2/error_ZigHomeWS.log

        # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel info

</VirtualHost>

当前的 settings.py 文件具有以下配置:

"""
Django settings for zighome_rest project.

For more information on this file, see
https://docs.djangoproject.com/en/1.6/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.6/ref/settings/
"""

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.6/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '6h34&@m=1t4!(axkhz1+%6%u8^o(7^ao500pl#*+=$sk(a846@'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
    'configuration',
    'execution',
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

ROOT_URLCONF = 'zighome_rest.urls'

WSGI_APPLICATION = 'zighome_rest.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.6/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': '/usr/local/ZigHomeWS/zighome_rest/zighome.db',
        'USER': '',
        'PASSWORD': '',
        'HOST': '',
        'PORT': '',
    }
}

# Internationalization
# https://docs.djangoproject.com/en/1.6/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.6/howto/static-files/

STATIC_URL = '/static/'

一旦我用curl调用其中一个服务,就像这样:

curl -X POST http://my.server.name/ZigHomeWS/execution/start/ -u user:passwd

我收到以下错误:

[Sat Sep 27 19:15:28 2014] [info] mod_wsgi (pid=13351): Adding '/usr/local/ZigHomeWS/zighome_rest' to path.
[Sat Sep 27 19:15:28 2014] [info] mod_wsgi (pid=13351): Adding '/usr/lib/python2.7' to path.
[Sat Sep 27 19:15:28 2014] [info] mod_wsgi (pid=13351): Adding '/usr/local/lib/python2.7/site-packages/django' to path.
[Sat Sep 27 19:15:32 2014] [info] [client 46.189.221.224] mod_wsgi (pid=13351, process='ZigHomeWS', application=''): Loading WSGI script '/usr/local/ZigHomeWS/zighome_rest/zighome_rest/WSGI/django.wsgi'.
[Sat Sep 27 18:15:33 2014] [error] No handlers could be found for logger "django.request"
[Sat Sep 27 18:15:33 2014] [error] [client 46.189.221.224] mod_wsgi (pid=13351): Exception occurred processing WSGI script '/usr/local/ZigHomeWS/zighome_rest/zighome_rest/WSGI/django.wsgi'.
[Sat Sep 27 18:15:33 2014] [error] [client 46.189.221.224] Traceback (most recent call last):
[Sat Sep 27 18:15:33 2014] [error] [client 46.189.221.224]   File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/wsgi.py", line 187, in __call__
[Sat Sep 27 18:15:33 2014] [error] [client 46.189.221.224]     response = self.get_response(request)
[Sat Sep 27 18:15:33 2014] [error] [client 46.189.221.224]   File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 199, in get_response
[Sat Sep 27 18:15:33 2014] [error] [client 46.189.221.224]     response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
[Sat Sep 27 18:15:33 2014] [error] [client 46.189.221.224]   File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 236, in handle_uncaught_exception
[Sat Sep 27 18:15:33 2014] [error] [client 46.189.221.224]     return debug.technical_500_response(request, *exc_info)
[Sat Sep 27 18:15:33 2014] [error] [client 46.189.221.224]   File "/usr/local/lib/python2.7/dist-packages/django/views/debug.py", line 91, in technical_500_response
[Sat Sep 27 18:15:33 2014] [error] [client 46.189.221.224]     html = reporter.get_traceback_html()
[Sat Sep 27 18:15:33 2014] [error] [client 46.189.221.224]   File "/usr/local/lib/python2.7/dist-packages/django/views/debug.py", line 350, in get_traceback_html
[Sat Sep 27 18:15:33 2014] [error] [client 46.189.221.224]     return t.render(c)
[Sat Sep 27 18:15:33 2014] [error] [client 46.189.221.224]   File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 148, in render
[Sat Sep 27 18:15:33 2014] [error] [client 46.189.221.224]     return self._render(context)
[Sat Sep 27 18:15:33 2014] [error] [client 46.189.221.224]   File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 142, in _render
[Sat Sep 27 18:15:33 2014] [error] [client 46.189.221.224]     return self.nodelist.render(context)
[Sat Sep 27 18:15:33 2014] [error] [client 46.189.221.224]   File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 844, in render
[Sat Sep 27 18:15:33 2014] [error] [client 46.189.221.224]     bit = self.render_node(node, context)
[Sat Sep 27 18:15:33 2014] [error] [client 46.189.221.224]   File "/usr/local/lib/python2.7/dist-packages/django/template/debug.py", line 80, in render_node
[Sat Sep 27 18:15:33 2014] [error] [client 46.189.221.224]     return node.render(context)
[Sat Sep 27 18:15:33 2014] [error] [client 46.189.221.224]   File "/usr/local/lib/python2.7/dist-packages/django/template/debug.py", line 90, in render
[Sat Sep 27 18:15:33 2014] [error] [client 46.189.221.224]     output = self.filter_expression.resolve(context)
[Sat Sep 27 18:15:33 2014] [error] [client 46.189.221.224]   File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 624, in resolve
[Sat Sep 27 18:15:33 2014] [error] [client 46.189.221.224]     new_obj = func(obj, *arg_vals)
[Sat Sep 27 18:15:33 2014] [error] [client 46.189.221.224]   File "/usr/local/lib/python2.7/dist-packages/django/template/defaultfilters.py", line 769, in date
[Sat Sep 27 18:15:33 2014] [error] [client 46.189.221.224]     return format(value, arg)
[Sat Sep 27 18:15:33 2014] [error] [client 46.189.221.224]   File "/usr/local/lib/python2.7/dist-packages/django/utils/dateformat.py", line 343, in format
[Sat Sep 27 18:15:33 2014] [error] [client 46.189.221.224]     return df.format(format_string)
[Sat Sep 27 18:15:33 2014] [error] [client 46.189.221.224]   File "/usr/local/lib/python2.7/dist-packages/django/utils/dateformat.py", line 35, in format
[Sat Sep 27 18:15:33 2014] [error] [client 46.189.221.224]     pieces.append(force_text(getattr(self, piece)()))
[Sat Sep 27 18:15:33 2014] [error] [client 46.189.221.224]   File "/usr/local/lib/python2.7/dist-packages/django/utils/dateformat.py", line 268, in r
[Sat Sep 27 18:15:33 2014] [error] [client 46.189.221.224]     return self.format('D, j M Y H:i:s O')
[Sat Sep 27 18:15:33 2014] [error] [client 46.189.221.224]   File "/usr/local/lib/python2.7/dist-packages/django/utils/dateformat.py", line 35, in format
[Sat Sep 27 18:15:33 2014] [error] [client 46.189.221.224]     pieces.append(force_text(getattr(self, piece)()))
[Sat Sep 27 18:15:33 2014] [error] [client 46.189.221.224]   File "/usr/local/lib/python2.7/dist-packages/django/utils/encoding.py", line 85, in force_text
[Sat Sep 27 18:15:33 2014] [error] [client 46.189.221.224]     s = six.text_type(s)
[Sat Sep 27 18:15:33 2014] [error] [client 46.189.221.224]   File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 144, in __text_cast
[Sat Sep 27 18:15:33 2014] [error] [client 46.189.221.224]     return func(*self.__args, **self.__kw)
[Sat Sep 27 18:15:33 2014] [error] [client 46.189.221.224]   File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/__init__.py", line 83, in ugettext
[Sat Sep 27 18:15:33 2014] [error] [client 46.189.221.224]     return _trans.ugettext(message)
[Sat Sep 27 18:15:33 2014] [error] [client 46.189.221.224]   File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/trans_real.py", line 325, in ugettext
[Sat Sep 27 18:15:33 2014] [error] [client 46.189.221.224]     return do_translate(message, 'ugettext')
[Sat Sep 27 18:15:33 2014] [error] [client 46.189.221.224]   File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/trans_real.py", line 306, in do_translate
[Sat Sep 27 18:15:33 2014] [error] [client 46.189.221.224]     _default = translation(settings.LANGUAGE_CODE)
[Sat Sep 27 18:15:33 2014] [error] [client 46.189.221.224]   File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/trans_real.py", line 209, in translation
[Sat Sep 27 18:15:33 2014] [error] [client 46.189.221.224]     default_translation = _fetch(settings.LANGUAGE_CODE)
[Sat Sep 27 18:15:33 2014] [error] [client 46.189.221.224]   File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/trans_real.py", line 189, in _fetch
[Sat Sep 27 18:15:33 2014] [error] [client 46.189.221.224]     "The translation infrastructure cannot be initialized before the "
[Sat Sep 27 18:15:33 2014] [error] [client 46.189.221.224] AppRegistryNotReady: The translation infrastructure cannot be initialized before the apps registry is ready. Check that you don't make non-lazy gettext calls at import time.

我必须承认,添加“/usr/local/lib/python2.7/site-packages/django”只是一种绝望的衡量标准,最终导致什么都没有。

我应该提到的一件事,虽然它对解决方案没有任何影响,但核心系统实际上是一个C ++系统,我用SWIG包裹起来。

毋庸置疑,它正在开发Django-REST开发环境。

最后但并非最不重要的是,如果在WSGI脚本文件中我更改了这两行:

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

通过这些:

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

它会崩溃而不是在日志文件中提供堆栈跟踪:

[Sat Sep 27 19:44:10 2014] [error] [client X.X.X.X] Premature end of script headers: django.wsgi

我最终应用了推荐的调试技术,但不幸的是它没有用,因为 gdb 的堆栈跟踪只是给了我:

(gdb) cont
Continuing.
[New Thread 0x7fc6d4de9700 (LWP 9312)]
[New Thread 0x7fc6d45e8700 (LWP 9313)]
[Thread 0x7fc6dde3a700 (LWP 9242) exited]
[Thread 0x7fc6d45e8700 (LWP 9313) exited]
[Thread 0x7fc6d4de9700 (LWP 9312) exited]
[Thread 0x7fc6dd639700 (LWP 9245) exited]
[Thread 0x7fc6e364a740 (LWP 9216) exited]
[Inferior 1 (process 9216) exited with code 01]

调查:

所以...我刚刚意识到了什么。

根据我从网上的几个网站上读到的内容,我应该真正使用这个电话:

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

在WSGI脚本上。由于我无法快速使用这两行代码,这些代码一直在崩溃,我记得打电话给我创建的任何其他网络服务和VOILA !!有用。 (不知道为什么我之前没有尝试过这个......我猜是顽固的)

现在,两个Web服务之间的主要区别之一是,只解析一些XML文件并将数据添加到sqlite3数据库而另一个(崩溃的数据库)启动整个系统,意味着创建多个线程等等。

因此,我现在正在寻找与modwsgi可以找到的任何线程相关的文档问题。


我希望你能给我一些帮助

谢谢

1 个答案:

答案 0 :(得分:3)

我已经更新了我的wsgi.py文件:

from django.core.handlers.wsgi import WSGIHandler
application = WSGIHandler()

要:

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