YouTip LogoYouTip

Fastapi Blog Deploy

This chapter will teach you production environment configuration for FastAPI, and deploying your blog to Railway using Railway. * * * ## Production Environment Configuration Management (venv) $ pip install pydantic-settings python-dotenv ## Example # File path: config.py from pydantic_settings import BaseSettings class Settings(BaseSettings): """Application configuration (automatically reads from .env file)""" app_name: str="TUTORIAL Blog" debug: bool=False secret_key: str="dev-secret-key-change-me" database_url: str="sqlite:///./blog.db" access_token_expire_minutes: int=60 * 24 * 7# 7 days class Config: env_file =".env"# Specify environment variables file settings = Settings() Create `.env` file: SECRET_KEY=a1b2c3d4...your-real-secret-key DATABASE_URL=sqlite:///./blog.db DEBUG=False * * * ## Generate Requirements File (venv) $ pip freeze > requirements.txt Ensure core dependencies are included: fastapi, uvicorn, sqlalchemy, jinja2, alembic, passlib, python-jose, python-multipart, sqladmin, pydantic-settings. * * * ## Configure Procfile and Startup Command FastAPI uses ASGI server, different from Flask/Django's WSGI: web: uvicorn main:app --host 0.0.0.0 --port $PORT If multiple workers are needed (recommended for production): web: gunicorn -k uvicorn.workers.UvicornWorker -w 4 main:app > WSGI vs ASGI: Django/Flask uses Gunicorn (WSGI server), FastAPI uses Uvicorn (ASGI server). Gunicorn itself is WSGI, but can run ASGI applications through `uvicorn.workers.UvicornWorker`. * * * ## Push to GitHub $ git init $ echo "venv/" > .gitignore $ echo "__pycache__/" >> .gitignore $ echo "*.pyc" >> .gitignore $ echo ".env" >> .gitignore $ git add . $ git commit -m "Initialize FastAPI blog project" $ git branch -M main $ git remote add origin https://github.com/your-username/fastapi-blog.git $ git push -u origin main * * * ## Railway Deployment 1. Visit [railway.app](https://railway.app/), log in with GitHub 2. New Project β†’ Deploy from GitHub repo β†’ Select fastapi-blog 3. Railway automatically detects Procfile 4. Set environment variables in Variables 5. Click Deploy ### Environment Variables Configuration | Variable Name | Value | Description | | --- | --- | --- | | SECRET_KEY | Randomly generated long string | JWT signing key | | DATABASE_URL | Injected by Railway | Production database | After successful deployment, you will get a link. Visiting /docs shows the Swagger documentation, and visiting /admin enters the admin panel. * * * ## Framework Comparison | Feature | FastAPI | Django | Flask | | --- | --- | --- | --- | | ORM | SQLAlchemy | Django ORM (built-in) | Flask-SQLAlchemy | | Data Validation | Pydantic | Django Forms | Flask-WTF | | Route Organization | APIRouter | urls.py | Blueprint | | User Authentication | JWT + OAuth2 | Session Auth (built-in) | Flask-Login (Session) | | Database Migration | Alembic | makemigrations / migrate | Flask-Migrate | | Admin Panel | sqladmin | Django Admin | Flask-Admin | | API Documentation | /docs (automatic) | Requires drf-spectacular | Requires Flasgger | | Server | Uvicorn (ASGI) | Gunicorn (WSGI) | Gunicorn (WSGI) | * * * ## Next Learning Directions | Learning Direction | Suitable For | Recommended Starting Point | | --- | --- | --- | | Async SQLAlchemy | Need higher concurrent performance | Switch synchronous Session to AsyncSession | | FastAPI + Vue3/React Frontend-Backend Separation | SPA + API architecture | Replace Jinja2 templates with Vue3/React, use FastAPI purely as API backend | | WebSocket | Need real-time push (chat, notifications) | FastAPI natively supports WebSocket: @app.websocket() | | PostgreSQL | Production-grade database | Replace DATABASE_URL, install asyncpg or psycopg2 |
← Skills ParamsFastapi Blog Jwt Auth β†’