Railway is a cloud platform with a free tier. Deploy from GitHub in minutes.
Preparing the project
pip install gunicorn whitenoise dj-database-url
pip freeze > requirements.txt
# settings.py
import os
import dj_database_url
DEBUG = os.environ.get('DEBUG', 'False') == 'True'
SECRET_KEY = os.environ.get('SECRET_KEY')
ALLOWED_HOSTS = os.environ.get('ALLOWED_HOSTS', '').split(',')
# Database from environment variable
DATABASES = {
'default': dj_database_url.config(
default=os.environ.get('DATABASE_URL'),
conn_max_age=600,
)
}
# Static files via whitenoise
MIDDLEWARE = [
'whitenoise.middleware.WhiteNoiseMiddleware',
...
]
STATIC_ROOT = BASE_DIR / 'staticfiles'
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
Procfile
Create a Procfile in the project root:
web: gunicorn mysite.wsgi --workers 2 --bind 0.0.0.0:$PORT
release: python manage.py migrate
.gitignore
.env
*.pyc
__pycache__/
db.sqlite3
staticfiles/
media/
Deploying to Railway
- Push the project to GitHub
- Go to railway.app → New Project → Deploy from GitHub
- Select your repository
- Add PostgreSQL: New → Database → PostgreSQL
- In the service settings, add the following variables:
-SECRET_KEY— generate one:python -c "from django.core.management.utils import get_random_secret_key; print(get_random_secret_key())"
-DEBUG=False
-ALLOWED_HOSTS=your-app.railway.app
-DATABASE_URL— pulled in automatically from the PostgreSQL service
Post-deploy commands
# In Railway → Service → Shell (or via CLI)
python manage.py migrate
python manage.py collectstatic --no-input
python manage.py createsuperuser
💬 Comments (0)
No comments yet
Be the first to share your opinion about this article!