Easily configure a performant, secure, and stable NGINX server.Learn More
Trouble running a Django app using Nginx and Gunicorn (502 Bad Gateway) in virtual environment.
Hello, I have managed to work through this tutorial, but in the end when I typed my droplet’s IP Adress into the web browser I kept getting the 502 Bad Gateway error.
I checked the nginx error log (sudo tail -30 /var/log/nginx/error.log), and this is the last error:
2018/10/09 08:24:17 [error] 9762#9762: *6 connect() to unix:/run/gunicorn.sock failed (111: Connection refused) while connecting to upstream, client: 18.104.22.168, server: 22.214.171.124, request: "GET / HTTP/1.1", upstream: "http://unix:/run/gunicorn.sock:/", host: "126.96.36.199:80"
I figured out that either the gunicorn.socket or gunicorn.service is not running properly, so I ran sudo systemctl status gunicorn.socket and got this result:
● gunicorn.socket - gunicorn socket Loaded: loaded (/etc/systemd/system/gunicorn.socket; enabled; vendor preset: enabled) Active: active (listening) since Tue 2018-10-09 08:55:34 UTC; 17s ago Listen: /run/gunicorn.sock (Stream) CGroup: /system.slice/gunicorn.socket
So the gunicorn.socket is running properly, then I checked the gunicorn.service (sudo systemctl status gunicorn
) and I got this result:
● gunicorn.service - gunicorn daemon Loaded: loaded (/etc/systemd/system/gunicorn.service; disabled; vendor preset: enabled) Active: failed (Result: exit-code) since Tue 2018-10-09 09:25:06 UTC; 3s ago Process: 10224 ExecStart=/my_website/venv/bin/gunicorn -- access-logfile - --workers 3 --bind unix:/run/gunicorn.sock my_website.wsgi:application (code=exited, status=200/CHDIR) Main PID: 10224 (code=exited, status=200/CHDIR) Oct 09 09:25:06 ubuntu-s-1vcpu-1gb-fra1-01 systemd: Started gunicorn daemon. Oct 09 09:25:06 ubuntu-s-1vcpu-1gb-fra1-01 systemd: gunicorn.service: Changing to the requested working directory failed: No such file or directory Oct 09 09:25:06 ubuntu-s-1vcpu-1gb-fra1-01 systemd: gunicorn.service: Failed at step CHDIR spawning /my_website/venv/bin/gunicorn: No such file or directory Oct 09 09:25:06 ubuntu-s-1vcpu-1gb-fra1-01 systemd: gunicorn.service: Main process exited, code=exited, status=200/CHDIR Oct 09 09:25:06 ubuntu-s-1vcpu-1gb-fra1-01 systemd: gunicorn.service: Failed with result 'exit-code'.
So it looks like something in my gunicorn.service vonfig file is wrong, but I checked this file and everything looks fine to me. This is my config:
[Unit] Description=gunicorn daemon Requires=gunicorn.socket After=network.target [Service] User=root WorkingDirectory=/my_website/ ExecStart=/my_website/venv/bin/gunicorn \ -- access-logfile - \ --workers 3 \ --bind unix:/run/gunicorn.sock \ my_website.wsgi:application [Install] WantedBy=multi-user.target
Anyway, when I change the directory to /my_website/bin/gunicorn and list it’s content it looks like the gunicorn file is there:
root@ubuntu-s-1vcpu-1gb-fra1-01:~/my_website/venv/bin# ls __pycache__ activate.csh activate_this.py django-admin easy_install **gunicorn** pip pip3.6 python-config python3.6 wagtail activate activate.fish chardetect django-admin.py easy_install-3.6 gunicorn_paster pip3 python python3 unidecode wheel
But when I run the command “ file /my_website/venv/bin/gunicorn”, it says that the file is not there.
The question is, how should I define the WorkingDirectory and ExecStart paths? I tried different combinations and none of them seems to work.
These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.×