2014-09-28 105 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