【问题标题】:Running fastapi app using uvicorn on ubuntu server在 ubuntu 服务器上使用 uvicorn 运行 fastapi 应用程序
【发布时间】:2020-07-14 15:44:16
【问题描述】:

我正在处理将 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.

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

【问题讨论】:

    标签: gunicorn fastapi uvicorn


    【解决方案1】:

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

    uvicorn main:app <b>--host 0.0.0.0</b> --port 8000

    现在通过 http://66.226.247.55:8000

    访问应用程序

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

    【讨论】:

    • 现在没有错误了。 INFO: Application startup complete. INFO: Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit) 但是我联系IP地址时仍然无法得到回应。还有其他细微差别吗?
    • 可能你还没有打开你的 GCP 的 8000 端口。
    • 允许端口 8000 使用 ufw sudo ufw allow 8000
    • 我不是 GCP 或 DevOps 方面的专家,但是,你可以尝试在你的 GCP 中运行其他东西(比如python HTTP server),然后检查它是否公开可用。
    • 谢谢,我会尝试在 digitalocean 上做同样的事情。可能 GCP 有问题。
    【解决方案2】:

    您无法在 GCP 中的远程服务器本地启动您的快速 api 应用程序。

    您必须将应用部署到 GCP。换句话说,您需要在远程服务器而不是本地主机上运行该命令。

    【讨论】:

    • 当然,我不会从本地机器运行命令。我的项目被克隆到 GCP,我从 GC 控制台运行命令。
    猜你喜欢
    • 2014-06-09
    • 1970-01-01
    • 2018-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-03
    相关资源
    最近更新 更多