运行 在 ubuntu 服务器上使用 uvicorn 的 fastapi 应用程序
Running fastapi app using uvicorn on ubuntu server
我正在处理将 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 - 来自 google 云平台实例的外部 IP 地址
如何启动项目才能通过IP访问?
您无法在本地启动快速 api 应用到 GCP 中的远程服务器。
您必须将您的应用程序部署到 GCP。换句话说,您需要 运行 在远程服务器而不是本地主机上执行该命令。
--host
应该是您的 GCP 服务器的本地地址。
uvicorn main:app <b>--host 0.0.0.0</b> --port 8000
现在通过 http://66.226.247.55:8000
访问应用程序
注意:您应该打开 GCP 服务器的 8000 端口。
如果您使用的是 nginx 服务器
在 /etc/nginx/sites-enabled/ 中创建一个文件
创建文件 touch fastapi_nginx
将代码复制到文件中并进行相应调整
server{
listen 80;
server_name "your public ip";
location / {
proxy_pass http://127.0.0.1:8000; #localhost
}
}
这应该重新路由到您的 public ip
我正在处理将 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 - 来自 google 云平台实例的外部 IP 地址 如何启动项目才能通过IP访问?
您无法在本地启动快速 api 应用到 GCP 中的远程服务器。
您必须将您的应用程序部署到 GCP。换句话说,您需要 运行 在远程服务器而不是本地主机上执行该命令。
--host
应该是您的 GCP 服务器的本地地址。
uvicorn main:app <b>--host 0.0.0.0</b> --port 8000
现在通过 http://66.226.247.55:8000
注意:您应该打开 GCP 服务器的 8000 端口。
如果您使用的是 nginx 服务器
在 /etc/nginx/sites-enabled/ 中创建一个文件 创建文件 touch fastapi_nginx 将代码复制到文件中并进行相应调整
server{
listen 80;
server_name "your public ip";
location / {
proxy_pass http://127.0.0.1:8000; #localhost
}
}
这应该重新路由到您的 public ip