Contents
FastAPI speaks ASGI, while Passenger, which runs Python apps behind cPanel’s Setup Python App page, speaks WSGI. A small adapter bridges the two. With the a2wsgi package you keep your FastAPI code as it is and add a three-line passenger_wsgi.py. For APIs, webhooks and admin tools that is all you need.
Prerequisites
- A plan with Setup Python App and SSH (the AI/Developer plans on ArabyHost).
- Python 3.9-3.12 and a project with
main.py(whereapp = FastAPI()lives) andrequirements.txt. - Preferably a subdomain such as
api.example.com. Running under a sub-path likeexample.com/apiworks too, but routes and the docs page then needroot_pathtuning.
Step-by-step deployment
1. Create the Python app
Open Software › Setup Python App in cPanel, click Create Application and fill in:
- Python version: 3.12
- Application root:
api - Application URL:
api.example.com - Application startup file:
passenger_wsgi.py - Application Entry point:
application - Passenger log file:
/home/USER/logs/api.log
Click Create, then copy the command shown at the top of the page for entering the virtualenv.
2. Upload the code and install packages
Upload your project into /home/USER/api with Git or a ZIP in File Manager, then over SSH:
source /home/USER/virtualenv/api/3.12/bin/activate && cd /home/USER/api
pip install --upgrade pip
pip install -r requirements.txt
pip install fastapi a2wsgi
A minimal main.py to test with:
from fastapi import FastAPI
app = FastAPI(title="My API")
@app.get("/health")
def health():
return {"status": "ok"}
3. Wrap the app in passenger_wsgi.py
import os
import sys
sys.path.insert(0, os.path.dirname(__file__))
from a2wsgi import ASGIMiddleware
from main import app
application = ASGIMiddleware(app)
application must match the Application Entry point field.
4. Add environment variables
In the app’s page in Setup Python App, use Environment variables › Add Variable for values such as DATABASE_URL or API_KEY, then click Save and Restart. These reach the app through Passenger only. If you also run scripts over SSH, keep the same values in a .env file (chmod 600) and load it with python-dotenv or pydantic-settings. For MySQL with SQLAlchemy, a typical URL is:
DATABASE_URL=mysql+pymysql://USER_api:password@localhost/USER_api?charset=utf8mb4
5. Restart and test
Click Restart, or run touch /home/USER/api/tmp/restart.txt after creating the tmp folder. Then:
curl -i https://api.example.com/health
You should get a 200 status and {"status":"ok"}. The interactive docs are at https://api.example.com/docs.
Alternative: uvicorn as a background worker
On AI/Developer plans you can also run uvicorn as a long-running process and let cron restart it when it stops, exactly like the bots in running a Telegram bot 24/7:
nohup /home/USER/virtualenv/api/3.12/bin/uvicorn main:app \
--host 127.0.0.1 --port 18731 --app-dir /home/USER/api \
>> /home/USER/api/uvicorn.log 2>&1 &
This gives you full ASGI (lifespan events, WebSockets), but the process listens on 127.0.0.1 only. That suits internal services, such as an API that another app in your account calls, or a worker that consumes a queue. Exposing a custom port to the internet is not part of a shared account’s settings, so ask support before designing around it. Pick an unusual port, because other accounts share the server and a taken port fails with “address already in use”.
Troubleshooting
TypeError: FastAPI.__call__() missing 1 required positional argument: 'send': Passenger is calling the ASGI app directly. Point the entry point at theASGIMiddlewarewrapper, not atapp.ModuleNotFoundError: No module named 'main': thesys.path.insertline is missing, ormain.pyisn’t in the app root. If the missing module is a package such asfastapi, you installed it outside the virtualenv. See ModuleNotFoundError.- Every route returns 404 under a sub-path: move the app to a subdomain, or set
root_pathto match the Application URL. - Startup code never runs: don’t rely on
lifespanor startup events behind an adapter. Open database connections lazily on first use instead. - The first request after a quiet period is slow: the app process starts on demand, so the first request pays the import time. Keep imports at module level light.
- A generic error page: read the Passenger log file. Tracebacks from import errors appear there. See Passenger app error.
On ArabyHost
FastAPI runs on the AI/Developer plans, which include Setup Python App with Python 3.9-3.12 through the CloudLinux selector (Phusion Passenger), SSH jailed in CageFS, Git, and background workers kept alive with cron. HTTPS comes free: AutoSSL issues a Let’s Encrypt certificate for your API subdomain automatically, and LiteSpeed serves it. Honest limits: no root access, no Docker and no GPU, so model inference belongs with an external API, and your app runs within your account’s own resource limits. For a Flask or Django project the steps are almost the same; see deploying Flask and FastAPI hosting.
FAQ
Do my async endpoints still work behind a2wsgi?
Yes. The adapter runs your async code in its own event loop, so `async def` endpoints and async database drivers keep working. Concurrency comes from the web server starting more app processes, not from a single uvicorn loop.
Can I use WebSockets with FastAPI on cPanel?
Not through a2wsgi, because WSGI has no WebSocket support. For live updates use short polling from the browser, or a hosted realtime service, and keep the FastAPI app for regular HTTP endpoints.
Do I need to install uvicorn?
Not for the Passenger route: Passenger loads passenger_wsgi.py and the adapter does the rest. You only need uvicorn if you run the app as a separate background worker.