如何使用 wsgi 服务器在共享主机上部署 fastapi 应用程序?

问题描述 投票:0回答:2

在最近的一篇文章中, https://www.vultr.com/docs/how-to-deploy-fastapi-applications-with-gunicorn-and-nginx-on-ubuntu-20-04/

我读到 fastapi“如果需要可以与 WSGI 一起使用”。我想知道怎么办?

我用 fastapi 做了一个完整的项目,并尝试将其部署在 cpanel 共享主机上(我目前的选择),

在 wsgi.py 文件中我使用了 a2sg 库

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

但是我遇到503暂时繁忙,请在浏览页面时重试

那么,我如何部署我的应用程序,我轻松部署了 django,但 fasapi 是一个问题,因为它主要使用 ASGI。 也可以吗

python cpanel fastapi passenger asgi
2个回答
1
投票

除了添加 A2WSGI 中间件之外,您还需要执行一些检查。

  1. 在每个目录(包括应用程序的根目录)中添加名为

    __init__.py
    的空 py 文件

  2. 使用您在

    passenger.py
    中给出的相同代码创建新的
    wsgi.py

    文件
  3. 在设置Python应用程序时使用上面的

    passenger.py
    文件,同时保留你已经在同一目录中准备好的
    wsgi.py
    。 (C面板->设置Python应用程序)

  4. 使用未使用的端口(如

    Uvicorn
    等)运行
    60323
    服务器

  5. 从 Python 应用程序选项重新启动应用程序


0
投票

我在 github 上创建了这个简单的存储库,以展示如何使用 WSGI 服务器在 Apachepython 下运行基于 ASGI(带有 fastapi)的基本 Web 应用程序:danielemaddaluno/pyjel

这是项目:

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

wsgi.py
文件:

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)

app.py
文件:

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)

requirements.txt
文件:

a2wsgi
uvicorn
fastapi

setup.sh
文件:

#!/bin/sh
virtualenv venv
source venv/bin/activate
pip install -r requirements.txt
deactivate
© www.soinside.com 2019 - 2024. All rights reserved.