flask的部署:gunicorn + supervisor + flask

flask的部署:gunicorn + supervisor + flask

背景:使用flask部署了一个发送请求的小工具给RD使用,部署在开发机上,但是提供外部使用时不太适合使用flask自带的web服务器,在这采用gunicorn + supervisor + flask这个组合

工具作用:

  • flask:web框架,都知道的
  • gunicorn:一个python使用的web服务器 gunicorn介绍
  • supervisor:进程管理工具,通过该工具管理gunicorn的运行,根据配置文件启动项目,还可以查看状态,停止等操作

软件安装

1、安装flask

pip install flask

2、安装gunicorn:版本问题需要注意

# python2的安装
python -m pip install gunicorn # 指定python版本,我的python默认是2.7

# python3的安装:一般机器上带有2套环境,默认是3
pip3 install gunicorn
或者 
pip install gunicorn

3、安装supervisor:也需要注意版本问题,如果用的py2,都要安装2的版本

# 1、安装
pip install supervisor   

# 2、生成默认的配置文件 建议放在自建配置目录里
cd /etc
mkdir supervisor
cd supervisor
echo_supervisord_conf > supervisor.conf # 生成 supervisor 默认配置文件,后续的操作都在该conf文件里

软件使用

1、supervisor.conf 配置文件底部添加:这个配置文件最好放在项目目录下,和启动py文件在一起

[program:myapp]
command=gunicorn -w 4 -b 0.0.0.0:2170 myapp:app               ; supervisor启动命令
directory=/home/wkk/python/sender                                    ; 项目的文件夹路径
startsecs=0                                                                             ; 启动时间
stopwaitsecs=0                                                                          ; 终止等待时间
autostart=false                                                                         ; 是否自动启动
autorestart=false                                                                       ; 是否自动重启
stdout_logfile=/home/wkk/python/sender/log/gunicorn.log                           ; log 日志
stderr_logfile=/home/wkk/python/sender/log/gunicorn.err        

2、启动supervisor,通过supervisor来管理 gunicorn,这样就flask项目启动完成

# 启动文件,查看到项目 runing
supervisord -c supervisord.conf 
posted @ 2022-02-24 13:11  alisleepy  阅读(507)  评论(0)    收藏  举报