python flask 生产环境部署,基于gunicorn(centos 7以上)
1.安装gunicorn,部分生产服务器会存在多个pip版本,一般用pip和pip3区分,本文中用pip3
pip3 install gunicorn
2.启动程序
cd /usr/app # 这里是项目启动文件的路径
gunicorn --workers 2 -b 0.0.0.0:5005 app2:app --timeout 60
因为我的项目文件名称为app2.py所以为app2:如果是其他的文件名称注意修改,验证项目正常后继续如下操作
如果项目文件为app.py 则命令如下:
gunicorn --workers 2 -b 0.0.0.0:5006 app:app --timeout 60
3.配置gunicorn配置文件
查看centos版本
cat /etc/redhat-release
# 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 --timeout 60 # 错误重启 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
# 列出所有服务
systemctl list-unit-files
浙公网安备 33010602011771号