uvicorn 框架软件编写后发布至ubuntu服务器
uvicorn安装使用
- 1.安装
1 | pip install uvicorn |
- 2.main.py编写
1 | from fastapi import FastApi |
- 3.开发环境运行
1 | if __name__ == '__main__': |
发布环境部署
- 1.gunicorn安装
1 | pip install uvicorn |
- 2.gunicorn.py配置文件编写
1 | import os |
- 3.py文件特殊处理
1 | # 导入OS模块 |
- 4.服务器运行
1 | gunicorn main:app -c gunicorn.py |
抑或
1 | gunicorn main:app -b 0.0.0.0:8000 -w 4 -k uvicorn.workers.UvicornWorker --daemon |
项目停止运行
- 1.获取gunicorn进程树
1 | pstree -ap | grep gunicorn |
- 2.终止gunicorn任务
kill -HUP 进程pid
- 3.如果使用了多进程,那么执行了上述命令后还会有子进程在运行,可以使用如下命令杀死
1 | kill -9 进程pid |
进程脚本杀死
1 |
|
- 脚本执行
1 | chmod 777 stop_gunicorn.sh |
配置开机自启动服务
配置 gunicorn.service服务开机自启动
1 | cat >/usr/lib/systemd/system/gunicorn.service << EOF |
依次执行下面命令
1 | systemctl daemon-reload |
查看服务状态
1 | systemctl status gunicorn |
Nginx代理配置
1 | server { |
nginx配置生效
1 | nginx -s reload |