Trouble running a Django app using Nginx and Gunicorn (502 Bad Gateway) in virtual environment.

October 9, 2018 1.6k views
Linux Basics Deployment Linux Commands Getting Started Django Nginx Ubuntu

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: 186.46.166.50, server: 46.101.159.58, request: "GET / HTTP/1.1", upstream: "http://unix:/run/gunicorn.sock:/", host: "46.101.159.58: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[1]: Started gunicorn daemon.
Oct 09 09:25:06 ubuntu-s-1vcpu-1gb-fra1-01 systemd[10224]: 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[10224]: 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[1]: gunicorn.service: Main process exited, code=exited, status=200/CHDIR
Oct 09 09:25:06 ubuntu-s-1vcpu-1gb-fra1-01 systemd[1]: 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.

1 Answer

I am having same issues and looks like there is no solutions!

  • I think I've eventually solved my problem and I forgot to post the solution. The reason was that i tried to run my app from /root/app/... directory. I think that Ubuntu blocks access to the root directory for security reasons.

    I've set up a new user on my Ubuntu webserver and put my app folder into the new user's directory. I hope this guide will help:

    https://www.digitalocean.com/community/tutorials/initial-server-setup-with-ubuntu-18-04

    by Justin Ellingwood
    When you first create a new Ubuntu 18.04 server, there are a few configuration steps that you should take early on as part of the basic setup. This will increase the security and usability of your server and will give you a solid foundation for subsequent...
Have another answer? Share your knowledge.