Ceci est une ancienne révision du document !
Créer et activer un environnement virtuel :
python3 -m venv venv . venv/bin/activate
Intaller FastAPI :
(venv) pip install fastapi
Installer Uvicorn :
(venv) pip install "uvicorn[standard]"
Script de base :
from fastapi import FastAPI app = FastAPI() @app.get("/") def index(): return {"message": "Hello there!"} app.get("/hello/{name}") def hello(name:str): return {"message": "Hello %s !" % name}
Lancer le serveur en mode développement (reload) :
uvicorn main:app --reload
Créer et activer un environnement virtuel :
python3 -m venv venv . venv/bin/activate
Intaller FastAPI :
(venv) pip install fastapi
Installer Uvicorn :
pip install "uvicorn[standard]"
Lancer le serveur sur le port 8000 :
uvicorn main:app --host 0.0.0.0 --port 8000
LIRE CECI :
Source: https://github.com/fnep/example_fastapi_behind_apache_using_wsgi/tree/main
Créer et activer un environnement virtuel :
python3 -m venv venv . venv/bin/activate
Intaller FastAPI :
(venv) pip install fastapi
Installer a2wsgi :
(venv) pip install a2wsgi
Fichier principal de l'application :
from fastapi import FastAPI from a2wsgi import ASGIMiddleware app = FastAPI() @app.get("/") def index(): return { "messsage": "Hello world !" } @app.get("/hello/{name}") def hello(name:str): return { "message": "Hello %s !" % name } # mod_wsgi expects the name 'application' by default application = ASGIMiddleware(app)
Configuration d'Apache :
<VirtualHost *:80> ServerName api.domaine.com ServerAdmin root@domaine.com RewriteEngine on RewriteCond %{HTTPS} !on RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} </VirtualHost> <VirtualHost *:443> ServerName api.domaine.com ServerAdmin root@domaine.com WSGIDaemonProcess fastapi_app user=www-data group=www-data python-home=/var/www/api_domaine_com/venv threads=5 WSGIProcessGroup fastapi_app WSGIScriptAlias / /var/www/api_domaine_com/main.py DocumentRoot /var/www/api_domaine_com <Directory /var/www/api_domaine_com> Options Indexes FollowSymLinks MultiViews AllowOverride all Require all denied Require all granted allow from all </Directory>