Deploy Backend to Fly.io
Step-by-step guide to deploying the Django backend to Fly.io
Deploy Backend to Fly.io
Fly.io is the recommended platform for deploying the Django backend. It offers global edge deployment, automatic SSL, and PostgreSQL hosting.
Prerequisites
- Fly.io account
- Fly CLI installed (
brew install flyctlor other methods) - Authenticated with
fly auth login
Initial Setup
1. Create the Fly App
cd backend
fly launch --no-deployThis creates a fly.toml configuration file. Choose your preferred region when prompted.
2. Configure fly.toml
app = "your-app-name"
primary_region = "iad"
[build]
dockerfile = "Dockerfile"
[env]
DJANGO_SETTINGS_MODULE = "config.settings"
PYTHONUNBUFFERED = "1"
[http_service]
internal_port = 8080
force_https = true
auto_stop_machines = true
auto_start_machines = true
min_machines_running = 1
[[services]]
internal_port = 8080
protocol = "tcp"
[[services.ports]]
handlers = ["http"]
port = 80
[[services.ports]]
handlers = ["tls", "http"]
port = 443
[checks]
[checks.health]
grace_period = "30s"
interval = "15s"
method = "get"
path = "/api/health/"
port = 8080
timeout = "10s"
type = "http"Database Setup
Option 1: Fly Postgres (Recommended)
fly postgres create --name your-app-db
fly postgres attach your-app-dbThis automatically sets DATABASE_URL in your app secrets.
Option 2: External Database (Neon/Supabase)
fly secrets set DATABASE_URL="postgres://user:pass@host:5432/db?sslmode=require"Redis Setup
Fly Redis (Upstash)
fly redis createCopy the Redis URL and set it:
fly secrets set REDIS_URL="redis://..."Environment Secrets
Set all required environment variables:
fly secrets set \
SECRET_KEY="your-secret-key" \
ALLOWED_HOSTS="your-app-name.fly.dev" \
CORS_ALLOWED_ORIGINS="https://your-frontend.vercel.app" \
AWS_ACCESS_KEY_ID="your-key" \
AWS_SECRET_ACCESS_KEY="your-secret" \
AWS_STORAGE_BUCKET_NAME="your-bucket" \
RESEND_API_KEY="your-resend-key"Deployment
First Deploy
fly deployRun Migrations
fly ssh console -C "python manage.py migrate"Create Superuser
fly ssh console -C "python manage.py createsuperuser"Scaling
Horizontal Scaling
fly scale count 2Vertical Scaling
fly scale vm shared-cpu-2xMonitoring
View Logs
fly logsOpen Console
fly ssh consoleCheck Status
fly statusCelery Workers
For background tasks, create a separate process in fly.toml:
[processes]
web = "gunicorn config.wsgi:application --bind 0.0.0.0:8080"
worker = "celery -A config worker -l info"
beat = "celery -A config beat -l info"Scale workers independently:
fly scale count web=2 worker=1 beat=1Custom Domain
fly certs create your-domain.comThen add a CNAME record pointing to your-app-name.fly.dev.
Troubleshooting
Check Application Health
fly checks listView Recent Deployments
fly releasesRollback
fly deploy --image registry.fly.io/your-app:v123Next Steps
Last updated on