-1

So I am trying to follow this here:

https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-16-04

except on the latest raspberry pi. I think or thought I had everything configured correctly...

In that tutorial above it gets you from creating a django project to getting it to run under gunicorn and even testing that with:

activating the virtual env and then

cd ~/myproject
gunicorn --bind 0.0.0.0:8000 myproject.wsgi

That part works, once that comes up it is like i did a python3 manage.py runserver 0.0.0.0:8000

i can Then goto my browser and type in the host name:

webserver2.abc.com:8000

and that all works app comes up.

If I stop the gunicorn command and type deactivate then I try to run it using the .service I added in /etc/systemd/system. It seems to run and a

 sudo systemctl status gunicorn


● gunicorn.service - gunicorn daemon
   Loaded: loaded (/etc/systemd/system/gunicorn.service; disabled; vendor preset: enabled)
   Active: active (running) since Wed 2020-10-21 13:24:51 MDT; 50s ago
 Main PID: 7645 (gunicorn)
    Tasks: 4 (limit: 4915)
   Memory: 55.0M
   CGroup: /system.slice/gunicorn.service
           ├─7645 /home/pi/myvirtualenv_covid19/covid19env/bin/python /home/pi/myvirtualenv_covid19/covid19env/bin/gunicorn --workers 3 --bind unix:/tmp/covid19.sock covid19.wsgi:application
           ├─7651 /home/pi/myvirtualenv_covid19/covid19env/bin/python /home/pi/myvirtualenv_covid19/covid19env/bin/gunicorn --workers 3 --bind unix:/tmp/covid19.sock covid19.wsgi:application
           ├─7652 /home/pi/myvirtualenv_covid19/covid19env/bin/python /home/pi/myvirtualenv_covid19/covid19env/bin/gunicorn --workers 3 --bind unix:/tmp/covid19.sock covid19.wsgi:application
           └─7653 /home/pi/myvirtualenv_covid19/covid19env/bin/python /home/pi/myvirtualenv_covid19/covid19env/bin/gunicorn --workers 3 --bind unix:/tmp/covid19.sock covid19.wsgi:application

Oct 21 13:24:51 webserver2 systemd[1]: Started gunicorn daemon.
Oct 21 13:24:51 webserver2 gunicorn[7645]: [2020-10-21 13:24:51 -0600] [7645] [INFO] Starting gunicorn 20.0.4
Oct 21 13:24:51 webserver2 gunicorn[7645]: [2020-10-21 13:24:51 -0600] [7645] [INFO] Listening at: unix:/run/gunicorn.sock (7645)
Oct 21 13:24:51 webserver2 gunicorn[7645]: [2020-10-21 13:24:51 -0600] [7645] [INFO] Using worker: sync
Oct 21 13:24:51 webserver2 gunicorn[7645]: [2020-10-21 13:24:51 -0600] [7651] [INFO] Booting worker with pid: 7651
Oct 21 13:24:51 webserver2 gunicorn[7645]: [2020-10-21 13:24:51 -0600] [7652] [INFO] Booting worker with pid: 7652
Oct 21 13:24:51 webserver2 gunicorn[7645]: [2020-10-21 13:24:51 -0600] [7653] [INFO] Booting worker with pid: 7653

To confirm.

So it seems to be running, though going to webserver2.abc.com:8000 again doesn't work this time and here is the gunicorn.service in /etc/systemd/system

[Unit]
Description = gunicorn daemon
Requires=gunicorn.socket
After=network.target

[Service]
User=pi
WorkingDirectory=/home/pi/myvirtualenv_covid19/covid19
ExecStart=/home/pi/myvirtualenv_covid19/covid19env/bin/gunicorn --workers 3 --bind unix:/tmp/covid19.sock covid19.wsgi:application
Restart=always

[Install]
WantedBy=multi-user.target

Curious there is no port to bind to? Maybe I am missing an extra config in my django project???

So secondly getting nginx to see any of this seems an issue, because if i goto

http://webserver2.abc.com:8080/

Gives me a 502 bad gateway. Usually tells me the error in the /var/log/nginx :

2020/10/21 13:33:11 [crit] 1836#1836: *65 connect() to unix:/tmp/covid19.sock failed (2: No such file or directory) while connecting to upstream, client: 10.75.1.245, server: , request: "GET / HTTP/1.1", upstream: "http://unix:/tmp/covid19.sock:/", host: "webserver2.abc.com:8080"

It seems it is running but no sock file in /tmp leads me to believe gunicorn is not running from the systemd/system .service file

So the sock isn't being created it seems, though the gunicorn.socket file does set to have a /run/gunicorn.sock file that is there. Just the /tmp/covid19.sock file is not.

I feel I am close just need a bit of help to connect the dots.

(also my nginx is set to listen on port 8080 but I don't see where I am telling in my gunicorn.service file for it to run on 8080?)

and for completeness my covid19.conf file from sites-available in /etc/nginx/

server {
    listen 8080;

    location = /favicon.ico { access_log off; log_not_found off; }

    location /static/ {
        root /home/pi/myvirtualenv_covid19/covid19;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/tmp/covid19.sock;
    }
}
Codejoy
  • 67
  • 3
  • 13

1 Answers1

1

OK, I've just been through setting up a Pi to serve my django app using the same instructions (I actually found your question before finding the digitalocean guide to follow, as I knew I didn't want 20.04/upstart).

Following the guide worked OK for me, so I'll try to help out. The things that I've noticed to look into are:

[Unit]
Description = gunicorn daemon
**Requires=gunicorn.socket**
After=network.target

[Service]
User=pi
WorkingDirectory=/home/pi/myvirtualenv_covid19/covid19
ExecStart=/home/pi/myvirtualenv_covid19/covid19env/bin/gunicorn --workers 3 --bind     unix:/tmp/covid19.sock covid19.wsgi:application
Restart=always

[Install]
WantedBy=multi-user.target

I didn't have the requires=gunicorn.socket in there in mine.

I put the socket file in my home directory for my user to avoid any permissions issues (I've had that be the issue before), so I'd recommend doing that.

AFAIK gunicorn communicates with nginx via the socket, so it doesn't need to be told a port to listen on - nginx does the listening and then passes that on to gunicorn.

I think you may need to check the logs, but I'd reckon it'd be between the extra line in the gunicorn.service file, and the location of the socket that would be the first place to start for me; with those things done (no reference, socket in home folder) I've got mine up and running without issue.

HTH

  • I got this working, it turns out the gunicorn wasn't really starting up I used the above ExecStart and it works! now I have to figure out why m firewall apache won't talk to it so I can see the website from the outside world but that much closer! – Codejoy Oct 28 '20 at 19:54