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

发布时间: 2022-02-24 22:28:21 作者: 大象笔记

测试开发环境

uvicorn server:app --reload

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

uvicorn 是什么

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

与 Gunicorn 一同使用

参考 uvicorn 及 FastAPI 的官方文档:

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

示例:

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

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

参考

我是一名山东烟台的开发者,联系作者