3

In my mission to deploy a python project, I have gotten to gunicorn setup.

Checking for gunicorn status file (I just ran this command earlier today and it returned the following):

sudo systemctl status gunicorn.socket
Failed to dump process list, ignoring: No such file or directory
● gunicorn.socket - gunicorn socket
   Loaded: loaded (/etc/systemd/system/gunicorn.socket; enabled; vendor preset: enabled)
   Active: active (listening) since Sun 2020-02-16 21:42:10 UTC; 1min 16s ago
   Listen: /run/gunicorn.sock (Stream)
   CGroup: /system.slice/gunicorn.socket

I just now went to run this command and the output was different:

sudo systemctl status gunicorn.socket
● gunicorn.socket - gunicorn socket
   Loaded: loaded (/etc/systemd/system/gunicorn.socket; enabled; vendor preset: enabled)
   Active: failed (Result: service-start-limit-hit) since Sun 2020-02-16 21:45:01 UTC; 28min ago
   Listen: /run/gunicorn.sock (Stream)

Feb 16 21:42:10 ubuntu-s-1vcpu-1gb-nyc3-01 systemd[1]: Listening on gunicorn socket.
Feb 16 21:45:01 ubuntu-s-1vcpu-1gb-nyc3-01 systemd[1]: gunicorn.socket: Failed with result 'service-st
lines 1-7/7 (END)

I ran systemctl restart ssh, this did not fix the issue.

Anyone know what is causing this?

F1Linux
  • 3,580
  • 3
  • 25
  • 24
Justin
  • 1,329
  • 3
  • 16
  • 25

2 Answers2

3

I faced the problem like this 3 days ago. Checking /var/log/syslog is the first way to solve the problem. Mostly is because misconfiguration in gunicorn or nginx (or what ever web server you use). I follow the instruction in this link

Let say you have folder structure like this (in my assumption, you install django and use wsgi):

  1. /home/user/venv/myvenv -> Your virtual environment directory
  2. /home/user/venv/myvenv/myproject -> your project directory
  3. /home/user/venv/myvenv/bin/gunicorn -> gunicorn file after installation via pip


If you create gunicorn service file (let say /etc/systemd/system/gunicorn.service), put the file gunicorn.sock in /run/gunicorn.sock, you can follow configuration below or similar like this:

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

[Service]
User=user
Group=www-data
WorkingDirectory=/home/user/venv/myvenv/myproject
ExecStart=/home/user/venv/myvenv/bin/gunicorn \
    --access-logfile - \
    --workers 3 \
    --bind unix:/run/gunicorn.sock \
    myproject.wsgi:application

[Install]
WantedBy=multi-user.target

Please note that myproject is your python project. Sometimes, it won't work because gunicorn doesn't know myproject folder location. You can add --chdir option inside ExecStart to become like this:

ExecStart=/home/user/venv/myvenv/bin/gunicorn \
    --access-logfile - \
    --workers 3 \
    --bind unix:/run/gunicorn.sock \
    --chdir /home/user/venv/myvenv/myproject \
    myproject.wsgi:application

The --chdir is use to point the project folder. Don't forget to reload the service, everytime you change gunicorn.service, before you restart it.

$ sudo systemctl daemon-reload

and then:

$ sudo systemctl restart gunicorn

Don't be confuse with myproject.wsgi. It's not the filename. It will call file wsgi inside myproject folder.

muchtarsp
  • 227
  • 1
  • 11
  • > `WorkingDirectory=/home/user/venv/myvenv/myproject` Why is your project/working directory under `venv/myenv/`? My `.venv` is in the same directory as `myproject`, not in the directory of my virtual environment. That's odd. – xtian Jul 24 '21 at 13:55
  • Because I need a simple way to do this. Placed my project directory under virtualenv directory. As long as you can connect in which virtual enviroment you use, you can place your working directory anywhere. I put my note in my answer: "... you can follow configuration below or similar like this ...". VENV is a folder of my all project using virtual environment. MYENV is a folder for my specific environmet. MYPROJECT is a folder of my project using MYENV as virtual enviroment. – muchtarsp Jul 28 '21 at 04:28
  • Thank you so much for your explanation. I couldn't find anywhere else that described what the "WorkingDirectory" was actually looking for, so kept running into errors with gunicorn trying to find the wsgi file. Appreciate your explanation! – Josh Jun 18 '22 at 17:16
0

You need to catch what's going on with gunicorn when it change state from active to failed. Check with sudo tail -f /var/log/syslog after active it with sudo systemctl restart gunicorn.socket

Try to access to your website or whatever gunicorn is doing and it should change state and print more informations in syslog.

If gunicorn is lunch from python environment try to reinstall environment like on this case.

Ythollet
  • 21
  • 3