Configure Django
The official How to Deploy Django guide and Django Deployment Checklist are a good first step to ensure your app is ready for deployment. The third-party package WhiteNoise is recommended for serving static files in production as this requires additional configuration. A production web server must be installed, typically either Gunicorn or uWSGI. The database adapter Psycopg is commonly used along with dj-database-url to establish a Django database connection via aDATABASE_URL environment variable.
Be sure to have generated an up-to-date requirements.txt file for any new packages added for deployment.
flyctl
Fly.io has its own command-line utility for managing apps, flyctl. If not already installed, follow the instructions on the installation guide and log in to Fly.Provision Django and Postgres Servers
To configure and launch the app, use the commandfly launch and follow the wizard. You can set a name for the app, choose a default region, launch and attach a Postgres database. You can also set up a Redis database though we will not be doing so in this example.
Dockerfile and fly.toml
Thefly launch command creates two new files in the project that are automatically configured: Dockerfile and fly.toml.
The Dockerfile is essentially instructions for creating an image.
The fly.toml file is used by Fly.io to configure applications for deployment. Configuration of builds, environment variables, internet-exposed services, disk mounts and release commands go here.
ALLOWED_HOSTS & CSRF_TRUSTED_ORIGINS
The dedicated URL for your deployment will be<app_name>.fly.dev. Update the ALLOWED_HOSTS and CSRF_TRUSTED_ORIGINS configurations with your <app_name> to include it.
Deploy Your Application
To deploy the application use the following command:Troubleshooting your initial deployment
Since this is an existing Django app, it is highly likely it might not boot because you probably need to configure secrets or other service dependencies. Let’s walk through how to troubleshoot these issues so you can get your app running.View Log Files
If your application didn’t boot on the first deploy, runfly logs to see
what’s going on.
Console
To SSH into your hosted Django application use the commandfly ssh console. For example, to execute Django’s createsuperuser command to log into the admin do the following:
The
--pty flag tells the SSH server to run the command in a pseudo-terminal, which createsuperuser requires.Secrets
Secrets allow sensitive values, such as credentials and API keys, to be securely passed to your Django applications. You can set, remove, or list all secrets with the fly secrets command.Git
Deployments are initiated via thefly deploy command—git isn’t needed to deploy to Fly.io. The advantage of this approach is that your git history will be clean and not full of git push commits such as occurs on other hosting platforms.
This also means that .gitignore files are not ignored. If you have secrets or other sensitive information in your git history, it is recommended to create a .dockerignore file and add the git repo there.
Databases
Fly.io has a Postgres offering to automate provisioning, maintenance, and snapshot tasks for your Postgres database, but it does not manage it. If you run out of disk space, RAM, or other resources on your Fly Postgres instances, you’ll have to scale those virtual machines from the Fly CLI. If you prefer, you can instead connect to an external fully-managed Postgres database.Custom Domain & SSL Certificates
After you finish deploying your application to Fly.io and have tested it extensively, read through the Custom Domain docs and point your domain at Fly. In addition to supportingCNAME DNS records, Fly.io also supports A and AAAA records for those who want to point example.com (without the www.example.com) directly at Fly.
flyctl Commands
The flyctl CLI docs have an extensive inventory offly commands. Here are a few common commands especially if you are coming from another hosting service like Heroku.