Production Django Deployment
Deploying Django securely requires separating the application server (Gunicorn) from the web server (Nginx).
1. Environment Setup
bash
sudo apt update
sudo apt install python3-pip python3-venv nginx2. Project Setup
bash
mkdir /var/www/myproject
cd /var/www/myproject
python3 -m venv venv
source venv/bin/activate
pip install django gunicorn psycopg2-binary3. Gunicorn Systemd Service
Create `/etc/systemd/system/gunicorn.service`:
ini
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=www-data
Group=www-data
WorkingDirectory=/var/www/myproject
ExecStart=/var/www/myproject/venv/bin/gunicorn --access-logfile - --workers 3 --bind unix:/var/www/myproject/myproject.sock myproject.wsgi:application
[Install]
WantedBy=multi-user.target4. Nginx Configuration
nginx
server {
listen 80;
server_name example.com;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /var/www/myproject;
}
location / {
include proxy_params;
proxy_pass http://unix:/var/www/myproject/myproject.sock;
}
}Page changelog
Last updated
- 2024-03-25—Initial or baseline update for this page.
Related articles
Deployment
Integrations & Plugins — Connect Your Stack
Patterns for integrating monitoring, auth, storage, CI/CD, and webhooks across your services.
Deployment
Deploying Next.js App Router on Cloud VPS
A comprehensive guide to hosting Next.js applications using Node.js, PM2, and Nginx reverse proxy architecture.
Deployment
Self-Hosted GitLab CE Deployment
Take control of your DevOps pipeline by hosting your own GitLab instance on a cloud VPS.
Deployment
Deploying Strapi Headless CMS
Setting up Strapi v4 with a PostgreSQL database for a robust content API.
Deployment
Self-Hosting a PaaS with Coolify
An open-source, self-hosted alternative to Heroku/Vercel for deploying your apps easily.
Deployment
S3-Compatible Storage with MinIO
Deploying high-performance private object storage for backups, artifacts, and large datasets.
Was this page helpful?