flask app

Die folgende Beschreibung nimmt Debian Bullseye als Grundlage.


Installiere pip und erstelle venv:
root $ apt install python3-pip
$ mkdir flask-app
$ cd flask-app
$ python3 -m venv .venv
$ source .venv/bin/activate
(.venv) $ python -m pip install --upgrade pip
(.venv) $ python -m pip install flask
Erstelle hello world app: app.py
from flask import Flask

app = Flask(__name__)

def main():
    return "Hello world!"

if __name__ == "__main__":


Erstelle WSGI Einstiegpunkt: wsgi.py:
from app import app

if __name__ == "__main__":
Installiere gunicorn:
(.venv) $ python -m pip install gunicorn
(.venv) $ deactivate
Erstelle Systembenutzer gunicorn:
root $ adduser --system --home /var/www/gunicorn --ingroup www-data --disabled-password --shell /bin/bash gunicorn
Description=Flask Web Application Server using Gunicorn

ExecStartPre=/usr/bin/mkdir -p -m 750 /tmp/gunicorn
ExecStart=/var/www/gunicorn/flask-app/.venv/bin/gunicorn -w 3 --bind unix:/tmp/gunicorn/flask-app.sock -m 007 wsgi:app

Kopiere das flask Projekt and aktiviere den Dienst:
root $ cp flask-app /var/www/gunicorn
root $ chown -R gunicorn:www-data /var/www/gunicorn/flask-app
root $ systemctl daemon-reload
root $ systemctl enable gunicorn-flask-app
root $ systemctl start gunicorn-flask-app


Install nginx:
root $ apt install nginx
### gunicorn wsgi configuration

upstream app_server {
  # fail_timeout=0 means we always retry an upstream even if it failed
  # to return a good HTTP response

  # for UNIX domain socket setups
  server unix:/tmp/gunicorn/flask-app.sock fail_timeout=0;

  # for a TCP configuration
  # server fail_timeout=0;

server {
  # use 'listen 80 deferred;' for Linux
  # use 'listen 80 accept_filter=httpready;' for FreeBSD
  listen 80 deferred;
  client_max_body_size 4G;

  # set the correct host(s) for your site
  server_name _;

  keepalive_timeout 5;

  # path for static files
  root /var/www/html;

  # Add index.php to the list if you are using PHP
  index index.html index.htm index.nginx-debian.html;

  location / {
    # checks for static file, if not found proxy to app
    try_files $uri @proxy_to_app;

  location @proxy_to_app {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header Host $http_host;
    # we don't want nginx trying to do something clever with
    # redirects, we set the Host: header above already.
    proxy_redirect off;
    proxy_pass http://app_server;

  location ~ /\.ht {
    deny all;
  location ~ /\.venv {
    deny all;
Enable site:
root $ cd /etc/nginx/sites-enabled
root $ rm default
root $ ln -s /etc/nginx/sites-available/gunicorn gunicorn 
root $ systemctl reload nginx
© 2001 - 2024 Frank Remetter