Deploying Python Flask Apps with Nginx, Gunicorn, and Supervisor

Learn how to set up a Python Flask web service on Linux by creating a virtual environment, installing Flask, configuring Nginx as a reverse proxy, deploying with Gunicorn as the WSGI server, and managing processes using Supervisor, complete with command examples.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Deploying Python Flask Apps with Nginx, Gunicorn, and Supervisor

Python Web Deployment

In web development, many languages compete, but deployment methods are limited. Typically, Nginx acts as a front‑end proxy, forwarding requests to a web service script. Common stack: Nginx + web service + script.

Nginx

Nginx is a high‑performance web server often used as a reverse proxy. A forward proxy sends client requests to the internet via the proxy; a reverse proxy receives external requests and forwards them to internal servers.

Deployment Stack: Nginx + Gunicorn + Flask + Supervisor

We use Gunicorn as the WSGI container, Flask as the Python web framework, and Supervisor to manage processes. The final deployment architecture is: Nginx + Gunicorn + Flask ++ Supervisor.
Create a project
mkdir myproject
Create a Python virtual environment
# Install virtualenv and create an isolated environment
virtualenv venv
cd myproject
virtualenv venv
source venv/bin/activate
Install Flask
pip install flask
Test the Flask installation with a simple app
from flask import Flask
app = Flask(__name__)

@app.route('/')
def index():
    return 'hello world'

if __name__ == '__main__':
    app.run(debug=True)
Run the app
python myapp.py
# Access http://127.0.0.1:5000 to see “hello world”.

Deploy with Gunicorn

# Install Gunicorn
pip install gunicorn

# Freeze dependencies
pip freeze > requirements.txt

# Start the app with Gunicorn (4 workers, listening on port 8000)
gunicorn -w 4 -b 0.0.0.0:8000 myapp:app
Install Supervisor
pip install supervisor
echo_supervisord_conf > supervisor.conf
vim supervisor.conf   # Add Gunicorn process configuration
[program:myapp]
command=/home/rsj217/rsj217/myproject/venv/bin/gunicorn -w4 -b0.0.0.0:8000 myapp:app
directory=/home/rsj217/rsj217/myproject
startsecs=0
stopwaitsecs=0
autostart=false
autorestart=false
stdout_logfile=/home/rsj217/rsj217/myproject/log/gunicorn.log
stderr_logfile=/home/rsj217/rsj217/myproject/log/gunicorn.err
Supervisor basic commands
supervisord -c supervisor.conf          # start Supervisor
supervisorctl -c supervisor.conf status # check status
supervisorctl -c supervisor.conf reload # reload config
supervisorctl -c supervisor.conf start [all|appname]
supervisorctl -c supervisor.conf stop [all|appname]
Enable Supervisor web interface
[inet_http_server]
port=127.0.0.1:9001
username=user
password=123

[supervisorctl]
serverurl=unix:///tmp/supervisor.sock
serverurl=http://127.0.0.1:9001
username=user
password=123
Start Gunicorn via Supervisor
supervisord -c supervisor.conf
Access the web interfaces
http://127.0.0.1:9001   # Supervisor web UI
http://127.0.0.1:8000   # Gunicorn‑served “hello world”

Install and Configure Nginx

# Install Nginx
sudo apt-get install nginx

# Nginx binary: /usr/sbin/nginx
# Configuration directory: /etc/nginx

# Manage Nginx with Supervisor
[program:nginx]
command=/usr/sbin/nginx
startsecs=0
stopwaitsecs=0
autostart=false
autorestart=false
stdout_logfile=/home/rsj217/rsj217/myproject/log/nginx.log
stderr_logfile=/home/rsj217/rsj217/myproject/log/nginx.err

Further automation can be achieved with Fabric (configuration not shown).

Project source: https://coding.net/rsj217/myproject.git

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

DeploymentFlaskSupervisorGunicorn
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.