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

Initial Setup

1. Create the Fly App

cd backend
fly launch --no-deploy

This 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

fly postgres create --name your-app-db
fly postgres attach your-app-db

This 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 create

Copy 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 deploy

Run 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 2

Vertical Scaling

fly scale vm shared-cpu-2x

Monitoring

View Logs

fly logs

Open Console

fly ssh console

Check Status

fly status

Celery 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=1

Custom Domain

fly certs create your-domain.com

Then add a CNAME record pointing to your-app-name.fly.dev.

Troubleshooting

Check Application Health

fly checks list

View Recent Deployments

fly releases

Rollback

fly deploy --image registry.fly.io/your-app:v123

Next Steps

Last updated on

On this page