Flask is a lightweight Python web microframework. This guide deploys a Flask application in production on Ubuntu 24.04 LTS using Gunicorn as the WSGI server and Nginx as the reverse proxy.

Tested and valid on:

  • Ubuntu 24.04 LTS

Prerequisites

  • Ubuntu 24.04 LTS server
  • Python 3.12 and pip installed
  • Nginx installed

Step 1 – Set Up the Project

Create the project directory and virtual environment:

mkdir /var/www/flaskapp && cd /var/www/flaskapp
python3.12 -m venv venv
source venv/bin/activate
pip install flask gunicorn

Step 2 – Create the Flask Application

Create app.py:

from flask import Flask
app = Flask(__name__)

@app.route('/')
def index():
    return '

Hello from Flask on Ubuntu 24.04!

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

Step 3 – Create the WSGI Entry Point

Create wsgi.py:

from app import app

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

Step 4 – Create a Systemd Service for Gunicorn

Create the service file:

sudo nano /etc/systemd/system/flaskapp.service

Add:

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

[Service]
User=www-data
Group=www-data
WorkingDirectory=/var/www/flaskapp
Environment="PATH=/var/www/flaskapp/venv/bin"
ExecStart=/var/www/flaskapp/venv/bin/gunicorn --workers 3 --bind unix:flaskapp.sock -m 007 wsgi:app

[Install]
WantedBy=multi-user.target

Enable and start:

sudo chown -R www-data:www-data /var/www/flaskapp
sudo systemctl enable flaskapp
sudo systemctl start flaskapp

Step 5 – Configure Nginx

Create the server block:

sudo nano /etc/nginx/sites-available/flaskapp

Add:

server {
    listen 80;
    server_name flask.example.com;
    location / {
        include proxy_params;
        proxy_pass http://unix:/var/www/flaskapp/flaskapp.sock;
    }
}

Enable and reload:

sudo ln -s /etc/nginx/sites-available/flaskapp /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx

Step 6 – Add SSL

Secure with Let’s Encrypt:

sudo certbot --nginx -d flask.example.com

Conclusion

Your Flask application is now deployed on Ubuntu 24.04 LTS. Gunicorn provides production-grade WSGI serving while Nginx handles SSL termination and acts as a reverse proxy.