Knowledge Base
0%
Intermediate ~30 min sudo required

Deploy a Python Flask/Django App with Gunicorn + Nginx

Set up a production-ready Python WSGI application behind Nginx with Gunicorn managed by systemd.

0 / 4 done

Prerequisites

  • Ubuntu server
  • Python Flask or Django application
  • Nginx installed
1
Step 1 Command

Install Python and create virtualenv

apt install python3 python3-pip python3-venv -y
cd /var/www/myapp
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt gunicorn
2
Step 2 Command

Create systemd service

# Save the gunicorn-service snippet to /etc/systemd/system/myapp.service
systemctl daemon-reload
systemctl enable myapp
systemctl start myapp
systemctl status myapp
3
Step 3 Command

Configure Nginx reverse proxy

# In /etc/nginx/sites-available/myapp:
# server {
#   listen 80;
#   server_name app.example.com;
#   location / {
#     proxy_pass http://unix:/var/www/myapp/myapp.sock;
#     proxy_set_header Host $host;
#   }
# }
ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/
nginx -t && systemctl reload nginx
4
Step 4 Command

Issue SSL certificate

certbot --nginx -d app.example.com