Setting up a Flask production server involves using a dedicated Web Server Gateway Interface (WSGI) server instead of the built-in development server. The built-in server is not designed to handle the scale, security, and performance requirements of a production environment.
- Choose a WSGI Server A WSGI server is a piece of software that can run a Python application. It acts as an intermediary between your web server (like Nginx) and your Flask application. Popular choices include:
- Gunicorn: A widely used, easy-to-configure, and highly performant WSGI server for UNIX-like systems. It's often the top choice for its simplicity and reliability.
- Waitress: A pure-Python WSGI server that's great for Windows environments where Gunicorn isn't natively supported. It's also simple to set up.
- uWSGI: A very powerful and feature-rich WSGI server that is highly configurable but can have a steeper learning curve.
- Prepare Your Flask Application Ensure your Flask application is ready for production.
- Disable Debug Mode: Set app.run(debug=False). Keeping debug mode on in production is a major security risk as it can expose sensitive information and allow arbitrary code execution.
- Use an init.py or wsgi.py file: Create a simple file that holds your Flask application instance. This is the file your WSGI server will reference.
wsgi.py
from my_app import create_app
Create the Flask application instance
app = create_app()
- Deploy with Gunicorn & Nginx
This is a common and robust production setup for Linux.
Step 1: Install Gunicorn
Install Gunicorn in your project's virtual environment.
pip install gunicorn
Step 2: Test Your Application with Gunicorn
Run Gunicorn from the command line to make sure it can serve your application.
gunicorn --bind 0.0.0.0:5000 wsgi:app
--bind 0.0.0.0:5000tells Gunicorn to listen on port 5000 from any IP address.wsgi:appspecifies the module (wsgi.py) and the application instance (app) to run. Step 3: Configure a System Service (systemd) To ensure your application automatically starts on boot and restarts if it crashes, create a systemd service file.- Create the file: sudo nano /etc/systemd/system/my_app.service
- Add the following configuration, adjusting paths and names as needed:
[Unit]
sh Description=Gunicorn instance to serve my_app After=network.target
[Service]
User=your_username
Group=www-data
WorkingDirectory=/path/to/your/project
ExecStart=/path/to/your/venv/bin/gunicorn --workers 3 --bind unix:my_app.sock -m 007 wsgi:app
[Install]
WantedBy=multi-user.target
- Enable and start the service:
sh sudo systemctl daemon-reload sudo systemctl start my_app sudo systemctl enable my_app
Step 4: Configure Nginx as a Reverse Proxy
Nginx will handle incoming requests, serve static files, and forward dynamic requests to your Gunicorn server via the Unix socket (my_app.sock).
Create a new Nginx configuration file:
sudo nano /etc/nginx/sites-available/my_appAdd the following configuration:
sh server { listen 80; server_name your_domain.com www.your_domain.com; location / { include proxy_params; proxy_pass http://unix:/path/to/your/project/my_app.sock; } }Optional: serve static files directly through Nginx
sh location /static { alias /path/to/your/project/my_app/static; }Create a symbolic link to enable the site:
sh sudo ln -s /etc/nginx/sites-available/my_app /etc/nginx/sites-enabled sudo nginx -t sudo systemctl restart nginx
- Other Production Considerations
- Environment Variables: Use environment variables for sensitive data like database credentials (
os.environ.get('DATABASE_URL')) instead of hardcoding them. - Static Files: For efficiency, it's best to configure Nginx or another web server to serve static files (CSS, JS, images) directly, bypassing the Flask application entirely.
- Security: Use HTTPS (SSL/TLS) for all traffic. Let's Encrypt provides free SSL certificates.
- Monitoring & Logging: Set up a logging system to monitor errors and application performance.
Comments
We accept only respectful comments. Any one who abuse this directive will be blocked from sending further comments. Read our comment policies for more information