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
- https://stackoverflow.com/questions/40711747/failed-to-start-gunicorn-service-unit-gunicorn-service-not-found This points back to the exact same guide that I am doing except with an older version of linux.
- this is not the same code and not answered https://askubuntu.com/questions/748836/unit-gunicorn-service-failed-to-load-no-such-file-or-directory
- Run:
systemctl status gunicorn.socket
Result:Warning: The unit file, source configuration file or drop-ins of gunicorn.socket changed on disk ● gunicorn.socket - gunicorn daemon Loaded: error (Reason: Invalid argument) Active: inactive (dead)
- Run:
systemctl is-enabled gunicorn.socket
Result:enabled
- Run:
systemctl is-enabled gunicorn.service
Result:Failed to get unit file state for gunicorn.service: No such file or directory
This fixed it:
sudo systemctl enable gunicorn.service