首页 > 解决方案 > 在 ubuntu 服务器上使用 uvicorn 运行 fastapi 应用程序

问题描述

我正在处理将 FastAPI 上的项目沉积到远程 ubuntu 服务器。我将尝试通过命令从终端(使用 SSH 连接)运行项目

gunicorn -k uvicorn.workers.UvicornWorker main:app

输出是

gunicorn -k uvicorn.workers.UvicornWorker main:app
[2020-07-14 15:24:28 +0000] [23102] [INFO] Starting gunicorn 20.0.4
[2020-07-14 15:24:28 +0000] [23102] [INFO] Listening at: http://127.0.0.1:8000 (23102)
[2020-07-14 15:24:28 +0000] [23102] [INFO] Using worker: uvicorn.workers.UvicornWorker
[2020-07-14 15:24:28 +0000] [23104] [INFO] Booting worker with pid: 23104
[2020-07-14 15:24:28 +0000] [23104] [INFO] Started server process [23104]
[2020-07-14 15:24:28 +0000] [23104] [INFO] Waiting for application startup.
[2020-07-14 15:24:28 +0000] [23104] [INFO] Application startup complete.

但我需要该项目在服务器的 IP 地址上可用。如果我尝试

uvicorn main:app --host 66.226.247.55 --port 8000 

我明白了

INFO:     Started server process [23308]
INFO:     Waiting for application startup.
INFO:     Connected to database postgresql://recognition:********@localhost:5432/reco
INFO:     Application startup complete.
ERROR:    [Errno 99] error while attempting to bind on address ('66.226.247.55', 8000): cannot assign requested address
INFO:     Waiting for application shutdown.
INFO:     Disconnected from database postgresql://recognition:********@localhost:5432/reco
INFO:     Application shutdown complete.

Where 66.226.247.55 - 来自谷歌云平台实例的外部 IP 地址 如何启动项目以便可以通过 IP 访问?

标签: gunicornfastapiuvicorn

解决方案


--host应该是 GCP 服务器的本地地址。

uvicorn main:app --host 0.0.0.0 --port 8000

现在通过http://66.226.247.55:8000

注意:您应该打开GCP 服务器的8000 端口


推荐阅读