How to Deploy FastAPI on cPanel with Uvicorn Print

  • 0

If you're hosting your application with Prabhu Host, deploying a FastAPI app on cPanel is entirely possible even though FastAPI uses ASGI (unlike Flask or Django which use WSGI). You can run FastAPI using Uvicorn, a high performance ASGI server.

Follow this step-by-step guide to set up your FastAPI project properly:

Step 1: Create a Python Application in cPanel

  1. Log in to your Prabhu Host cPanel.

  2. Navigate to "Setup Python App" under the Software section.

  3. Click Create Application and configure the following:

    • Python Version: Choose Python 3.8 or later.

    • Application Root: e.g., /home/username/myfastapi

    • Application URL: Select a subdomain or folder where it will run.

    • Application Entry Point: cPanel will auto-generate passenger_wsgi.py (you will customize this later).

  4. Click Create to generate the environment.

Step 2: Install FastAPI and Uvicorn

Once the app is created, install the required packages within the virtual environment:

cd /home/username/myfastapi
source bin/activate # Activates the virtual environment
pip install fastapi uvicorn
 
You may also install any additional dependencies your app needs at this point.

Step 3: Configure ASGI with Uvicorn

Since FastAPI doesn’t use WSGI, you’ll need to replace passenger_wsgi.py with Uvicorn-compatible code:

  1. Navigate to your app directory.

  2. Delete or overwrite passenger_wsgi.py with the following:

from myapp import app # Replace 'myapp' with your actual filename
from os import environ
from uvicorn import Config, Server

port = int(environ.get('PORT', 8000))

config = Config(app=app, host="0.0.0.0", port=port, workers=1)
server = Server(config)
server.run()

Ensure your main FastAPI file (e.g., myapp.py) contains your FastAPI() instance as app.

Step 4: Edit the .htaccess File

Inside your project root (e.g., /home/username/myfastapi), create or update your .htaccess file:

PassengerStartupFile passenger_wsgi.py
PassengerPython /home/username/myfastapi/bin/python

This tells the server which file to run and which Python interpreter to use.

Step 5: Restart the Application

  1. Go back to "Setup Python App" in cPanel.

  2. Find your app and click "Restart".

Final Notes

  • Visit your FastAPI app’s URL to verify it’s running.

  • Ensure your firewall rules allow traffic on the correct port (if applicable).

  • For more robust deployment (with multiple workers, logging, etc.), consider deploying via a production ASGI server on a VPS.

 Need Help?

If you face any issues during deployment, feel free to reach out to Prabhu Host’s support team for quick assistance.


Was this answer helpful?
Back