简体   繁体   English

在uwsgi或gunicorn无法正常工作的Nginx上部署Django应用

[英]Deploy django App on nginx with uwsgi or gunicorn not working

I am trying to deploy a django application on ubuntu with nginx server. 我正在尝试使用Nginx服务器在ubuntu上部署Django应用程序。 I try to use both methods by gunicorn and uwsgi but still not workong. 我尝试使用Gunicorn和uwsgi的两种方法,但仍然无法使用。 I go through a lot of tutorial. 我经历了很多教程。 Main issue may be that my django app is not on virtual env. 主要问题可能是我的Django应用程序不在虚拟环境中。 It is directly on my server. 它直接在我的服务器上。 Please help me in figuring out. 请帮我弄清楚。

Django app is working stand alone on 8000 port. Django应用程序可以在8000端口上独立运行。 and ngnix is also running. 而且ngnix也正在运行。 Gunicorn and uwsgi is also installed properly. Gunicorn和uwsgi也已正确安装。 uwsgi gives this error while running: 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 is giving 404 error. Nginx出现404错误。 Please help me in deployment. 请帮我部署。

I also follow this question from [1]: Django app deployment on nGINX 我也从[1]追踪这个问题: nGINX上的Django应用程式部署

Well also the error might be very clearly stated at the end of the error log 好吧,错误可能会在错误日志的末尾清楚地说明

Check that you don't make non-lazy gettext calls at import time 检查您在导入时没有进行非惰性的gettext调用

If you have translation calls in your code make sure they are lazy calls , lazy calls are not evaluated until they're needed, and that's usually on the front end or when writing the values to some output or a log. 如果您的代码中有翻译调用,请确保它们是延迟调用 ,直到需要时才对延迟调用进行评估,并且延迟调用通常在前端或将值写入某些输出或日志时进行。 In some rare cases you will have to use non-lazy calls, but make sure they're not being executed before all the apps are ready and Django is ready, so especially in your settings.py file. 在极少数情况下,您将不得不使用非延迟调用,但要确保在所有应用程序就绪且Django就绪之前,它们没有被执行,尤其是在settings.py文件中。

Also I second @dizballanze, DO NOT RUN uWSGI AS ROOT. 另外我第二个@dizballanze,不要以根目录运行uWSGI。

I think the problem is described in following line of uwsgi output: 我认为问题在以下uwsgi输出行中描述:

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

Try to install pcre: 尝试安装pcre:

sudo apt-get install libpcre3 libpcre3-dev

and when reinstall uwsgi: 并在重新安装uwsgi时:

pip uninstall uwsgi
pip install uwsgi

PS You are really should not run the application as root user. PS您确实不应该以root用户身份运行该应用程序。

I think this project can help you alot. 我认为这个项目可以为您提供很多帮助。 it deploys to EC2 but you can read the source code to help you with your deployment. 它可以部署到EC2,但是您可以阅读源代码以帮助您进行部署。 Its easy and does everything you ask for. 它很容易,可以满足您的所有要求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM