Deploying Django on Railway
March 20, 2026, 6:22 p.m.
Step 1 — Prepare settings.py
Move all sensitive values to .env and read them from the environment.
import os
from dotenv import load_dotenv
load_dotenv()
SECRET_KEY = os.environ.get('SECRET_KEY')
DEBUG = os.environ.get('DEBUG', 'False') == 'True'
ALLOWED_HOSTS = os.environ.get('ALLOWED_HOSTS', '').split(',')
Add PostgreSQL config that reads the Railway URL:
import dj_database_url
DATABASES = {
'default': dj_database_url.config(
default=os.environ.get('DATABASE_URL'),
conn_max_age=600,
)
}
Add static files config:
STATIC_URL = '/static/'
STATIC_ROOT = BASE_DIR / 'staticfiles'
Step 2 — Install required dependencies
pip install gunicorn dj-database-url psycopg2-binary whitenoise
pip freeze > requirements.txt
gunicorn: the WSGI server for production (replaces Django's dev server)dj-database-url: parses the PostgreSQL URL provided by Railwaypsycopg2-binary: the PostgreSQL driver for Pythonwhitenoise: serves static files directly from Django without Nginx
Step 3 — Configure WhiteNoise for static files
In settings.py, add WhiteNoise to the middleware list:
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware', # right after SecurityMiddleware
...
]
Step 4 — Create the Procfile
At the root of the project, create a file named Procfile (no extension):
web: gunicorn blog.wsgi:application --bind 0.0.0.0:$PORT
release: python manage.py migrate && python manage.py collectstatic --noinput
web: the command that starts the serverrelease: runs automatically on every deployment — applies migrations and collects static files
Step 5 — Check .gitignore
Make sure these files are ignored:
.env
*.pyc
__pycache__/
db.sqlite3
staticfiles/
Step 6 — Push the code to GitHub
git add .
git commit -m "prepare for railway deployment"
git push origin main
Step 7 — Create the project on Railway
- Go to railway.app and create an account
- Click New Project
- Choose Deploy from GitHub repo
- Select your
plateformerepo - Railway automatically detects it's a Python project
Step 8 — Add PostgreSQL
- In the Railway project, click New → Database → PostgreSQL
- Railway automatically creates the
DATABASE_URLvariable and makes it available to your app
Step 9 — Set environment variables
In Railway → your service → Variables tab, add:
SECRET_KEY=your_secret_key
DEBUG=False
ALLOWED_HOSTS=your-project.up.railway.app
RESEND_API_KEY=your_resend_key
DATABASE_URL is already injected automatically by Railway via the PostgreSQL service.
Step 10 — Verify the deployment
- Railway automatically triggers a build on every push
- Follow the logs in Railway → your service → Deployments tab
- Once deployed, click the generated URL (format
xxx.up.railway.app)
Summary of modified/created files
| File | Action |
|---|---|
settings.py |
SECRET_KEY, DEBUG, ALLOWED_HOSTS, DATABASE_URL, WhiteNoise |
requirements.txt |
gunicorn, dj-database-url, psycopg2-binary, whitenoise |
Procfile |
web and release commands |
.env |
SECRET_KEY + local variables |
.gitignore |
make sure .env and db.sqlite3 are ignored |