Quiz 2
Registry Synced

Deployment — Taking Your App Live

480 words
2 min read

Reading compass

Now · 🎯 Learning Objectives

Deployment — Taking Your App Live

🎯 Learning Objectives

  • Configure Flask for production
  • Use WSGI servers (Gunicorn, Waitress)
  • Manage environment variables
  • Understand deployment options

1. Production vs Development

python
# config.py — separate configuration
import os
class Config:
    SECRET_KEY = os.environ.get('SECRET_KEY') or 'dev-key'
    SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL') or 'sqlite:///app.db'
    DEBUG = False
class DevelopmentConfig(Config):
    DEBUG = True
class ProductionConfig(Config):
    # Production settings
    SESSION_COOKIE_SECURE = True
    SESSION_COOKIE_HTTPONLY = True

2. WSGI Servers

Flask's built-in server is for development only. For production, use a WSGI server:
bash
# Install Gunicorn (Linux/Mac)
pip install gunicorn
# Run with Gunicorn
gunicorn -w 4 -b 0.0.0.0:8000 app:app
# -w 4: 4 worker processes
# -b: bind to address:port
# app:app: module:application
# Run with Waitress (Windows compatible)
pip install waitress
waitress-serve --port=8000 app:app

3. Environment Variables

python
# Store secrets in environment, not in code:
import os
SECRET_KEY = os.environ.get('SECRET_KEY')
DATABASE_URL = os.environ.get('DATABASE_URL')
FLASK_ENV = os.environ.get('FLASK_ENV', 'production')
bash
# .env file (don't commit to git)
SECRET_KEY=your-secret-key-here
DATABASE_URL=postgresql://user:pass@localhost/dbname
FLASK_ENV=production

4. Practice Questions

Q1: Why shouldn't you use Flask's built-in server in production?
Answer: Flask's dev server is single-threaded, can't handle concurrent requests, and has security vulnerabilities. Production WSGI servers (Gunicorn, Waitress) handle multiple workers, load balancing, and are security-hardened. Q2: What are environment variables used for?
Answer: Store configuration that varies between environments (development, testing, production) and sensitive data (API keys, database passwords, secret keys). They keep secrets out of the codebase. Q3: What does gunicorn -w 4 app:app mean?
Answer: -w 4 = 4 worker processes (can handle 4 requests concurrently). app:app = module app.py, Flask application instance named app. Gunicorn needs to know both the module and the app variable name. Q4: How do you handle database migrations in production?
Answer: Use Flask-Migrate (Alembic wrapper). Create migration scripts, test them, then apply to production database. Always backup before migrating. Q5: What should be in your .gitignore file?
Answer: .env, *.pyc, __pycache__/, instance/, *.db, .venv/, venv/. Never commit secrets, compiled files, or the database file. Q6: How do you set Flask to production mode?
Answer: export FLASK_ENV=production or set app.config['ENV'] = 'production'. This disables debug mode and the interactive debugger. Q7: What is a requirements.txt file?
Answer: Lists all Python dependencies. Created with pip freeze > requirements.txt. Used to install dependencies on the server: pip install -r requirements.txt. Q8: What deployment platforms support Flask?
Answer: PythonAnywhere (beginner-friendly), Render, Railway, Railway, Heroku (requires container), AWS Elastic Beanstalk, Google App Engine, DigitalOcean, or any VPS with Gunicorn + Nginx.

📐 Key Concepts

AspectDevelopmentProduction
Serverapp.run()Gunicorn/Waitress
Debugdebug=Truedebug=False
HTTPSNoYes (via reverse proxy)
Env vars.env fileSystem environment
Workers14+ (based on CPU cores)

🔗 Cross-References

Document outline

Keep your place and jump directly to a heading.

Table of Contents
System Normal // Awaiting Context

Intelligence Hub

Navigate the knowledge graph to generate context. The Hub adapts dynamically to surface backlinks, related notes, and metadata insights.