Deploy Flask with Gunicorn and Nginx: WSGI config, Docker image, and production env variables.
Deploying Flask
# Gunicorn WSGI server
pip install gunicorn
gunicorn -w 4 -b 0.0.0.0:8000 "app:create_app()"
# Nginx config
server {
listen 80;
server_name myapp.com;
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
location /static {
root /var/www/myapp;
expires 1y;
}
}
# Docker
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
EXPOSE 8000
CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:8000", "app:create_app()"]
# .env for production
FLASK_ENV=production
SECRET_KEY=super-random-secret
DATABASE_URL=postgresql://user:pass@db:5432/myapp