2014-09-28 102 views
0

我一直在尝试使用MOD_WSGI将DJANGO-REST WS集成到Apache中,但是在尝试应用所有调试技术和不同的wsgi脚本一段时间后,帮帮我。DJANGO-REST,MOD_WSGI和APACHE集成问题

Server版本:阿帕奇/ 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 [email protected] 
    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([email protected]' 

# 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 -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”只是一种绝望的措施,最终导致无所事事。

我应该提到的一点是,尽管它对解决方案没有任何影响,但核心系统实际上是一个我用SWIG包装的C++系统。

不用说,它正在使用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服务和VOILA!有用。 (不知道为什么我以前没有尝试过......固执我猜)

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

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


我希望你能为我提供一些帮助

谢谢

+0

请用您的设置文件更新问题。 – 2014-09-28 12:21:01

+0

我编辑了添加settings.py文件配置请求的问题。 – cvicente 2014-09-28 12:31:48

+0

从错误看来,你应该在某些文件中使用'gettext_lazy'时使用'gettext';从你的设置不明确。但是,检查您的其他应用程序。如果你从'INSTALLED_APPS'注释掉'rest_framework',你还会得到错误吗? – 2014-09-28 13:03:51

回答

2

我从django.core.handlers.wsgi进口WSGIHandler 应用程序更新从

我wsgi.py文件= WSGIHandler()

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

+0

是啊......我已经做到了,但它不起作用。它似乎与我在C++端启动的线程有关。 奇怪的部分是,当我使用uwsgi + nginx时,它的工作完美无瑕。 – cvicente 2014-12-23 11:10:13

+0

这为我工作。 – Jinesh 2015-04-09 06:08:46