使用uwsgi或gunicorn在nginx上部署django App无法正常工作

时间:2015-04-16 09:09:42

标签: python django nginx uwsgi gunicorn

我正在尝试使用nginx服务器在ubuntu上部署django应用程序。我尝试使用gunicorn和uwsgi这两种方法,但仍然没有工作。我经历了很多教程。主要问题可能是我的django应用程序不在虚拟环境中。它直接在我的服务器上。请帮我搞清楚。

Django app在8000端口上独立工作。和ngnix也在运行。 Gunicorn和uwsgi也正确安装。 uwsgi在运行时出现此错误:

uwsgi --ini uwsgi.ini
[uWSGI] getting INI configuration from uwsgi.ini
*** Starting uWSGI 2.0.10 (64bit) on [Thu Apr 16 04:49:44 2015] ***
compiled with version: 4.8.2 on 14 April 2015 02:24:25
os: Linux-3.13.0-43-generic #72-Ubuntu SMP Mon Dec 8 19:35:06 UTC 2014
nodename: movieran
machine: x86_64
clock source: unix
detected number of CPU cores: 1
current working directory: /root/mymovie
detected binary path: /usr/local/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** 
chdir() to /root/mymovie
*** WARNING: you are running uWSGI without its master process manager ***
your processes number limit is 7782
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uWSGI http bound on :8000 fd 4
spawned uWSGI http 1 (pid: 29087)
uwsgi socket 0 bound to TCP address 127.0.0.1:52804 (port auto-assigned)     fd 3
Python version: 2.7.6 (default, Mar 22 2014, 23:03:41)  [GCC 4.8.2]
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0xa69d20
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 72768 bytes (71 KB) for 1 cores
*** Operational MODE: single process ***
added /var/www to pythonpath.
WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0xa69d20 pid: 29086 (default app)
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI worker 1 (and the only) (pid: 29086, cores: 1)
No handlers could be found for logger "django.request"
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/wsgi.py", line 189, in __call__
response = self.get_response(request)
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 218, in get_response
response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 261, in handle_uncaught_exception
return debug.technical_500_response(request, *exc_info)
File "/usr/local/lib/python2.7/dist-packages/django/views/debug.py", line 97, in technical_500_response
html = reporter.get_traceback_html()
File "/usr/local/lib/python2.7/dist-packages/django/views/debug.py", line 384, in get_traceback_html
return t.render(c)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 209, in render
return self._render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 201, in _render
return self.nodelist.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 903, in render
bit = self.render_node(node, context)
File "/usr/local/lib/python2.7/dist-packages/django/template/debug.py", line 79, in render_node
return node.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/debug.py", line 89, in render
output = self.filter_expression.resolve(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 674, in resolve
new_obj = func(obj, *arg_vals)
File "/usr/local/lib/python2.7/dist-packages/django/template/defaultfilters.py", line 774, in date
return format(value, arg)
File "/usr/local/lib/python2.7/dist-packages/django/utils/dateformat.py", line 345, in format
return df.format(format_string)
File "/usr/local/lib/python2.7/dist-packages/django/utils/dateformat.py", line 37, in format
pieces.append(force_text(getattr(self, piece)()))
File "/usr/local/lib/python2.7/dist-packages/django/utils/dateformat.py", line 270, in r
return self.format('D, j M Y H:i:s O')
File "/usr/local/lib/python2.7/dist-packages/django/utils/dateformat.py", line 37, in format
pieces.append(force_text(getattr(self, piece)()))
File "/usr/local/lib/python2.7/dist-packages/django/utils/encoding.py", line 92, in force_text
s = six.text_type(s)
File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 141, in __text_cast
return func(*self.__args, **self.__kw)
File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/__init__.py", line 84, in ugettext
return _trans.ugettext(message)
File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/trans_real.py", line 323, in ugettext
return do_translate(message, 'ugettext')
File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/trans_real.py", line 300, in do_translate
_default = _default or translation(settings.LANGUAGE_CODE)
File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/trans_real.py", line 206, in translation
_translations[language] = DjangoTranslation(language)
File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/trans_real.py", line 116, in __init__
self._add_installed_apps_translations()
File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/trans_real.py", line 164, in _add_installed_apps_translations
"The translation infrastructure cannot be initialized before the "django.core.exceptions.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.
[pid: 29086|app: 0|req: 1/1] 24.90.108.186 () {38 vars in 595 bytes} [Thu Apr 16 03:50:31 2015] GET / => generated 0 bytes in 163 msecs (HTTP/1.1 500) 0 headers in 0 bytes (0 switches on core 0)

Nginx发出404错误。请帮我部署。

我也从[1]中关注了这个问题:Django app deployment on nGINX

3 个答案:

答案 0 :(得分:1)

错误日志末尾可能会非常清楚地说明错误

  

检查您是否在导入时不进行非延迟的gettext调用

如果您的代码中有翻译调用,请确保它们是lazy calls,延迟调用在需要之前不会被评估,并且通常位于前端或将值写入某些输出或日志时。在极少数情况下,您将不得不使用非延迟调用,但要确保在所有应用程序准备好并且Django准备好之前它们没有被执行,尤其是在settings.py文件中。

我也是第二个@dizballanze,不要把uWSGI作为ROOT。

答案 1 :(得分:0)

我认为问题在uwsgi输出的以下行中描述:

!!! no internal routing support, rebuild with pcre support !!!

尝试安装pcre:

sudo apt-get install libpcre3 libpcre3-dev

重新安装uwsgi:

pip uninstall uwsgi
pip install uwsgi

P.S。 你真的不应该以root用户身份运行应用程序。

答案 2 :(得分:-1)

我认为this project可以帮助你。它部署到EC2但您可以阅读源代码以帮助您进行部署。它很容易,并且可以满足您的一切要求。