生产环境部署 FastAPI + Nginx + Systemd 服务

更新日期: 2022-02-24 阅读次数: 2082 字数: 706 分类: Python

测试开发环境

uvicorn server:app --reload

但是生产环境需要配置的项目要更多,例如:

  • 指定端口号
  • 如何利用多核。python 的历史问题
  • nginx 配置
  • systemd 配置

uvicorn 是什么

Uvicorn is a lightning-fast ASGI server implementation, using uvloop and httptools.

与 Gunicorn 一同使用

参考 uvicorn 及 FastAPI 的官方文档:

  • https://www.uvicorn.org/#running-with-gunicorn
  • https://fastapi.tiangolo.com/deployment/server-workers/

都建议在生产环境配合 Gunicorn 一起使用。原因是:

  • 方便的管理 work 进程数。主要是为了充分利用服务器的多核;虽然 unicorn 也能指定 work 数,但是不监控进程的状态,所以就不能保活?
  • work 进程可以平滑重启
  • 升级无需宕机

示例:

gunicorn server:app -w 4 -k uvicorn.workers.UvicornWorker --bind 127.0.0.1:9012

安装 gunicorn

pip install "uvicorn[standard]" gunicorn

配置 systemd

如果只单纯运行 gunicorn,会发现其没有自动进入后台运行

> gunicorn server:app -w 4 -k uvicorn.workers.UvicornWorker --bind 127.0.0.1:9012
[2022-02-24 22:16:04 +0800] [1133099] [INFO] Starting gunicorn 20.1.0
[2022-02-24 22:16:04 +0800] [1133099] [INFO] Listening at: http://127.0.0.1:9012 (1133099)
[2022-02-24 22:16:04 +0800] [1133099] [INFO] Using worker: uvicorn.workers.UvicornWorker
[2022-02-24 22:16:04 +0800] [1133101] [INFO] Booting worker with pid: 1133101
[2022-02-24 22:16:04 +0800] [1133102] [INFO] Booting worker with pid: 1133102
[2022-02-24 22:16:04 +0800] [1133103] [INFO] Booting worker with pid: 1133103
[2022-02-24 22:16:04 +0800] [1133104] [INFO] Booting worker with pid: 1133104
[2022-02-24 22:16:06 +0800] [1133101] [INFO] Started server process [1133101]
[2022-02-24 22:16:06 +0800] [1133101] [INFO] Waiting for application startup.
[2022-02-24 22:16:06 +0800] [1133101] [INFO] Application startup complete.
[2022-02-24 22:16:06 +0800] [1133102] [INFO] Started server process [1133102]
[2022-02-24 22:16:06 +0800] [1133102] [INFO] Waiting for application startup.
[2022-02-24 22:16:06 +0800] [1133102] [INFO] Application startup complete.
[2022-02-24 22:16:06 +0800] [1133103] [INFO] Started server process [1133103]
[2022-02-24 22:16:06 +0800] [1133103] [INFO] Waiting for application startup.
[2022-02-24 22:16:06 +0800] [1133103] [INFO] Application startup complete.
[2022-02-24 22:16:06 +0800] [1133104] [INFO] Started server process [1133104]
[2022-02-24 22:16:06 +0800] [1133104] [INFO] Waiting for application startup.
[2022-02-24 22:16:06 +0800] [1133104] [INFO] Application startup complete.

^C[2022-02-24 22:16:12 +0800] [1133099] [INFO] Handling signal: int
[2022-02-24 22:16:12 +0800] [1133099] [WARNING] Worker with pid 1133101 was terminated due to signal 3
[2022-02-24 22:16:12 +0800] [1133099] [WARNING] Worker with pid 1133104 was terminated due to signal 3
[2022-02-24 22:16:12 +0800] [1133099] [WARNING] Worker with pid 1133102 was terminated due to signal 3
[2022-02-24 22:16:12 +0800] [1133099] [WARNING] Worker with pid 1133103 was terminated due to signal 3
[2022-02-24 22:16:12 +0800] [1133099] [INFO] Shutting down: Master

将之前的 golang 服务的 systemd 配置文件复制一份,修改一下 gunicorn 的路径即可。

$ which gunicorn
/home/username/.local/bin/gunicorn
sudo systemctl enable some.service
sudo systemctl start some.service
sudo systemctl status some.service

部署还是太麻烦

python 的线上部署,不但比不过 golang,连 php 都不如。 概念多,流程复杂,对新手很不友好。而且性能还拿不出手,悲剧。

TODO

  • done: git 拉取代码
  • done: 安装 uvicorn, gunicorn
  • done: 测试能否运行起来。uvicorn server:app --reload
  • done: 整理各服务的端口占用情况,存在根目录的文件中,方便给新服务分配端口
  • nginx 配置:端口及 static 目录
  • systemd 配置

ImportError: libGL.so.1

运行

uvicorn server:app --reload

报错:

ImportError: libGL.so.1: cannot open shared object file: No such file or directory

解决方法:

sudo apt update
sudo apt install libgl1

参考

  • https://dev.to/shuv1824/deploy-fastapi-application-on-ubuntu-with-nginx-gunicorn-and-uvicorn-3mbl
  • https://blog.nawaz.info/deploy-fastapi-application-on-ubuntu-with-nginx-gunicorn-and-uvicorn

tags: systemd fastapi

爱评论不评论