Flask is a lightweight and powerful framework for building web applications, but it is not production-ready on its own. For production deployments, using a WSGI server like Gunicorn is an excellent choice. Gunicorn is reliable, scalable, and works seamlessly with Flask.
In this blog post, we’ll walk through setting up Gunicorn to serve your Flask application efficiently.
1. Install Gunicorn
First, ensure Gunicorn is installed in your Python environment. If you don’t have it yet, you can install it using pip:
Copied!pip install gunicorn
2. Flask Application Structure
Let’s assume you have the following Flask app structure:
Copied!my_app/ │ ├── app.py └── requirements.txt
Here’s a basic example of what app.py
might look like:
Copied!from flask import Flask app = Flask(__name__) @app.route('/') def home(): return "Hello, World!" if __name__ == "__main__": app.run()
3. Running the Application with Gunicorn
Gunicorn serves your application using the WSGI interface. To run your Flask app with Gunicorn, navigate to the directory containing app.py
and execute:
Copied!gunicorn -w 4 -b 0.0.0.0:8000 app:app
Here’s what each option does:
-
-w 4
: Starts 4 worker processes (adjust based on your server’s CPU cores). -
-b 0.0.0.0:8000
: Binds the application to all network interfaces on port 8000. -
app:app
: Specifies the entry point for Gunicorn. The format isMODULE_NAME:APP_VARIABLE_NAME
.
4. Making Gunicorn Production-Ready
Configuring Gunicorn
Instead of passing options via the command line, you can create a Gunicorn configuration file (gunicorn.conf.py
) for better manageability. Example:
Copied!# gunicorn.conf.py bind = "0.0.0.0:8000" workers = 4 loglevel = "info" errorlog = "/var/log/gunicorn/error.log" accesslog = "/var/log/gunicorn/access.log"
Run Gunicorn with the configuration:
Copied!gunicorn -c gunicorn.conf.py app:app
Running Gunicorn as a Service
To ensure your app starts automatically on server boot, set up a systemd service for Gunicorn:
- Create the Service File
Create/etc/systemd/system/my_app.service
:
Copied![Unit] Description=Gunicorn instance to serve Flask application After=network.target [Service] User=your_user Group=your_group WorkingDirectory=/path/to/your/app ExecStart=/path/to/venv/bin/gunicorn -c /path/to/gunicorn.conf.py app:app [Install] WantedBy=multi-user.target
2. Start and Enable the Service
Copied!sudo systemctl start my_app sudo systemctl enable my_app
5. Integrating with Nginx
While Gunicorn can serve requests directly, pairing it with a reverse proxy like Nginx improves performance and adds an extra layer of security. A typical Nginx configuration might look like this:
Copied!server { listen 80; server_name yourdomain.com; location / { proxy_pass http://127.0.0.1:8000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
After editing the configuration, reload Nginx:
Copied!sudo systemctl reload nginx
6. Testing Your Deployment
Visit your server’s IP address or domain (e.g., http://yourdomain.com
). If everything is set up correctly, your Flask application should be live and accessible.
Our recently developed Scalable Data Ingestion is an excellent example to try this yourself.
Conclusion
Using Gunicorn as the WSGI server for your Flask application is a reliable and efficient way to handle production traffic. Pairing it with Nginx ensures better performance and added security. With this setup, you can confidently deploy and scale your Flask applications.