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

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

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

  1. Go to railway.app and create an account
  2. Click New Project
  3. Choose Deploy from GitHub repo
  4. Select your plateforme repo
  5. Railway automatically detects it's a Python project

Step 8 — Add PostgreSQL

  1. In the Railway project, click NewDatabasePostgreSQL
  2. Railway automatically creates the DATABASE_URL variable 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

  1. Railway automatically triggers a build on every push
  2. Follow the logs in Railway → your service → Deployments tab
  3. 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