如何让Gunicorn使用Python 3而不是Python 2(502 Bad Gateway)

我试图让Gunicorn使用Python3来创build我想要创build的Django应用程序。 我正在使用Digital Ocean的Django图像开始。 它带有Django,Gunicorn和Nginx的安装和configuration。 这个图像附带的默认Django项目似乎对Python 2来说工作正常。

我已经apt-get '编辑这些软件包。

  • python3
  • python3-psycopg2
  • python3-dev的
  • python3-PIP

为了避免任何问题,我也做了这个。

  • 点卸载Django
  • pip3安装django

rm -rf编辑股票项目,并用django-admin.py startproject django_project创build一个新项目。 django-admin.py使用Python 3(根据shebang)。 后来,我使用python3 manage.py startapp django_app来创build一个新的应用程序。

在这一点上,一切正常。 就像默认的应用程序。 然后,在django_app/views.py我这样做,它打破。

 from django.shortcuts import render from django.http import HttpResponse def index(request): # Python 2 and 3 - works fine # print('PRINTING') # Python 3 only - crashes print(1, 2, end=' ') return HttpResponse("Hello, world! This is my first view.") 

错误页面说我正在使用Python 2.7.6。

好的,那么我想我可以通过Python 3来安装Gunicorn,所以我这样做。

但是,我最后只是502坏门户。 当我service gunicorn status ,我得到gunicorn stop/waiting 。 我尝试了service gunicorn restart ,但它仍然说gunicorn stop/waiting

我做了一个which gunicorn ,它安装在/usr/local/bin/gunicorn 。 呃…我真的不知道还有什么我可以尝试。 任何帮助将不胜感激。 谢谢。

重新开始可能更容易。 教程在https://www.digitalocean.com/community/articles/how-to-install-and-configure-django-with-postgres-nginx-and-gunicorn

我把它运行在一个新的Ubuntu 14.04滴。 安装python3和django,然后只需按照教程。 虽然没有做postgres或virtualenv位。

如果这两个环节断了一天,这是我怎么得到它的工作。

执行这些指令后开始。

  • pip uninstall gunicorn
  • pip3 install gunicorn

安装supervisorsudo apt-get install supervisor

接下来,我需要在我的项目目录的根目录下创建gunicorn_config.py ,其中包含这个目录。

 command = '/usr/local/bin/gunicorn' pythonpath = '/home/django/django_project' bind = '127.0.0.1:9000' workers = 3 user = 'nobody' 

然后,我为supervisor创建了一个配置文件。 vim /etc/supervisor/conf.d/gunicorn.conf ,包含这些内容。

 [program:gunicorn] command=/usr/local/bin/gunicorn -c /home/django/django_project/gunicorn_config.py django_project.wsgi user=nobody autostart=true autorestart=true stderr_logfile=/var/log/gunicorn3.err.log stdout_logfile=/var/log/gunicorn3.out.log 

之后,我做了一个supervisorctl rereadsupervisorctl update ,然后开始工作。

你可以用supervisorctl status gunicorn来检查gunicorn是否在运行。 您可以使用supervisorctl restart gunicorn来重新启动。

写了下面的脚本,用DigitalOcean的14.04 Django图像切换到Python 3.4,因为我希望它是一个很好的一步设置…它将被维护在https://gist.github.com/tr00st/190ab4de62f9b23bea69

对于我来说,主要的问题是与gevent,切换到龙卷风,工人工作正常。

 #!/bin/bash # Python 3 Upgrade for Django Droplet # Will update the "Django on 14.04" Digital Ocean image. # Run as root. # Grab psycopg2 and pip apt-get install python3-pip python3-psycopg2 # Remove the Python 2.7 version of gunicorn, so we can... pip uninstall gunicorn # Install the Python 3 version of gunicorn, and a couple of dependencies. pip3 install gunicorn tornado django # Sadly, at time of writing, gevent isn't Python 3 compatible... But tornado is! # So, switch them out with a little sed magic sed 's/worker_class = '\''gevent'\''/worker_class='\''tornado'\''/' /etc/gunicorn.d/gunicorn.py -i.orig # Restart gunicorn to make the changes take effect... service gunicorn restart # And we're good! 

我的方式:

 virtualenv -p /usr/bin/python3 /home/py3env source /home/py3env/bin/activate pip3 install gunicorn /home/py3env/bin/gunicorn -w4 -b0.0.0.0:8000 [projectname].wsgi 

我在数字海洋上使用了“14.04上的Ubuntu Django”。

我意识到,当使用Python 3时,“gevent”worker类型对我来说是个问题。即使我使用python3 -m pip freeze检查安装了“gevent”,但没有奏效。 我在/etc/gunicorn.d/gunicorn.py中将其更改为“sync”

 ... worker_class = 'sync' ... 

我重新启动了gunicorn:

 sudo service gunicorn restart 

我检查了gunicorn服务正在使用service gunicorn status运行,并能够通过达到我的液滴的IP地址看到django页面的欢迎。

我希望对其他人有用。

如果你看看gunicorn可执行文件,它只是一个小的python脚本:

 $ cat gunicorn #!/usr/bin/env python # -*- coding: utf-8 -*- import re import sys from gunicorn.app.wsgiapp import run if __name__ == '__main__': sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0]) sys.exit(run()) 

重要的是from gunicorn.app.wsgiapp import run行,它告诉你模块响应运行你的应用程序的位置。 如果gunicorn是质量代码(它是),您应该能够直接从命令行导入该模块并运行您的应用程序。

“直接从命令行导入模块”是指使用-m命令行开关 。

当使用-m module-name调用时,给定的模块位于Python模块路径中并作为脚本执行。

使用你选择的python:

 $ /path/to/your/python -m gunicorn.app.wsgiapp {{ gunicorn command line args go here }} server:app 

当然,它运行!

 [2017-12-04 02:05:27 +0000] [24] [INFO] Starting gunicorn 19.7.1 [2017-12-04 02:05:27 +0000] [24] [INFO] listning at: http://127.0.0.1:5000 (24) [2017-12-04 02:05:27 +0000] [24] [INFO] Using worker: sync [2017-12-04 02:05:27 +0000] [28] [INFO] Booting worker with pid: 28 [2017-12-04 02:05:27 +0000] [29] [INFO] Booting worker with pid: 29 

这种行为是非常有用的,例如从Dockerfile这样的地方运行gunicorn。