Comprehensive Guide to Deploying Python Django/Flask Applications on VPS with Gunicorn and Supervisor
A detailed guide on deploying Python Django or Flask applications on VPS using Gunicorn as WSGI server and Supervisor for process management, ensuring stable, auto-starting, and...
Introduction
Deploying Python Django or Flask applications to a VPS is a crucial step to move your app from development to production. To ensure stable operation, automatic startup, and recovery from failures, you should use tools like Gunicorn (a WSGI server) and Supervisor (a process manager). This guide walks you through configuring and deploying your Python application on a VPS with Gunicorn and Supervisor.
Prerequisites
- VPS running Ubuntu 20.04 or 22.04
- Python 3.8 or higher
- Ready Django or Flask application (a simple template works)
- Root or sudo-enabled user access
- SSH client to connect to the VPS
Install and Configure Gunicorn
Gunicorn is a popular WSGI server for Python applications. It efficiently handles multiple concurrent requests and improves application stability.
Step 1: Install Gunicorn
Run the following command on your VPS to install Gunicorn:
pip install gunicornStep 2: Run the Application with Gunicorn
Assuming you have a Django project with a manage.py file in the root directory, start it with:
gunicorn --bind 0.0.0.0:8000 myproject.wsgi:applicationReplace myproject.wsgi:application with your actual WSGI module.
Configure Supervisor for Process Management
Supervisor manages processes to ensure your application stays running. If the app crashes, Supervisor restarts it automatically.
Step 1: Install Supervisor
sudo apt update\nsudo apt install supervisor -yStep 2: Create Supervisor Configuration File
Create a configuration file in /etc/supervisor/conf.d/:
sudo nano /etc/supervisor/conf.d/django-app.confAdd the following content (adjust application name and paths as needed):
[program:django-app]\ncommand=/home/ubuntu/myproject/venv/bin/gunicorn --bind 0.0.0.0:8000 myproject.wsgi:application\ndirectory=/home/ubuntu/myproject\nuser=ubuntu\nautostart=true\nautorestart=true\nredirect_stderr=true\nstdout_logfile=/home/ubuntu/myproject/logs/gunicorn.logStep 3: Start Supervisor
sudo supervisorctl reread\nsudo supervisorctl update\nsudo supervisorctl start django-appVerify and Test Functionality
- Check process status:
sudo supervisorctl statusEnsure the status is RUNNING.
- Access the application in a browser:
http://your-vps-ip:8000Troubleshooting
- App fails to start: Check logs with
sudo tail -f /home/ubuntu/myproject/logs/gunicorn.log. - Supervisor doesn’t recognize app: Verify the program name in the config and run
sudo supervisorctl reread. - App not responding: Ensure the port is open (check firewall) and WSGI configuration is correct.
References
- High-RAM Cloud VPS for Self-Hosted LLMs & API Gateways
- Comprehensive Guide to Deploying Cloud Native Backup with Velero on k3s VPS
- Deploy Applications Lightning-Fast by Installing Kamal 2 on a Minimal Ubuntu VPS
Related infrastructure & engineering guides: High-RAM Cloud VPS for Self-Hosted LLMs & API Gateways · Comprehensive Guide to Deploying Cloud Native Backup with Velero on k3s VPS
High-RAM Cloud VPS Plans Optimized for AI, Databases & Cache
Enterprise RAID 10 NVMe SSDs, CMC & FPT District 9 uplinks with Anycast Anti-DDoS and 99.99% SLA.
VPS-4E
10% VAT included
- CPU: 4 vCPU
- RAM: 8 GB
- SSD: 50 GB NVMe
