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

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 = 3user = '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.wsgiuser=nobodyautostart=trueautorestart=truestderr_logfile=/var/log/gunicorn3.err.logstdout_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/py3envsource /home/py3env/bin/activatepip3 install gunicorn/home/py3env/bin/gunicorn -w4 -b0.0.0.0:8000 [projectname].wsgi