Apache2 & wsgi

Install

Installer les paquets:

apt update && apt dist-upgrade apt install apache2 python3 python3-pip python3-venv

Créer l'environnement:

cd /opt && mkdir flask-app cd flask-app python3 -m venv flask-venv source flask-venv/bin/activate pip3 install flask

Créer un fichier app.py

nano app.py

Avec le contenu:

from flask import Flask app = Flask(__name__) @app.route('/') def index(): return 'Hello World'

Tester la base:

export FLASK_APP=app.py flask run --host=0.0.0.0

Quitter l'envirronnement:

deactivate

Installer le module wsgi pour apache:

apt install libapache2-mod-wsgi-py3

Créer un fichier flask-app:

nano /opt/flask-app/flask-app.wsgi

Contenu:

import sys sys.path.insert(0,'/opt/flask-app') from app import app as application

nano /etc/apache2/sites-available/flask.conf

<VirtualHost *:80> ServerName yourdomain.com DocumentRoot /opt/flask-app/

WSGIDaemonProcess app user=www-data group=www-data threads=5 python-home=/opt/flask-app/flask-venv WSGIScriptAlias / /opt/flask-app/flask-app.wsgi

ErrorLog ${APACHE_LOG_DIR}/flask-error.log CustomLog ${APACHE_LOG_DIR}/flask-access.log combined

<Directory /opt/flask-app> WSGIProcessGroup app WSGIApplicationGroup %{GLOBAL} Order deny,allow Require all granted </Directory> </VirtualHost>

a2dissite 000-default.conf a2ensite flask.conf

https://www.rosehosting.com/blog/how-to-install-flask-on-ubuntu-22-04-with-apache-and-wsgi/

Sources

https://www.bogotobogo.com/python/Flask/Python_Flask_HelloWorld_App_with_Apache_WSGI_Ubuntu14.php