Flask is a lightweight Python web framework ideal for microservices, APIs, and small to medium applications. Gunicorn serves Flask in production as a WSGI application server, while Nginx handles SSL, static files, and load balancing. This guide deploys Flask on Ubuntu 26.04 LTS.
Tested and valid on:
- Ubuntu 26.04 LTS
Prerequisites
- Ubuntu 26.04 LTS with Python 3.14 and Nginx installed
- A user with sudo privileges
Step 1 – Set Up the Project
sudo mkdir -p /var/www/myflask
sudo chown $USER:$USER /var/www/myflask
cd /var/www/myflask
python3 -m venv .venv
source .venv/bin/activate
pip install flask gunicorn
Step 2 – Create the Flask Application
cat > app.py << 'EOF'
from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
return 'Flask on Ubuntu 26.04!
'
if __name__ == '__main__':
app.run()
EOF
Step 3 – Create the WSGI Entry Point
cat > wsgi.py << 'EOF'
from app import app
if __name__ == '__main__':
app.run()
EOF
Step 4 – Test Gunicorn
gunicorn --workers 3 --bind 0.0.0.0:5000 wsgi:app
Step 5 – Create a systemd Service
sudo nano /etc/systemd/system/flaskapp.service
Add:
[Unit]
Description=Gunicorn for Flask
After=network.target
[Service]
User=www-data
Group=www-data
WorkingDirectory=/var/www/myflask
Environment="PATH=/var/www/myflask/.venv/bin"
ExecStart=/var/www/myflask/.venv/bin/gunicorn --workers 3
--bind unix:/run/flaskapp.sock wsgi:app
[Install]
WantedBy=multi-user.target
sudo chown -R www-data:www-data /var/www/myflask
sudo systemctl start flaskapp
sudo systemctl enable flaskapp
Step 6 – Configure Nginx
sudo nano /etc/nginx/sites-available/myflask
Add:
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://unix:/run/flaskapp.sock;
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/myflask /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx
Step 7 – Add SSL
sudo certbot --nginx -d example.com
Conclusion
Your Flask application is now running in production on Ubuntu 26.04 LTS. Gunicorn handles concurrent requests and Nginx provides a secure, high-performance frontend. Use Flask’s application factory pattern for larger projects.