How to fix "Failed to restart gunicorn.service: Unit gunicorn.socket not found." error?

Viewed 1639

I'm trying to deploy a django application to a DigitalOcean droplet. I created a systemd service to start gunicorn on boot.

Here is my config file: (/etc/systemd/system/gunicorn.service)

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

[Service]
User=root
Group=www-data
Environment="DJANGO_SETTINGS_MODULE=core.settings.production"
WorkingDirectory=/home/myproject-api/src
ExecStart=/home/myproject-api/env/bin/gunicorn --access-logfile - --workers 3 --bind unix:/run/gunicorn.sock core.wsgi:application

[Install]
WantedBy=multi-user.target

When I run "ExecStart" line on directly on terminal, it works. But I cant start the gunicorn service.

I get this error when I try to start gunicorn: Failed to start gunicorn.service: Unit gunicorn.socket not found.

I checked the gunicorn executable, it exists:

test -f /home/myproject-api/env/bin/gunicorn && echo "Gunicorn exists."

I'm able to run server with gunicorn --bind 0.0.0.0:8000 core.wsgicommand. When I run like this, i can access the server using the server's IP address.

Normally, the socket file should been created when I start the server. Also I tried to create the socket file with "touch /run/gunicorn.sock" but it didn't work.

I double-checked file and directory names. No mistake.

How can I solve this problem?

1 Answers

I solved it by creating a /etc/systemd/system/gunicorn.socket file:

[Unit]
Description=gunicorn socket

[Socket]
ListenStream=/run/gunicorn.sock

[Install]
WantedBy=sockets.target

In Ubuntu 20, we have to create this file to run gunicorn service.

Related