1

I am following this How To Set Up Django with Postgres, Nginx, and Gunicorn on Ubuntu 18.04 guide.

I have created the following file .socket

sudo nano /etc/systemd/system/gunicorn.socket

[Unit]
Description=gunicorn socket

[Socket]
ListenStream=/run/gunicorn.sock

[Install]
WantedBy=sockets.target

I have created the following file .service

sudo nano /etc/systemd/system/gunicorn.service

Original RECOMENDED_FORMATTING-s in the guide

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


[Service]
User=sammyRECOMENDED_FORMATTING
Group=www-data
WorkingDirectory=/home/sammyRECOMENDED_FORMATTING/myprojectdirRECOMENDED_FORMATTING
ExecStart=/home/sammyRECOMENDED_FORMATTING/myprojectdirRECOMENDED_FORMATTING/myprojectenvRECOMENDED_FORMATTING/bin/gunicorn \
          --access-logfile - \
          --workers 3 \
          --bind unix:/run/gunicorn.sock \
          myprojectRECOMENDED_FORMATTING.wsgi:application

[Install]
WantedBy=multi-user.target

How I have formatted my own version I had my virtual environment outside of the project folder on the server

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

[Service]
User=SERVER_USER
Group=www-data
WorkingDirectory=/home/SERVER_USER/MAIN_PROJECT_FOLDER
ExecStart=/home/SERVER_USER/ven/bin/gunicorn \
          --access-logfile - \
          --workers 3 \
          --bind unix:/home/SERVER_USER/MAIN_PROJECT_FOLDER/MAINAPPLICATION_FOLDER.sock \
          MAINAPPLICATION_FOLDER.wsgi:application

[Install]
WantedBy=multi-user.target

I have also tried leaving these as originally recommended

--bind unix:/run/gunicorn.sock \

Than I have tried to execute the following code

sudo systemctl start gunicorn

error message 1

Failed to start gunicorn.service: Unit gunicorn.service not found.

and I have also tried with 2

sudo systemctl start gunicorn.socket

Error message 2

Failed to start gunicorn.socket: Unit gunicorn.socket is not loaded properly: Invalid argument.
See system logs and 'systemctl status gunicorn.socket' for details.

To solve this, I have tried

12
  • 1
    Can you check the unit files with systemctl list-unit-files | grep gunicorn Mar 24, 2020 at 20:02
  • 1
    Also to start a socket related service you have to do sudo systemctl start gunicorn.socket which will start the service also Mar 24, 2020 at 20:03
  • 1
    To enable socket run sudo systemctl enable gunicorn.socket the to check if both is enabled run thje commands systemctl is-enabled gunicorn.socket and systemctl is-enabled gunicorn.service . You did not mention creating the gunicorn socket anywhere. Mar 24, 2020 at 20:48
  • 1
    It appears the gunicorn.service may not be saved. Run ls -lF /etc/systemd/system/gunicorn.*. You should see two files 1) gunicorn.socket and 2) gunicorn.service. I suspect you may be missing gunicorn.service as indicated by unit file missing error Mar 24, 2020 at 21:31
  • 1
    Did you enable gunicorn.service with sudo systemctl enable gunicorn.service? After doing that run the commands sudo systemctl is-enabled gunicorn.service. Then check the status of service with command systemctl status gunicorn.service Mar 24, 2020 at 21:44

2 Answers 2

6

You have the [Install] section in your service which means you needs to run the command sudo systemctl enable gunicorn.service to start the service on system startup.

Once you run the command a symlink to this file will be created in the directory /etc/systemd/system/multi-user.target.wants/.

Note that you also need to create the socket file named gunicorn.socket and need to enable the same. Once you enable, you can activate the socket by running the command sudo systemctl start gunicorn.socket. You don't need to run the service as systemd will figure out the dependency.

You can check the status of the service with systemctl status gunicorn.service.

2
  • 1
    You made my day 😊👍
    – sogu
    Mar 24, 2020 at 23:03
  • you made my day as wellllll thank you!!!! Nov 23, 2022 at 13:53
2

in my case, the server didn't create thegunicorn.socket file. so, I had to create the file by myself and enable it.

go to:/etc/systemd/system and check all the available files and dirs by the command ls.

if the file is not there, try to re-create the file from the first by: sudo nano ~/etc/systemd/system/gunicorn.socket, or sudo nano gunicorn.socket.

and then enable it first by: sudo systemctl enable gunicorn.socket, after the enabling do: sudo systemctl start gunicorn.socket.

if everything works fine, you'll see the status of the file gunicorn.socket is active, after running the command sudo systemctl status gunicorn.socket you'll see:

     gunicorn.socket - gunicorn socket
     Loaded: loaded (/etc/systemd/system/gunicorn.socket; enabled; vendor prese>
     Active: active (listening) since Fri 2020-06-26 17:53:10 UTC; 14s ago
   Triggers: ● gunicorn.service
     Listen: /run/gunicorn.sock (Stream)
      Tasks: 0 (limit: 1137)
     Memory: 0B
     CGroup: /system.slice/gunicorn.socket

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .