Deploy Python Web Apps with Nginx, Gunicorn, and Supervisor – A Complete Guide
This tutorial walks through setting up a Python web project, creating a virtual environment, installing Flask, deploying with Gunicorn, managing processes via Supervisor, and configuring Nginx as a reverse proxy, providing a full end‑to‑end deployment workflow.
Overview
Web development often uses Nginx as a front‑end proxy and a web service script for deployment. A common stack is nginx + webservice + script , where Nginx can act as both forward and reverse proxy.
Proxy Basics
Forward proxy: client → proxy server → target server. Reverse proxy: client → proxy server → internal server. Nginx supports both modes.
Deployment Stack
We use Gunicorn as the WSGI container, Flask as the Python web framework, and Supervisor to manage processes, resulting in the stack:
nginx + gunicorn + flask + supervisorCreate a Project
Create a Python Virtual Environment
Use virtualenv to isolate Python packages. Example path: /home/rsj217/rsj217. After creating the venv, activate it.
Install Flask
Flask is a lightweight Python web micro‑framework that depends on werkzeug and jinja2. Install with pip install flask.
Test the installation by writing a simple Flask app and running it.
Install Gunicorn
Gunicorn serves as the production‑grade WSGI server. Install with pip install gunicorn and record the dependency in a requirements.txt file using pip freeze > requirements.txt.
Run the app with Gunicorn, e.g., gunicorn -w 4 -b 0.0.0.0:8000 myapp:app. The -w flag sets worker count, -b sets the bind address.
Manage Processes with Supervisor
Install Supervisor ( pip install supervisor) and create a supervisor.conf for the Gunicorn process (example path: /home/rsj217/rsj217/).
Basic Supervisor commands are shown below:
Supervisor also provides a web UI (enabled via configuration). Access it at http://127.0.0.1:9001 and view the Gunicorn‑served app at http://127.0.0.1:2170.
Install and Configure Nginx
Install via sudo apt-get install nginx. Binary resides in /usr/sbin, configuration in /etc/nginx. Use Supervisor to manage Nginx, remembering that Nginx runs as root, so Supervisor must also run as root.
Automation (Optional)
For batch deployments, tools like Fabric can automate copying code to remote servers. The project repository includes a Fabric script; adjust username and secret before use.
Project source: https://coding.net/u/rsj217/p/myproject/git
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
