Production Deployment Guide¶
This guide covers deploying the NSGG Backend to a production environment.
Prerequisites¶
- Docker and Docker Compose
- PostgreSQL 15+
- Redis 7+
- Domain name and SSL certificate
- Cloud provider account (AWS/GCP/Azure)
Environment Setup¶
- Set up environment variables:
# .env.production
DEBUG=False
ALLOWED_HOSTS=api.yourdomain.com
DATABASE_URL=postgres://user:password@host:5432/nsgg
REDIS_URL=redis://host:6379/0
SECRET_KEY=your-secure-secret-key
STRIPE_PUBLIC_KEY=your-stripe-public-key
STRIPE_SECRET_KEY=your-stripe-secret-key
STRIPE_WEBHOOK_SECRET=your-stripe-webhook-secret
- Configure SSL certificates:
- Obtain SSL certificate from Let's Encrypt or your provider
- Configure certificate paths in Nginx
Database Setup¶
- Create production database:
CREATE DATABASE nsgg;
CREATE USER nsgg_user WITH PASSWORD 'secure_password';
GRANT ALL PRIVILEGES ON DATABASE nsgg TO nsgg_user;
- Configure database backups:
Docker Configuration¶
- Production Dockerfile:
FROM python:3.12-slim
ENV PYTHONUNBUFFERED=1
ENV PYTHONDONTWRITEBYTECODE=1
WORKDIR /app
RUN apt-get update && apt-get install -y \
postgresql-client \
&& rm -rf /var/lib/apt/lists/*
COPY requirements/production.txt .
RUN pip install --no-cache-dir -r production.txt
COPY . .
RUN python manage.py collectstatic --noinput
CMD ["gunicorn", "config.wsgi:application", "--bind", "0.0.0.0:8000"]
- Docker Compose for production:
version: '3.8'
services:
web:
build:
context: .
dockerfile: Dockerfile
environment:
- DATABASE_URL=${DATABASE_URL}
- REDIS_URL=${REDIS_URL}
ports:
- "8000:8000"
depends_on:
- redis
redis:
image: redis:7-alpine
volumes:
- redis_data:/data
nginx:
image: nginx:alpine
ports:
- "80:80"
- "443:443"
volumes:
- ./nginx:/etc/nginx/conf.d
- ./static:/static
- ./media:/media
- /etc/letsencrypt:/etc/letsencrypt
depends_on:
- web
volumes:
redis_data:
Nginx Configuration¶
# /etc/nginx/conf.d/nsgg.conf
upstream nsgg_backend {
server web:8000;
}
server {
listen 80;
server_name api.yourdomain.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
server_name api.yourdomain.com;
ssl_certificate /etc/letsencrypt/live/api.yourdomain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/api.yourdomain.com/privkey.pem;
location / {
proxy_pass http://nsgg_backend;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
location /static/ {
alias /static/;
expires 1d;
add_header Cache-Control "public, no-transform";
}
location /media/ {
alias /media/;
expires 1d;
add_header Cache-Control "public, no-transform";
}
}
Deployment Steps¶
- Build and deploy:
# Build images
docker-compose -f docker-compose.prod.yml build
# Start services
docker-compose -f docker-compose.prod.yml up -d
# Run migrations
docker-compose -f docker-compose.prod.yml exec web python manage.py migrate
- Set up monitoring:
# Install Prometheus and Grafana
helm install prometheus prometheus-community/prometheus
helm install grafana grafana/grafana
- Configure backups:
# Set up database backups
chmod +x /etc/cron.daily/backup-nsgg-db
# Set up media backups
aws s3 sync /path/to/media s3://your-bucket/media/
Security Checklist¶
- SSL/TLS Configuration:
- Enable SSL
- Configure secure ciphers
-
Enable HSTS
-
Firewall Rules:
- Allow ports 80, 443
- Restrict SSH access
-
Configure rate limiting
-
Database Security:
- Regular backups
- Encrypted connections
-
Strong passwords
-
Application Security:
- Enable CSRF protection
- Set secure cookie flags
- Configure CORS properly
Monitoring¶
- Set up Prometheus metrics:
# settings.py
INSTALLED_APPS += ['django_prometheus']
MIDDLEWARE = ['django_prometheus.middleware.PrometheusBeforeMiddleware'] + MIDDLEWARE
MIDDLEWARE += ['django_prometheus.middleware.PrometheusAfterMiddleware']
- Configure Grafana dashboards:
- Request latency
- Error rates
- Database performance
- Cache hit rates
Maintenance¶
-
Database maintenance:
-
Log rotation:
Troubleshooting¶
-
Check logs:
-
Monitor resources:
-
Check database connections:
Scaling¶
- Horizontal scaling:
- Add web servers
- Configure load balancer
-
Scale Redis cluster
-
Vertical scaling:
- Increase resources
- Optimize queries
- Implement caching
Rollback Procedure¶
-
Tag releases:
-
Rollback steps: