python flask 生产环境部署,基于gunicorn

发布时间 2023-12-15 18:33:06作者: 峡谷风

1.安装gunicorn,部分生产服务器会存在多个pip版本,一般用pip和pip3区分,本文中用pip

pip install gunicorn

2.启动程序

cd /usr/app
gunicorn --workers 2 -b 0.0.0.0:5056 app:app

 验证项目正常后继续如下操作

3.配置gunicorn配置文件

查看centos版本

cat /etc/redhat-release
# centos 6.5 
cd /etc/init.d
# centos 8
cd /etc/systemd/system

新建一个配置文件fanxing.service

[Unit]
# 描述
Description=Gunicorn
# 在网络服务启动后再启动
After=network.target
 
[Service]
# 指定运行服务的用户
User=root
# 指定运行服务的用户组
Group=root
# 为服务指定环境变量
Environment="Path=/usr/app"
# 项目文件目录
WorkingDirectory=/usr/app
# gunicorn启动命令
ExecStart=gunicorn --workers 2 -b 0.0.0.0:5056 app:app
# 错误重启
Restart=on-failure
 
[Install]
WantedBy=multi-user.target

注意上边可以用whoami,查看当前用户,groups查看当前用户组

4.启动命令

# 重新加载配置文件
sudo systemctl daemon-reload
# 开启服务
sudo systemctl start fanxing.service
# 查看服务状态
sudo systemctl status fanxing.service
# 设置开机启动
sudo systemctl enable fanxing.service