Neural Sync Active
Deployment — Taking Your App Live
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 doesgunicorn -w 4 app:appmean?Answer:-w 4= 4 worker processes (can handle 4 requests concurrently).app:app= moduleapp.py, Flask application instance namedapp. 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=productionor setapp.config['ENV'] = 'production'. This disables debug mode and the interactive debugger. Q7: What is a requirements.txt file?Answer: Lists all Python dependencies. Created withpip 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
| Aspect | Development | Production |
|---|---|---|
| Server | app.run() | Gunicorn/Waitress |
| Debug | debug=True | debug=False |
| HTTPS | No | Yes (via reverse proxy) |
| Env vars | .env file | System environment |
| Workers | 1 | 4+ (based on CPU cores) |