How to Use Flask in Production: Best Practices and Setup
Flask in production, run your app with a production-ready WSGI server like Gunicorn or uWSGI behind a reverse proxy such as Nginx. Avoid using Flask's built-in server as it is not designed for production use.Syntax
In production, you do not run Flask's built-in server. Instead, you use a WSGI server to serve your Flask app. The typical command to run Flask with Gunicorn looks like this:
gunicorn -w 4 -b 0.0.0.0:8000 myapp:app
Here, -w 4 means 4 worker processes, -b 0.0.0.0:8000 binds to all IPs on port 8000, and myapp:app points to your Flask app object.
gunicorn -w 4 -b 0.0.0.0:8000 myapp:app
Example
This example shows a simple Flask app and how to run it with Gunicorn for production. It demonstrates the app structure and the command to start the server.
from flask import Flask app = Flask(__name__) @app.route('/') def hello(): return 'Hello, Production Flask!' if __name__ == '__main__': app.run() # Only for development, not production # To run in production, use the command: # gunicorn -w 4 -b 0.0.0.0:8000 myapp:app
Common Pitfalls
Using Flask's built-in server in production: Flask's built-in server is single-threaded and not designed for handling real traffic or security.
Not using a reverse proxy: Without a reverse proxy like Nginx, you miss out on SSL termination, load balancing, and better static file handling.
Ignoring environment variables: Always set FLASK_ENV=production and configure secret keys securely.
## Wrong way (development server in production): from flask import Flask app = Flask(__name__) @app.route('/') def hello(): return 'Hello World' if __name__ == '__main__': app.run(host='0.0.0.0', port=8000) # Not for production ## Right way (run with Gunicorn): # gunicorn -w 4 -b 0.0.0.0:8000 myapp:app
Quick Reference
- Use
GunicornoruWSGIas WSGI servers. - Place a reverse proxy like
Nginxin front for SSL and load balancing. - Set environment variables:
FLASK_ENV=production,SECRET_KEY. - Do not use Flask's built-in server in production.
- Use multiple worker processes for handling concurrent requests.