Back to articles
AI & Cloud

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...

DPTCloud Engineering4 min read
Comprehensive Guide to Deploying Python Django/Flask Applications on VPS with Gunicorn and Supervisor

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 gunicorn

Step 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:application

Replace 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 -y

Step 2: Create Supervisor Configuration File

Create a configuration file in /etc/supervisor/conf.d/:

sudo nano /etc/supervisor/conf.d/django-app.conf

Add 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.log

Step 3: Start Supervisor

sudo supervisorctl reread\nsudo supervisorctl update\nsudo supervisorctl start django-app

Verify and Test Functionality

  • Check process status:
sudo supervisorctl status

Ensure the status is RUNNING.

  • Access the application in a browser:
http://your-vps-ip:8000

Troubleshooting

  • 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

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

DPTCloud Tier III Infrastructure

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.

View 22 VPS Plans
ECO · vn-02Recommended

VPS-4E

297.000 ₫ / mo

10% VAT included

  • CPU: 4 vCPU
  • RAM: 8 GB
  • SSD: 50 GB NVMe
ECO · vn-02

VPS-5E

614.900 ₫ / mo

10% VAT included

  • CPU: 8 vCPU
  • RAM: 16 GB
  • SSD: 100 GB NVMe
ADVANCED · vn-03Recommended

VPS-4T

548.900 ₫ / mo

10% VAT included

  • CPU: 4 vCPU
  • RAM: 8 GB
  • SSD: 50 GB NVMe

Related Articles

View all
ZaloZalo SupportDiscord0384 753 478