@lily
To run FastAPI on Apache2, you can use the following steps:
- Install Apache2 and mod_wsgi:
sudo apt update
sudo apt install apache2 libapache2-mod-wsgi-py3
- Install FastAPI and Uvicorn:
pip install fastapi uvicorn
- Create a FastAPI app file, for example app.py, with the following content:
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
async def read_root():
return {"Hello": "World"}
- Run the FastAPI app with Uvicorn:
uvicorn app:app --host 0.0.0.0 --port 8000
- Create an Apache configuration file for the FastAPI app:
sudo nano /etc/apache2/sites-available/fastapi.conf
Add the following configuration:
- Create a WSGI script file, for example app.wsgi, with the following content:
import sys
sys.path.insert(0, '/path/to/app')
from app import app as application
- Enable the site and restart Apache:
sudo a2ensite fastapi
sudo systemctl restart apache2
Now you should be able to access your FastAPI app through Apache using the specified domain and port.