50% 折扣 所有计划,时间有限。开始于 $2.48/mo
快速API

快速API

1. 连接到您的服务器 使用 SSH 连接到您的服务器:ssh root@<server-ip> 2. 访问登录凭据 已创建名为 fastapi 的专用用户来管理您的应用程序。您可以在此文件中找到登录凭据: /root/.fastapi-creds 要查看它们,请运行: cat /root/.fastapi-creds 如果您想更改密码,请使用: passwd [...]

版本

最新的

操作系统

Ubuntu 服务器 24.04 LTS

分钟。内存

1 GB

IP类型

IPV4,IPV6

1. 连接到您的服务器

使用 SSH 连接到您的服务器:

ssh root@<server-ip>

2. 访问登录凭据

已创建一个名为 fastapi 的专用用户来管理您的应用程序。

您可以在此文件中找到登录凭据: /root/.fastapi-creds

要查看它们,请运行:

cat /root/.fastapi-creds

如果您想更改密码,请使用:

passwd fastapi

3.切换到App用户

切换到 fastapi 用户帐户来管理您的应用程序:

su - flask

该帐户包含您的 FastAPI 项目和环境。

4. 项目结构

这是默认的目录布局:

/home/fastapi/fastapi_app/
├── main.py          → Your main FastAPI application file
├── venv/            → Virtual environment
└── __pycache__/     → Compiled Python files (auto-generated)

5. 管理您的申请

导航到应用程序目录:

cd ~/fastapi_app

激活虚拟环境:

source venv/bin/activate

编辑主应用程序文件:

nano main.py

重新启动 FastAPI 服务以应用更改:

sudo systemctl restart uvicorn-fastapi

6. 查看日志

您可以按如下方式检查 Uvicorn 和 Nginx 的日志:

  • 独角兽日志: /var/log/uvicorn/
  • Nginx 错误日志: /var/log/nginx/error.log
  • Nginx访问日志: /var/log/nginx/access.log

要查看它们:

sudo tail -f /var/log/uvicorn/*

7. 访问您的 API

服务运行后,您的 FastAPI 应用程序就可以在浏览器中使用: http://<server-ip>

Nginx 自动将 HTTP 请求转发到运行 FastAPI 应用程序的 Uvicorn 套接字。

8. 更改域名或端口

如果要更新服务器名称、端口或域:

  1. 编辑 Nginx 站点配置: sudo nano /etc/nginx/sites-available/uvicorn-fastapi
  2. 测试配置: sudo nginx -t
  3. 重新加载 Nginx: sudo systemctl restart nginx

9.独角兽服务

FastAPI 应用程序通过名为 uvicorn-fastapi 的 systemd 服务运行。

它使用 /home/fastapi/fastapi_app/main:app 作为应用程序入口点并在失败时自动重新启动。

管理服务:

systemctl status uvicorn-fastapi
systemctl restart uvicorn-fastapi

编辑服务:

sudo vim /etc/systemd/system/uvicorn-fastapi.service
sudo systemctl daemon-reload
sudo systemctl restart uvicorn-fastapi
sudo systemctl status uvicorn-fastapi

10.启用HTTPS(可选)

使用 Certbot 启用 HTTPS。

立即部署 FastAPI。 每月 2.48 美元起。