Gunicorn unable to start after server update ( 502 bad gateway )

November 5, 2019 195 views
Nginx Django

Hello, I have updated my old Ubuntu server from version 14 to 16.
This also included a Django and Python update.
After restarting the server, I got a 502 error from Nginx, which I have discovered is being caused by Gunicorn not being able to start.
I am able to start gunicorn manually, and the site works perfectly, by doing:

root@server:/home/django/django_project/django_project# gunicorn -b 127.0.0.1:9000 wsgi

wsgi.py is located at /home/django/djangoproject/djangoproject

The problem I am having is that the gunicorn service is not able to start automatically, even if the config (/etc/init/gunicorn.conf) looks correct:

[...]

chdir /home/django
exec gunicorn \
--name=django_project \
--pythonpath=django_project \
--bind=127.0.0.1:9000 \
--config /etc/gunicorn.d/gunicorn.py \
django_project.wsgi:application 

Any ideas of why the service is not starting by itself anymore?

service gunicorn restart

does not work either.

1 comment
3 Answers

So, just for everyone to know, I finally solved this by editing /etc/init/gunicorn.conf

The file was assuming gunicorn was in my django folder, but in reality it is located at /usr/bin

So, the old file was:

setuid django
setgid django
chdir /home/django

exec gunicorn \
    --name=django_project \
    --pythonpath=django_project \
    --bind=127.0.0.1:9000 \
    --config /etc/gunicorn.d/gunicorn.py \
    django_project.wsgi:application

The new file is now:

setuid django
setgid django
chdir /home/django/django_project

exec usr/bin/gunicorn \
    --bind=127.0.0.1:9000 \
    --config /etc/gunicorn.d/gunicorn.py \
    django_project.wsgi:application

So I made it execute gunicorn from its real location ( it was not there before the update ) and I also cleaned up the command by removing name and pythonpath.

Hi @baroale,

Can you show us the error you are experiencing when you try to perform

service gunicorn restart

If it doesn’t start, it should at least have an error being logged somewhere, perhaps

journalctl -xe

Regards,
KDSys

Hello, first of all, the gunicorn log file is not getting written anymore:

/var/log/upstart/gunicorn.log

Regarding the log files you mentioned ( journalctl -xe ), what I am getting is:

- Unit gunicorn.service has begun starting up.

Nov 66 06:33:09 PythonServer gunicorn[9300]: * Starting Gunicorn workers

Nov 66 06:33:09 PythonServer gunicorn[9300]: vetc’gunicorn.d/gunicorn.py:2: RuntimeWarning: Parent nodule ’ etc/gunicorn.d/gunic
Nov 66 06:33:09 PythonServer gunicorn[9300]: from multiprocessing import cpu_count

Nov 06 06:33:09 PythonServer gunicorn(9300]: vetc/gunicorn.d/gunicorn.py:3: RuntimeWarning: Parent module ‘etc’gunicorn.d/gunic
Nov 66 06:33:09 PythonServer gunicorn[9300]: fron os import environ

Nov 66 06:33:09 PythonServer gunicorn[9300]:

Nov 06 06:33:09 PythonServer systend(1]: Started gunicorn.service.

— Subject: Unit gunicorn.service has finished start-up

- Def ined-By: systend

- Support: http:// lists .freedesktop.org/mailman/ list info/systemd—devel

- Unit gunicorn.service has finished starting up.

- The start-up result is done.

Nov 66 06:33:46 PythonServer sshd(93161]: pam_unix(sshd:auth): authentication failure: logname= uid=8 euid=0 tty=ssh ruser= rhost
Nov 66 06:33:43 PythonServer sshd[93141]: pam_unix(sshd:auth): authentication failure: logname= uid=@ euid=0 tty=ssh ruser= rhost
Nov 66 06:33:43 PythonServer sshd(93161]: Failed password for root fron 51.38.186.207 port 37778 ssh2

Nov 66 06:33:43 PythonServer sshd(9316): Received disconnect fron 51.38.186.267 port 37778:11: Bye Bye Cpreauth]

Nov 06 06:33:43 PythonServer sshd(93161]: Disconnected from 51.38.186.207 port 37778 Cpreauth]

Nov 06 06:33:44 PythonServer sshd[9314]: Failed password for root from 210.177.54.141 port 50740 sshz

Nov 66 06:33:44 PythonServer sshd[9314]1: Received disconnect from 210.177.54.141 port 50740:11: Bye Bye [preauth]

Nov 06 06:33:44 PythonServer sshd([9314]: Disconnected from 216.177.54.141 port 56740 [preauth]

lines 989 1035/1035 CEND)

I also had another idea: Maybe the django user had no permission to run gunicorn ( because I am manually running it as root ), but when I do su django and run the manual gunicorn command, it still runs OK.

Have another answer? Share your knowledge.