UnderHost
Knowledgebase Docs

Deploy Python Apps on aaPanel: Flask, Django, FastAPI

Deploy Python applications on aaPanel. Flask, Django, FastAPI setup, virtual environments, Gunicorn, Nginx proxy, environment variables, monitoring.

On this page

Python is powerful for web applications: fast to develop, maintainable, large ecosystem of libraries. aaPanel simplifies Python deployment: manage virtual environments, configure Gunicorn, set up Nginx reverse proxy, and monitor everything from one dashboard. No SSH knowledge required.

Why Python for Web Apps

  • Fast development: Build features quickly with readable code
  • Large ecosystem: 300,000+ packages for everything (data science, APIs, web, automation)
  • Scalable: Powers Instagram, Spotify, Dropbox, etc.
  • Machine learning: TensorFlow, Scikit-learn for AI/ML applications
  • Easy to learn: Great for beginners, powerful for experts

Python Frameworks

FrameworkComplexityBest For
FlaskLightAPIs, small apps, custom control
DjangoHeavyFull-featured apps, admin panel, ORM
FastAPILight-MediumModern APIs, async, real-time apps
PyramidMediumLarge, flexible applications

Requirements

  • Cloud VPS with aaPanel (minimum 1GB RAM, 2GB+ recommended)
  • Python 3.8+ (modern, faster, better libraries)
  • pip (Python package manager) for installing dependencies
  • Virtual environment to isolate project dependencies
  • Gunicorn to run Python app
  • Nginx as reverse proxy (aaPanel handles this)

Setup Python Environment

Step 1: Install Python in aaPanel

  1. aaPanel → App Store → Python
  2. Select version (3.10+ recommended)
  3. Click Install
  4. Wait 2-5 minutes

Step 2: Create project directory

mkdir -p /www/pythonapp
cd /www/pythonapp

Step 3: Create virtual environment

python3 -m venv venv
source venv/bin/activate  # Activate environment
pip install --upgrade pip

Step 4: Install dependencies

# Install framework and dependencies
pip install flask gunicorn  # For Flask
# OR
pip install django gunicorn  # For Django

# Generate requirements.txt
pip freeze > requirements.txt

Deploy Python App

Example Flask app (app.py):

from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello():
    return 'Hello from Python on aaPanel!'

if __name__ == '__main__':
    app.run(debug=False, host='127.0.0.1', port=5000)

Gunicorn configuration:

# Create gunicorn_config.py
workers = 4
worker_class = 'sync'
worker_connections = 100
timeout = 30
bind = '127.0.0.1:5000'
debug = False

Start with Gunicorn:

cd /www/pythonapp
source venv/bin/activate
gunicorn -c gunicorn_config.py app:app

Process Management with PM2

aaPanel uses PM2 to manage Python apps (auto-restart on failure):

npm install -g pm2  # Install PM2

# Create ecosystem.config.js
module.exports = {
  apps: [{
    name: 'pythonapp',
    cwd: '/www/pythonapp',
    script: 'venv/bin/gunicorn',
    args: '-c gunicorn_config.py app:app',
    instances: 4,
    exec_mode: 'cluster',
    watch: ['app.py'],
    ignore_watch: ['node_modules']
  }]
};

# Start with PM2
pm2 start ecosystem.config.js
pm2 save  # Remember on reboot

Nginx Reverse Proxy

aaPanel automatically configures Nginx to proxy requests to Gunicorn:

  • User requests yourdomain.com:80
  • Nginx proxies to Python app on 127.0.0.1:5000
  • Response sent back to user
  • SSL/HTTPS handled by Nginx automatically

Manual Nginx config (if needed):

server {
    listen 80;
    server_name yourdomain.com;

    location / {
        proxy_pass http://127.0.0.1:5000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

Troubleshooting

App won't start:

  • Check for syntax errors: python -m py_compile app.py
  • View logs: pm2 logs pythonapp
  • Test manually: python app.py (should show no errors)

502 Bad Gateway error:

  • Gunicorn process not running: pm2 restart pythonapp
  • Wrong port in Nginx config (should match Gunicorn port)
  • Firewall blocking connection (check ufw rules)

High memory usage:

  • Reduce worker count in gunicorn_config.py
  • Profile code: pip install memory-profiler
  • Add more RAM to VPS

App hangs / slow requests:

  • Increase Gunicorn timeout: timeout = 120
  • Add caching: Redis or Memcached
  • Optimize database queries
Use virtual environments for isolation

Always use Python virtual environments. They isolate project dependencies, prevent version conflicts, and allow multiple Python projects on the same VPS.

Related: aaPanel setup | Python deployment | Flask deployment | Django deployment

Was this article helpful?

Need aaPanel hosting?

Install aaPanel on an UnderHost VPS or dedicated server when you want a simple web interface for sites, databases, SSL, and apps.

Related articles

Back to aaPanel