skip to Main Content

in a recent article,
https://www.vultr.com/docs/how-to-deploy-fastapi-applications-with-gunicorn-and-nginx-on-ubuntu-20-04/

I read that fastapi "can work with WSGI if needed". I was wondering how?

I did a whole project with fastapi and tried to deploy it on cpanel shared hosting(my option for the moment) ,

in the wsgi.py file I used a2sg library

from main import app
from a2wsgi import ASGIMiddleware
application = ASGIMiddleware(app)

but I get 503 temporary busy, try again when I browse to the page

so, how can I deploy my app , I deployed django with ease but fasapi is an issue as it mainly uses ASGI.
is it also possible?

2

Answers


  1. There are few checks you need to do apart from adding the A2WSGI Middleware.

    1. Adding empty py files named as __init__.py in each directory including root of application

    2. Create new passenger.py file with same code you have given in wsgi.py

    3. Use the above passenger.py file while setting up the python application, also keep wsgi.py as you have prepared already in same directory.
      (Cpanel->Setup Python App)

    4. Run Uvicorn server with unused port like 60323 etc.

    5. Restart the app from the Python Application Options

    Login or Signup to reply.
  2. I have created this simple repository on github to show how to run under Apachepython a basic webapp based on ASGI (with fastapi) using a WSGI server: danielemaddaluno/pyjel

    This is the project:

    .
    ├── app
    │   └── app.py
    ├── requirements.txt
    ├── setup.sh
    └── wsgi.py
    

    The wsgi.py file:

    import os, sys
    
    virtenv = os.path.expanduser('~') + '/venv/'
    virtualenv = os.path.join(virtenv, 'bin/activate_this.py')
    try:
        if sys.version.split(' ')[0].split('.')[0] == '3':
            exec(compile(open(virtualenv, "rb").read(), virtualenv, 'exec'), dict(__file__=virtualenv))
        else:
            execfile(virtualenv, dict(__file__=virtualenv))
    except IOError:
        pass
    
    sys.path.append(os.path.expanduser('~'))
    sys.path.append(os.path.expanduser('~') + '/ROOT/')
    
    from app import app
    from a2wsgi import ASGIMiddleware
    application = ASGIMiddleware(app.app)
    

    The app.py file:

    import uvicorn
    from fastapi import FastAPI
    
    app = FastAPI()
    
    @app.get("/")
    async def root():
        return {"message": "Hello World"}
    
    if __name__ == "__main__":
        uvicorn.run(app, host="0.0.0.0", port=8000)
    

    The requirements.txt file:

    a2wsgi
    uvicorn
    fastapi
    

    The setup.sh file:

    #!/bin/sh
    virtualenv venv
    source venv/bin/activate
    pip install -r requirements.txt
    deactivate
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search