Gunicorn Deployment Example
A snippet showing how to deploy Django with Gunicorn.
# Install Gunicorn inside your virtualenv
pip install gunicorn
# Quick start (from project root containing project/wsgi.py)
gunicorn project.wsgi:application
# Bind to host/port and set workers (rule of thumb: 2-4 x CPU cores)
gunicorn --bind 0.0.0.0:8000 --workers 4 project.wsgi:application
# Use a Unix socket (recommended with Nginx)
gunicorn --bind unix:/run/gunicorn.sock --workers 4 project.wsgi:application
# Example systemd unit - /etc/systemd/system/gunicorn.service
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=www-data
Group=www-data
WorkingDirectory=/srv/project
Environment="DJANGO_SETTINGS_MODULE=project.settings.prod"
ExecStart=/srv/venv/bin/gunicorn --workers 4 --bind unix:/run/gunicorn.sock project.wsgi:application
Restart=on-failure
RuntimeDirectory=gunicorn
RuntimeDirectoryMode=0755
[Install]
WantedBy=multi-user.target
# Enable & start
sudo systemctl daemon-reload
sudo systemctl enable gunicorn
sudo systemctl start gunicorn
# Tuning flags (examples)
# Increase timeout for slow startups/requests
gunicorn --timeout 60 project.wsgi:application
# Use gevent workers for many concurrent keep-alive connections
pip install gevent
gunicorn -k gevent --workers 4 --worker-connections 1000 project.wsgi:application
# Set access/error logs
gunicorn --access-logfile - --error-logfile - project.wsgi:application
Explanation:
- Gunicorn is a performant WSGI server well-suited for synchronous Django apps.
-
Use
--workersto match CPU cores; benchmark your workload to choose the best number. -
Swap to
-k uvicorn.workers.UvicornWorkeronly for ASGI apps (see ASGI snippet). - Centralize logs to stdout/stderr for containerized deployments.
- Category Deployment & Settings
- Total Views 545
- Last Modified 02 January, 2026
- Tags #deployment #gunicorn #wsgi #server
Previous snippet
Setup ASGI for Async Deployment
Next snippet