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

425*_*esp 32 django nginx python-2.7 python-3.x gunicorn

我正在努力让Gunicorn将Python3用于我想制作的Django应用程序.我正在使用Digital Ocean的Django图像开始.它安装和配置了Django,Gunicorn和Nginx.这个图像附带的默认Django项目似乎适用于Python 2.

我已经把apt-get这些包裹了.

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

为了避免任何问题,我也做到了这一点.

  • pip uninstall django
  • pip3安装django

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

在这一点上,一切正常.就像默认的应用程序一样.然后,在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.")
Run Code Online (Sandbox Code Playgroud)

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

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

  • pip卸载gunicorn
  • pip3安装gunicorn

但后来我最终得到了502 Bad Gateway.当我这样做时service gunicorn status,我明白了gunicorn stop/waiting.我试过了service gunicorn restart,但它仍然说gunicorn stop/waiting.

我做了一个which gunicorn,它安装在/usr/local/bin/gunicorn.呃......我不确定我还能尝试什么.任何帮助将不胜感激.谢谢.

pap*_*adp 22

似乎有一个名为的软件包gunicorn3(已在ubuntu上进行了测试)

sudo apt-get install gunicorn3

然后运行以下命令应该可以运行并使用python3运行gunicorn:

gunicorn3 --log-level debug --bind 0.0.0.0:30443 server:app

  • 这是在Ubuntu 3.6上运行的唯一答案。谢谢。 (2认同)

425*_*esp 20

如果两个链接断开一天,这就是我如何使它工作.

执行这些指令后开始.

  • pip uninstall gunicorn
  • pip3 install gunicorn

安装supervisor,sudo 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'
Run Code Online (Sandbox Code Playgroud)

然后,我为其创建了一个配置文件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
Run Code Online (Sandbox Code Playgroud)

在那之后,我做了supervisorctl rereadsupervisorctl update,然后这一切开始工作.

您可以supervisorctl status gunicorn用来检查是否gunicorn正在运行.您可以使用supervisorctl restart gunicorn重启.


war*_*rdk 7

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

我用新的ubuntu 14.04液滴运行了它.安装python3和django然后只需按照教程.虽然没有做postgres或virtualenv位.


小智 7

我的方式:

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
Run Code Online (Sandbox Code Playgroud)


Ale*_*lig 6

我实现这一目标的方法是从各处卸载gunicorn:

sudo apt-get remove gunicorn
pip uninstall gunicorn
pip3 uninstall gunicorn
Run Code Online (Sandbox Code Playgroud)

然后gunicorn 从源安装。

pip3 install git+https://github.com/benoitc/gunicorn.git
Run Code Online (Sandbox Code Playgroud)

现在一切都运行没有问题。

  • 这是在 OSX 上对我有用的唯一答案(跳过 apt-get 部分)谢谢 (2认同)

小智 5

写了下面的脚本,用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!
Run Code Online (Sandbox Code Playgroud)