Gunicorn Setup

  • Gunicorn is an application server (Nginx is serving proxy to Gunicorn)
  • sudo vim /etc/systemd/system/gunicorn.socket
    [Unit]
    Description=gunicorn socket
    
    [Socket]
    ListenStream=/run/gunicorn.sock
    
    [Install]
    WantedBy=sockets.target
  • sudo vim /etc/systemd/system/gunicorn.service
    [Unit]
    Description=gunicorn daemon
    Requires=gunicorn.socket
    After=network.target
    
    [Service]
    User=USERNAME
    Group=www-data
    WorkingDirectory=/home/USERNAME/MY_PROJECT_DIR
    ExecStart=MY_PROJECT_PATH/bin/gunicorn \
              --access-logfile - \
              --workers 3 \
              --bind unix:/run/gunicorn.sock \
              MY_PROJECT.wsgi:application
    
    [Install]
    WantedBy=multi-user.target
  • sudo systemctl start gunicorn.socket
  • sudo systemctl enable gunicorn.socket
  • sudo systemctl status gunicorn.socket
  • file /run/gunicorn.sock, output should be: /run/gunicorn.sock: socket, if some issues, check sudo journalctl -u gunicorn.socket and look at /etc/systemd/system/gunicorn.socket to fix some problems
  • sudo systemctl status gunicorn - Active: inactive (dead)
  • curl --unix-socket /run/gunicorn.sock localhost - output should be some html
  • sudo systemctl status gunicorn - Active: active (running), if some issues, check sudo journalctl -u gunicorn and /etc/systemd/system/gunicorn.service and if you will make any changes, reload the daemon sudo systemctl daemon-reload and sudo systemctl restart gunicorn


Resources:

➡️https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-django-with-postgres-nginx-and-gunicorn

➡️https://www.digitalocean.com/community/tutorials/how-to-deploy-a-local-django-app-to-a-vps

➡️https://docs.gunicorn.org/

➡️http://rahmonov.me/posts/run-a-django-app-with-nginx-and-gunicorn/

➡️https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-debian-9