Build and Install
Install the build tool
When you want to deploy your application elsewhere, build a wheel (
.whl) file:Build the wheel file
dist/flaskr-1.0.0-py3-none-any.whl.The file name format is: {project name}-{version}-{python tag}-{abi tag}-{platform tag}Configure the Secret Key
In development, you used a default value forSECRET_KEY. This must be changed to a random value in production, or attackers could use the public 'dev' key to modify the session cookie.
Run with a Production Server
Use a production WSGI server instead of the development server. Here’s an example using Waitress:Other WSGI Servers
Waitress is just one example. Other popular options include:-
Gunicorn (Linux/Unix)
-
uWSGI (Linux/Unix)
- mod_wsgi (Apache) Works with Apache HTTP Server
Deployment Options
There are many ways to host your Flask application:Traditional Hosting
-
Virtual Private Server (VPS)
- DigitalOcean, Linode, AWS EC2
- Full control over the server
- Manually configure web server, WSGI server, database
-
Platform as a Service (PaaS)
- Heroku, Railway, Render
- Simplified deployment
- Automatic scaling and management
-
Container Platforms
- Docker + Docker Compose
- Kubernetes
- Consistent environment across development and production
Reverse Proxy Setup
In production, typically run your WSGI server behind a reverse proxy like Nginx or Apache:- Serve static files efficiently
- Handle SSL/TLS termination
- Load balancing
- Security features
Production Checklist
Security
- Set a strong, random
SECRET_KEY - Use HTTPS (SSL/TLS certificate)
- Enable security headers
- Validate and sanitize all user input
- Keep dependencies updated
Database
- Use a production database (PostgreSQL, MySQL) instead of SQLite for high-traffic sites
- Set up database backups
- Use connection pooling
- Enable database migrations (e.g., Flask-Migrate)
Performance
- Enable caching (Redis, Memcached)
- Use a CDN for static assets
- Configure logging
- Set up monitoring (Sentry, New Relic)
Environment Variables
Instead of usingconfig.py, you can use environment variables:
flaskr/__init__.py
Systemd Service (Linux)
Create a systemd service to manage your application:/etc/systemd/system/flaskr.service
Next Steps
Congratulations! You’ve completed the Flask tutorial. You now have:- A working blog application
- Understanding of Flask’s core concepts
- Experience with blueprints, templates, and databases
- A tested and deployable application
Continue Learning
- Explore Flask extensions to add more features
- Read the Flask documentation for advanced topics
- Build your own projects to practice
- Join the Flask community
