【问题标题】:Deploy django App on nginx with uwsgi or gunicorn not working在 nginx 上部署 django App,但 uwsgi 或 gunicorn 不工作
【发布时间】:2015-06-22 14:11:25
【问题描述】:

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

Django 应用程序在 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

【问题讨论】:

  • 能否提供nginx和gunicorn/uwsgi的配置?
  • 这里是 gunicorn conf [1]: (dpaste.de/1exA) 这里是 uwsgi conf [2]: (dpaste.de/3ztN) 和 nginx conf [3]: (dpaste.de/RkoC)
  • @dizballanze 请在上面的链接中找到所有配置文件

标签: python django nginx uwsgi gunicorn


【解决方案1】:

我认为this project 可以帮到你很多。它部署到 EC2,但您可以阅读源代码来帮助您进行部署。它很简单,可以满足您的所有要求。

【讨论】:

    【解决方案2】:

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

    检查您在导入时没有进行非惰性 gettext 调用

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

    我也支持@dizballanze,不要以 root 身份运行 uWSGI。

    【讨论】:

      【解决方案3】:

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

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

      尝试安装pcre:

      sudo apt-get install libpcre3 libpcre3-dev
      

      当重新安装uwsgi时:

      pip uninstall uwsgi
      pip install uwsgi
      

      附: 你真的不应该以 root 用户身份运行应用程序。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-02-05
        • 2017-08-07
        • 1970-01-01
        • 2012-05-27
        • 1970-01-01
        • 2016-08-12
        • 2015-06-02
        • 2020-04-06
        相关资源
        最近更新 更多