Skip to content

Instantly share code, notes, and snippets.

@afk-mario
Last active February 22, 2024 22:15
Show Gist options
  • Save afk-mario/1cab38aa37eeb69e6ff21a8e107654cd to your computer and use it in GitHub Desktop.
Save afk-mario/1cab38aa37eeb69e6ff21a8e107654cd to your computer and use it in GitHub Desktop.
Django + Gunicorn + VirtualEnv + Systemd
[Unit]
Description=Gunicorn for project %I
After=network.target
After=syslog.target
[Service]
EnvironmentFile=/etc/conf.d/django_%i
ExecStart=/usr/bin/start_gunicorn
Restart=always
SyslogIdentifier=gunicorn%I
[Install]
WantedBy=multi-user.target
PORT=8000
SRC_PATH="<PATH_TO_DJANGO_APP>"
ENV_PATH="<PATH_TO_VIRTUAL_ENVIROMENT>"
APP_NAME="<NAME_OF_THE_APP"
# Make sure /var/log/gunicorn exists!
ACCESS_LOGFILE="/var/log/gunicorn/NAME_OF_THE_APP_access.log"
ERROR_LOGFILE="/var/log/gunicorn/NAME_OF_THE_APP_error.log"
#!/bin/bash
exec "${ENV_PATH}/bin/gunicorn" \
--bind="127.0.0.1:${PORT}" \
--access-logformat="%({X-Real-IP}i)s %(l)s %(u)s %(t)s \'%(r)s\' %(s)s %(b)s \'%(f)s\' \'%(a)s\'" \
--access-logfile="$ACCESS_LOGFILE" \
--error-logfile="$ERROR_LOGFILE" \
--log-level="warning" \
--chdir="$SRC_PATH" \
"${APP_NAME}.wsgi:application"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment