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

  • 1
    Can you check the unit files with systemctl list-unit-files | grep gunicorn – randominstanceOfLivingThing Mar 24 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 – randominstanceOfLivingThing Mar 24 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. – randominstanceOfLivingThing Mar 24 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 – randominstanceOfLivingThing Mar 24 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 – randominstanceOfLivingThing Mar 24 at 21:44
1

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.

| improve this answer | |
  • You made my day 😊👍 – sogu Mar 24 at 23:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.