【问题标题】:DJANGO-REST, MOD_WSGI and APACHE integration problemsDJANGO-REST、MOD_WSGI 和 APACHE 集成问题
【发布时间】:2018-09-15 08:35:28
【问题描述】:

我一直在尝试使用 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 脚本上。由于这两行代码一直在崩溃,所以我没有任何进展,我记得调用我创建的任何其他 Web 服务,瞧!有用。 (不知道为什么我以前没有尝试过……我猜是固执)

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

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


希望你能给我一些帮助

谢谢

【问题讨论】:

  • 请用您的设置文件更新问题。
  • 我已经编辑了添加 settings.py 文件配置请求的问题。
  • 从错误看来,当您应该在某些文件中使用 gettext_lazy 时,您正在使用 gettext;从您的设置中不清楚。但是,请检查您的其他应用程序。如果您从INSTALLED_APPS 中注释掉rest_framework,您还会收到错误消息吗?
  • 如果我从 INSTALLED_APPS 中评论 rest_framework,WebService 的全部目的将不复存在。我的意思是……这是服务的核心。没有它,我没有什么可部署的。
  • 关键是要找出问题所在。您的设置中还有另外两个应用程序

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


【解决方案1】:

我已从以下位置更新了我的 wsgi.py 文件:

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

收件人:

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

【讨论】:

  • 是的......我也已经这样做了,但它不起作用。它似乎与我在 C++ 端启动的线程有关。奇怪的是,当我使用 uwsgi+nginx 时,它可以完美运行。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-02-13
  • 2011-05-24
  • 2014-11-29
  • 1970-01-01
  • 2018-10-24
  • 1970-01-01
  • 2012-03-17
相关资源
最近更新 更多