Installation

Install Gunicorn

Install Gunicorn within your virtual environment.

pip install gunicorn

Systemd Service Configuration

Create Service File

Create file at /etc/systemd/system/billing-gunicorn.service.

sudo nano /etc/systemd/system/billing-gunicorn.service

Service Configuration Template

Update User, WorkingDirectory, and paths to match your project.


[Unit]
Description=Gunicorn service for Django (Billing)
After=network.target

[Service]
User=user
Group=www-data

WorkingDirectory=/home/user/My_Billing

Environment="PATH=/home/user/My_Billing/venv/bin"
Environment="DJANGO_SETTINGS_MODULE=Billing.settings"
Environment="PYTHONUNBUFFERED=1"

ExecStart=/home/user/My_Billing/venv/bin/gunicorn \
    --workers 3 \
    --timeout 120 \
    --bind unix:/run/billing-gunicorn/billing.sock \
    --access-logfile - \
    --error-logfile - \
    Billing.wsgi:application

Restart=always
RestartSec=5

RuntimeDirectory=billing-gunicorn
RuntimeDirectoryMode=0755

[Install]
WantedBy=multi-user.target

Service Management

Start & Enable

Commands to start the service and check status.

# Reload systemd to find the new service
sudo systemctl daemon-reload

# Start and enable boot launch
sudo systemctl start billing-gunicorn
sudo systemctl enable billing-gunicorn

# Check status
sudo systemctl status billing-gunicorn

# Restart Gunicorn Service
sudo systemctl restart billing-gunicorn