如何让Gunicorn使用Python 3而不是python2(502坏网关)

时间:2021-12-19 23:58:15

I'm trying to get Gunicorn to use Python3 for a Django app I want to make. I'm using Digital Ocean's Django image to get started. It comes with Django, Gunicorn, and Nginx installed and configured. The default Django project that comes with this image seems to work fine for Python 2.

我想让Gunicorn用Python3来做我想做的Django应用。我正在用数字海洋的Django图像开始。它有Django、Gunicorn和Nginx安装和配置。这个映像附带的默认Django项目似乎适合Python 2。

I've apt-get'ed these packages.

我apt-get这些包。

  • python3
  • python3
  • python3-psycopg2
  • python3-psycopg2
  • python3-dev
  • python3-dev
  • python3-pip
  • python3-pip

In order to try to avoid any problems, I've also done this.

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

  • pip uninstall django
  • 皮普卸载django
  • pip3 install django
  • pip3安装django

I rm -rf'ed the stock project and created a new one with django-admin.py startproject django_project. django-admin.py uses Python 3 (according to the shebang). Later, I use python3 manage.py startapp django_app to create a new app.

我对股票项目进行了射频识别,并与django-admin创建了一个新的项目。py startproject django_project。django-admin。py使用Python 3(根据shebang)。稍后,我使用python3管理。py startapp django_app创建一个新应用。

At this point, everything works fine. Just like the default app. Then, in django_app/views.py I do this and it breaks.

在这一点上,一切都很好。就像默认的应用程序一样,然后在django_app/视图中。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.")

The error page says I'm using Python 2.7.6.

错误页面说我使用的是python2.7.6。

Okay, so then I thought I could install Gunicorn through pip for Python 3, so I do this.

好了,我想我可以通过pip为Python 3安装Gunicorn,所以我就这么做了。

  • pip uninstall gunicorn
  • 皮普卸载gunicorn
  • pip3 install gunicorn
  • pip3安装gunicorn

But then I just end up with 502 Bad Gateway. When I do service gunicorn status, I get gunicorn stop/waiting. I tried service gunicorn restart, but it still says gunicorn stop/waiting.

然后我就得到了502个坏网关。当我服役的时候,我就会有枪。我试过用枪来重新启动,但它还是说gunicorn停止/等待。

I did a which gunicorn and it's installed at /usr/local/bin/gunicorn. Uhh... I'm not really sure what else I could try. Any help would be greatly appreciated. Thanks.

我做了一个gunicorn,它安装在/usr/local/bin/gunicorn。喔…我不确定我还能尝试什么。非常感谢您的帮助。谢谢。

8 个解决方案

#1


6  

It's probably easier to start afresh. Tutorial at https://www.digitalocean.com/community/articles/how-to-install-and-configure-django-with-postgres-nginx-and-gunicorn.

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

I got it running on a fresh ubuntu 14.04 droplet. Install python3 and django and then simply follow the tutorial. Didn't do the postgres or virtualenv bits though.

我在一个新的ubuntu 14.04液滴上运行它。安装python3和django,然后简单地跟随教程。但是,没有做postgres或virtualenv比特。

#2


14  

In case the two links break one day, here's how I got it working.

如果这两个链接有一天会中断,下面是我如何让它工作的。

Starting after executing these instructions.

从执行这些指令开始。

  • pip uninstall gunicorn
  • 皮普卸载gunicorn
  • pip3 install gunicorn
  • pip3安装gunicorn

Install supervisor, sudo apt-get install supervisor.

安装主管,sudo apt-get安装主管。

Next, I needed to make gunicorn_config.py in the root of my project directory, which contains this.

接下来,我需要制作gunicorn_config。在我的项目目录的根目录中,包含这个。

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

Then, I created a configuration file for supervisor. vim /etc/supervisor/conf.d/gunicorn.conf, with these contents.

然后,我为主管创建了一个配置文件。vim /etc/supervisor/conf.d/gunicorn.相依,与这些内容。

[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

After that, I did a supervisorctl reread and supervisorctl update and then it all started working.

在那之后,我做了一个监督ctl再读和监督ctl更新,然后一切开始工作。

You can use supervisorctl status gunicorn to check if gunicorn is running or not. You can use supervisorctl restart gunicorn to restart.

您可以使用监督ctl状态gunicorn检查gunicorn是否正在运行。您可以使用监督ctl重启gunicorn重新启动。

#3


6  

my way:

我的方法:

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

#4


5  

Wrote the following script to switch to Python 3.4 with DigitalOcean's 14.04 Django image, as I wanted it to be a nice one-step setup... It will be maintained at https://gist.github.com/tr00st/190ab4de62f9b23bea69

写了下面的脚本切换到Python 3.4与DigitalOcean的14.04 Django图像,我希望它是一个好的一步设置…它将保持在https://github.com/tr00st/190ab4de62f9b23bea69。

The main issue with the setup for me was with gevent, switching to tornado for workers worked fine.

对我来说,最主要的问题是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!

#5


1  

If you look at the gunicorn executable, it's just a small python script:

如果你看一下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())

The important bit is the from gunicorn.app.wsgiapp import run line, which tells you the location of the module responsibe for running your app. If gunicorn is quality code (which it is), you should be able to import that module directly from the command line and run your app.

重要的一点是来自gunicorn.app。如果gunicorn是质量代码(它是),你应该能够直接从命令行导入该模块并运行你的应用程序。

"Importing the module directly from the command line" means using the -m command line switch.

“直接从命令行导入模块”意味着使用-m命令行开关。

When called with -m module-name, the given module is located on the Python module path and executed as a script.

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

Using the python of your choice:

使用您选择的python:

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

And sure enough it runs!

果然它跑了!

[2017-12-04 02:05:27 +0000] [24] [INFO] Starting gunicorn 19.7.1
[2017-12-04 02:05:27 +0000] [24] [INFO] Listening 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

This behavior is very useful, for example when running gunicorn from somewhere like a Dockerfile.

这种行为非常有用,例如,从一个类似Dockerfile的地方运行gunicorn。

#6


0  

I had the same issue on Digital Ocean using the droplet "Ubuntu Django on 14.04".

我在数字海洋上也有同样的问题,使用droplet“Ubuntu Django on 14.04”。

I realized that the 'gevent' worker type was the problem for me when using Python 3. Even though I checked with python3 -m pip freeze that 'gevent' was installed, it did not work. I changed it to 'sync' in /etc/gunicorn.d/gunicorn.py:

我意识到在使用Python 3时,“gevent”worker类型是我的问题。尽管我检查了python3 -m pip的冻结,但它并没有被安装。我将它改为“同步”在/etc/gunicorn.d/gunicorn.py:

...
worker_class = 'sync'
...

I restarted gunicorn:

我重新启动gunicorn:

sudo service gunicorn restart

I checked the gunicorn service was running by using service gunicorn status, and was able to see the welcome to django page by reaching my droplet's ip address.

我检查了gunicorn服务的运行,通过使用服务gunicorn的状态,并通过到达我的droplet的ip地址,看到了django页面的欢迎。

I hope that works for other people.

我希望对其他人有用。

#7


0  

Install a python3 virtual environment in your project folder

在项目文件夹中安装python3虚拟环境。

$ pipenv --three

Then run gunicorn in the activated environment

然后在激活的环境中运行gunicorn。

$ gunicorn [PROJECT].wgsi

#8


0  

Here is how i made it to work for me. I have installed gunicorn both with pip2 and pip3. I need both versions. The default one is gunicorn with pip2.

这就是我如何让它为我工作。我已经用pip2和pip3安装了gunicorn。我需要两个版本。默认的是带pip2的gunicorn。

What I did was I installed gunicorn with virtualenv using pip3 and looked at content of the file gunicorn under bin in the virtualenv folder, that says

我所做的就是用pip3安装了gunicorn,并查看了在virtualenv文件夹下的文件gunicorn的内容。

#!/********/virtualenv/gunicorn3/corto/bin/python3

# -*- 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())

I copied this file and put it elsewhere, then i changed

我复制了这个文件,把它放到了别的地方,然后我换了。

#!/********/virtualenv/gunicorn3/corto/bin/python3

to

#!/usr/bin/python3

Now you can run gunicorn easily like this after getting into the directory that you copied the file gunicorn in

现在你可以很容易地运行gunicorn,在进入你复制文件gunicorn的目录之后。

python3 gunicorn -c /your_config_file.py class_app:app

Note: doing pip3 uninstall gunicorn, followed by pip3 install gunicorn will install gunicron with python3 version (overriding the python2 version) in the dir 'usr/local/bin' (ubuntu) and then you can get the file contents of gunicron there. This will help you avoid using virtualenv.

注意:使用pip3卸载gunicorn,然后pip3安装gunicorn将在dir 'usr/local/bin' (ubuntu)中安装带有python3版本的gunicron(覆盖python2版本),然后您可以在那里获取gunicron的文件内容。这将帮助您避免使用virtualenv。

If this doesnt work first time, do pip3 unistall gunicorn, followed by pip3 install gunicorn.

如果这不是第一次使用,请使用pip3 unistall gunicorn,然后是pip3安装gunicorn。

Happy unicorning ;)

快乐的独角兽。

#1


6  

It's probably easier to start afresh. Tutorial at https://www.digitalocean.com/community/articles/how-to-install-and-configure-django-with-postgres-nginx-and-gunicorn.

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

I got it running on a fresh ubuntu 14.04 droplet. Install python3 and django and then simply follow the tutorial. Didn't do the postgres or virtualenv bits though.

我在一个新的ubuntu 14.04液滴上运行它。安装python3和django,然后简单地跟随教程。但是,没有做postgres或virtualenv比特。

#2


14  

In case the two links break one day, here's how I got it working.

如果这两个链接有一天会中断,下面是我如何让它工作的。

Starting after executing these instructions.

从执行这些指令开始。

  • pip uninstall gunicorn
  • 皮普卸载gunicorn
  • pip3 install gunicorn
  • pip3安装gunicorn

Install supervisor, sudo apt-get install supervisor.

安装主管,sudo apt-get安装主管。

Next, I needed to make gunicorn_config.py in the root of my project directory, which contains this.

接下来,我需要制作gunicorn_config。在我的项目目录的根目录中,包含这个。

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

Then, I created a configuration file for supervisor. vim /etc/supervisor/conf.d/gunicorn.conf, with these contents.

然后,我为主管创建了一个配置文件。vim /etc/supervisor/conf.d/gunicorn.相依,与这些内容。

[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

After that, I did a supervisorctl reread and supervisorctl update and then it all started working.

在那之后,我做了一个监督ctl再读和监督ctl更新,然后一切开始工作。

You can use supervisorctl status gunicorn to check if gunicorn is running or not. You can use supervisorctl restart gunicorn to restart.

您可以使用监督ctl状态gunicorn检查gunicorn是否正在运行。您可以使用监督ctl重启gunicorn重新启动。

#3


6  

my way:

我的方法:

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

#4


5  

Wrote the following script to switch to Python 3.4 with DigitalOcean's 14.04 Django image, as I wanted it to be a nice one-step setup... It will be maintained at https://gist.github.com/tr00st/190ab4de62f9b23bea69

写了下面的脚本切换到Python 3.4与DigitalOcean的14.04 Django图像,我希望它是一个好的一步设置…它将保持在https://github.com/tr00st/190ab4de62f9b23bea69。

The main issue with the setup for me was with gevent, switching to tornado for workers worked fine.

对我来说,最主要的问题是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!

#5


1  

If you look at the gunicorn executable, it's just a small python script:

如果你看一下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())

The important bit is the from gunicorn.app.wsgiapp import run line, which tells you the location of the module responsibe for running your app. If gunicorn is quality code (which it is), you should be able to import that module directly from the command line and run your app.

重要的一点是来自gunicorn.app。如果gunicorn是质量代码(它是),你应该能够直接从命令行导入该模块并运行你的应用程序。

"Importing the module directly from the command line" means using the -m command line switch.

“直接从命令行导入模块”意味着使用-m命令行开关。

When called with -m module-name, the given module is located on the Python module path and executed as a script.

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

Using the python of your choice:

使用您选择的python:

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

And sure enough it runs!

果然它跑了!

[2017-12-04 02:05:27 +0000] [24] [INFO] Starting gunicorn 19.7.1
[2017-12-04 02:05:27 +0000] [24] [INFO] Listening 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

This behavior is very useful, for example when running gunicorn from somewhere like a Dockerfile.

这种行为非常有用,例如,从一个类似Dockerfile的地方运行gunicorn。

#6


0  

I had the same issue on Digital Ocean using the droplet "Ubuntu Django on 14.04".

我在数字海洋上也有同样的问题,使用droplet“Ubuntu Django on 14.04”。

I realized that the 'gevent' worker type was the problem for me when using Python 3. Even though I checked with python3 -m pip freeze that 'gevent' was installed, it did not work. I changed it to 'sync' in /etc/gunicorn.d/gunicorn.py:

我意识到在使用Python 3时,“gevent”worker类型是我的问题。尽管我检查了python3 -m pip的冻结,但它并没有被安装。我将它改为“同步”在/etc/gunicorn.d/gunicorn.py:

...
worker_class = 'sync'
...

I restarted gunicorn:

我重新启动gunicorn:

sudo service gunicorn restart

I checked the gunicorn service was running by using service gunicorn status, and was able to see the welcome to django page by reaching my droplet's ip address.

我检查了gunicorn服务的运行,通过使用服务gunicorn的状态,并通过到达我的droplet的ip地址,看到了django页面的欢迎。

I hope that works for other people.

我希望对其他人有用。

#7


0  

Install a python3 virtual environment in your project folder

在项目文件夹中安装python3虚拟环境。

$ pipenv --three

Then run gunicorn in the activated environment

然后在激活的环境中运行gunicorn。

$ gunicorn [PROJECT].wgsi

#8


0  

Here is how i made it to work for me. I have installed gunicorn both with pip2 and pip3. I need both versions. The default one is gunicorn with pip2.

这就是我如何让它为我工作。我已经用pip2和pip3安装了gunicorn。我需要两个版本。默认的是带pip2的gunicorn。

What I did was I installed gunicorn with virtualenv using pip3 and looked at content of the file gunicorn under bin in the virtualenv folder, that says

我所做的就是用pip3安装了gunicorn,并查看了在virtualenv文件夹下的文件gunicorn的内容。

#!/********/virtualenv/gunicorn3/corto/bin/python3

# -*- 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())

I copied this file and put it elsewhere, then i changed

我复制了这个文件,把它放到了别的地方,然后我换了。

#!/********/virtualenv/gunicorn3/corto/bin/python3

to

#!/usr/bin/python3

Now you can run gunicorn easily like this after getting into the directory that you copied the file gunicorn in

现在你可以很容易地运行gunicorn,在进入你复制文件gunicorn的目录之后。

python3 gunicorn -c /your_config_file.py class_app:app

Note: doing pip3 uninstall gunicorn, followed by pip3 install gunicorn will install gunicron with python3 version (overriding the python2 version) in the dir 'usr/local/bin' (ubuntu) and then you can get the file contents of gunicron there. This will help you avoid using virtualenv.

注意:使用pip3卸载gunicorn,然后pip3安装gunicorn将在dir 'usr/local/bin' (ubuntu)中安装带有python3版本的gunicron(覆盖python2版本),然后您可以在那里获取gunicron的文件内容。这将帮助您避免使用virtualenv。

If this doesnt work first time, do pip3 unistall gunicorn, followed by pip3 install gunicorn.

如果这不是第一次使用,请使用pip3 unistall gunicorn,然后是pip3安装gunicorn。

Happy unicorning ;)

快乐的独角兽。