Deployment with Railway
Push to GitHub, deploy on Railway — your Python app live on the internet in 5 minutes.
Your app works on localhost. But localhost is invisible to the world. Deploying means your app runs 24/7 on a server in the cloud, reachable by anyone on the planet.
Railway deploys Python apps in 5 minutes from a GitHub repo. No Docker, no Kubernetes, no AWS configuration. The same platform used by thousands of Indian startups to get their Python backend live the same day.
📦 3 Things a Server Needs (5–25 min)
requirements.txt — Railway reads this to install your packages. Run pip freeze > requirements.txt
Procfile — tells Railway how to start your app: web: gunicorn myproject.wsgi
Environment Variables — SECRET_KEY, DATABASE_URL, DEBUG=False. Never commit secrets.
💻 Live Demo — Deploy in 5 Steps (25–45 min)
# Step 1: Production-ready settings pip install gunicorn dj-database-url django-environ whitenoise # settings.py changes for production import environ env = environ.Env() environ.Env.read_env() DEBUG = env.bool("DEBUG", default=False) SECRET_KEY = env("SECRET_KEY") ALLOWED_HOSTS = [".railway.app", "127.0.0.1"] # Static files (whitenoise serves CSS/JS without a CDN) MIDDLEWARE = [... "whitenoise.middleware.WhiteNoiseMiddleware" ...] STATIC_ROOT = BASE_DIR / "staticfiles" # Database from environment variable DATABASES = { "default": env.db() # reads DATABASE_URL env var }
# Step 2: Procfile (no extension) at project root web: gunicorn myproject.wsgi --log-file - # Step 3: runtime.txt (tell Railway which Python version) python-3.12.0 # Step 4: Collect static files python manage.py collectstatic --no-input # Step 5: Push to GitHub, then Railway git add . git commit -m "deploy: production settings" git push origin main # Railway: connect GitHub repo → New Service → Add env vars: # SECRET_KEY=your-long-secret-key-here # DEBUG=False # Railway auto-provides DATABASE_URL for PostgreSQL
Run migrations on production: in Railway dashboard, open Shell tab and run python manage.py migrate. Then python manage.py createsuperuser. Now your app is live, has a database, and has an admin user.
- Update your Django project: install gunicorn, whitenoise, django-environ
- Create
Procfileandruntime.txtin project root - Update settings.py for production (DEBUG=False, SECRET_KEY from env, whitenoise)
- Run
pip freeze > requirements.txt - Push to GitHub, create Railway account, connect repo
- Add environment variables in Railway dashboard, deploy
- Show: Your app running on a
.railway.appURL — share the link
📄 Day 13 Cheat Sheet — Django Deployment
Key Files
| requirements.txt | pip freeze > requirements.txt |
| Procfile | web: gunicorn app.wsgi |
| runtime.txt | python-3.12.0 |
| .env | Local env vars (never commit) |
| .gitignore | Add .env, __pycache__, venv |
Production Settings
| DEBUG = False | Never True in prod |
| ALLOWED_HOSTS = [domain] | Whitelist your domain |
| SECRET_KEY from env | Never hardcode |
| collectstatic | Bundle static files |
| HTTPS only | Railway handles SSL |
Railway Commands
| railway login | Authenticate CLI |
| railway init | Link project |
| railway up | Deploy current code |
| railway run python manage.py migrate | Run on prod |
| railway logs | Live logs |
Environment Variables
| SECRET_KEY | Django secret key |
| DEBUG | False |
| DATABASE_URL | Auto from Railway PostgreSQL |
| ALLOWED_HOSTS | .railway.app |
| CORS_ALLOWED_ORIGINS | Your frontend URL |