UnderHost
Knowledgebase Docs

Deploy Flask Applications on UnderHost VPS

Deploy lightweight Flask web applications on UnderHost VPS. Configure Gunicorn, Nginx, databases, and static files for production.

On this page

Flask is a lightweight Python web framework perfect for small to medium applications and microservices. Unlike Django, Flask gives you flexibility without built-in features—you choose your tools. On UnderHost VPS, Flask deploys the same way as Django but with simpler configuration.

Requirements

  • UnderHost Cloud VPS
  • Python 3.8+
  • Nginx and Gunicorn
  • Database (optional, your choice)
  • SSH access

Project Setup

Create virtual environment:

cd /home/username
python3 -m venv myapp_env
source myapp_env/bin/activate
pip install flask gunicorn

Create Flask app structure:

mkdir myapp
cd myapp
cat > app.py << 'EOF'
from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello():
    return 'Hello from Flask!'

if __name__ == '__main__':
    app.run()
EOF

Configure Gunicorn

Test locally with Gunicorn:

gunicorn --bind 127.0.0.1:8000 app:app

Your Flask app is now accessible locally on port 8000.

Nginx Reverse Proxy

Create /etc/nginx/sites-available/myapp:

server {
    listen 80;
    server_name yourdomain.com;

    location / {
        proxy_pass http://127.0.0.1:8000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

Enable and restart:

sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/
sudo systemctl restart nginx

Systemd Service

Create /etc/systemd/system/myapp.service:

[Unit]
Description=Flask Gunicorn
After=network.target

[Service]
Type=notify
User=username
WorkingDirectory=/home/username/myapp
ExecStart=/home/username/myapp_env/bin/gunicorn \
    --workers 3 \
    --bind 127.0.0.1:8000 \
    app:app

[Install]
WantedBy=multi-user.target

Start service:

sudo systemctl daemon-reload
sudo systemctl enable myapp
sudo systemctl start myapp

Static Files

Serve CSS, JavaScript, images via Nginx:

# In Nginx config, add:
location /static/ {
    alias /home/username/myapp/static/;
    expires 30d;
}

# In Flask, use:
@app.route('/static/')
def static(filename):
    return send_from_directory('static', filename)

Production Tips

  • Use environment variables: os.environ.get('DATABASE_URL') for sensitive config
  • Enable HTTPS: Add SSL cert with Let's Encrypt (included in aaPanel/CloudPanel)
  • Monitor logs: journalctl -u myapp -f to follow service logs
  • Use multiple workers: --workers 3 or more based on CPU cores
  • Enable caching: Use Redis or Memcached for session/cache data
Flask vs Django

Use Flask for: Microservices, APIs, small apps, custom solutions. Use Django for: Full-featured applications with auth, admin, ORM.

Related: Django deployment | Python deployment | Python on CloudPanel

Was this article helpful?

Need a developer-friendly server?

Use an UnderHost Cloud VPS for SSH, Git, Node.js, Python, Laravel, Docker, cron, and custom development workflows.

Related articles

Back to Developer Tools