Skip to content

Loading…

← Back to Blog Developers

Flask on Ubuntu 24.04 with Gunicorn and Nginx

By Hoststack Team·Nov 2026·3 min read

Flask's built-in server is for development only. In production, a WSGI server such as Gunicorn runs the app and Nginx sits in front of it to handle HTTPS and client connections. This guide sets that up on an Ubuntu 24.04 VPS, with the app restarting automatically after crashes and reboots.

What you need

  • An Ubuntu 24.04 VPS with root or sudo access. A 1-core, 2 GB Linux VPS is enough for a small app.
  • A domain or subdomain, for example app.example.com, with an A record pointing to the server.

1. Install Python tools and Nginx

sudo apt update
sudo apt -y install python3-venv python3-pip nginx

2. Create a user and a virtual environment

Running the app as its own user keeps it separate from root and from other apps on the server.

sudo adduser --disabled-password --gecos "" flask
sudo -iu flask
mkdir ~/myapp && cd ~/myapp
python3 -m venv venv
source venv/bin/activate
pip install flask gunicorn

Create ~/myapp/app.py. ProxyFix makes Flask trust the client IP and HTTPS scheme that Nginx passes on:

from flask import Flask
from werkzeug.middleware.proxy_fix import ProxyFix

app = Flask(__name__)
app.wsgi_app = ProxyFix(app.wsgi_app, x_for=1, x_proto=1)

@app.get("/")
def index():
    return "Hello from Flask"

Test it with Gunicorn, then press Ctrl+C and type exit to go back to your sudo user:

gunicorn --bind 127.0.0.1:8000 app:app
# in a second terminal: curl http://127.0.0.1:8000

3. Run Gunicorn as a systemd service

Gunicorn's documentation suggests (2 × CPU cores) + 1 workers as a starting point, so 3 on a 1-core server. Create /etc/systemd/system/myapp.service:

[Unit]
Description=Gunicorn for myapp
After=network.target

[Service]
User=flask
Group=flask
WorkingDirectory=/home/flask/myapp
Environment="PATH=/home/flask/myapp/venv/bin"
ExecStart=/home/flask/myapp/venv/bin/gunicorn --workers 3 --bind 127.0.0.1:8000 app:app
Restart=always

[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl enable --now myapp
sudo systemctl status myapp

4. Put Nginx in front

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

server {
    listen 80;
    server_name app.example.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;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}
sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx

5. HTTPS and the firewall

sudo apt -y install certbot python3-certbot-nginx
sudo certbot --nginx -d app.example.com
sudo ufw allow OpenSSH
sudo ufw allow 'Nginx Full'
sudo ufw enable

Certbot edits the Nginx config to serve HTTPS and sets up automatic renewal.

Deploying updates

sudo -iu flask
cd ~/myapp && git pull            # or copy the new files
source venv/bin/activate && pip install -r requirements.txt
exit
sudo systemctl restart myapp

Common problems

  • 502 Bad Gateway: Gunicorn is not running. Check sudo journalctl -u myapp -n 50.
  • Changes do not appear: restart the service after every deploy; Gunicorn does not reload code by itself.
  • Wrong client IP or http links behind HTTPS: make sure ProxyFix is applied and Nginx sends the X-Forwarded headers above.

Host your Flask app

Linux VPS plans with full root access, NVMe SSD and a dedicated IPv4 address, or managed Python hosting from ₹99/month.

See Python hosting

Deploy today

Online in 60 seconds. Supported around the clock.

Hosting, VPS and game servers across Mumbai, Delhi, Singapore, Germany, France & the USA — INR billing, GST invoices, and code APEX20 for 20% off your first order.

5,000+ customers served
99.9% uptime SLA
<8ms ping within Mumbai
24/7 expert support
WhatsApp Discord