How to get Gunicorn to use Python 3 instead of Python 2 (502 Bad Gateway)

It seems that there's a package for this called gunicorn3 (this was tested on ubuntu)

sudo apt-get install gunicorn3

then running the following command should work and run gunicorn with python3:

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


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

Starting after executing these instructions.

  • pip uninstall gunicorn
  • pip3 install gunicorn

Install supervisor, sudo apt-get install supervisor.

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

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.

[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.

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


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

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.

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.