声明,本博客写给自己看的,相当于云笔记,亲爱的陌生人请勿尝试!!!
所需环境
- centos7
- gunicorn
- nginx
- supervisor
升级Python到3.7.6
1.从官网上下载安装包
wget https://www.python.org/ftp/python/3.7.6/Python-3.7.6.tgz
2.解压缩
tar -xvf Python-3.7.6.tgz
3.创建安装目录
mkdir /usr/local/python3
4.编译
cd Python-3.7.6
./configure --prefix=/usr/local/python3
make all
make install
可能会缺少库,如zlib
,缺少什么安装什么
yum -y install zlib*
# 下面的命令把可能用到的依赖都装了
yum install gcc-c++ gcc make cmake zlib-devel bzip2-devel openssl-devel ncurse-devel libffi-devel -y
5.进入安装的绝对路径,检查是否安装成功
/usr/local/python3/bin/python3.6 -V
6.修改软连接,让Python指向Python3.7.6
更改原来的软连接
mv /usr/bin/python /usr/bin/python2.7.5
让Python指向新安装的Python
ln -s /usr/local/python3/bin/python3 /usr/bin/python
7.修改yum,让它指向就Python
sudo vim /usr/bin/yum
修改#! /usr/bin/python
为#! /usr/bin/python2.7.5
gunicorn
-
安装gunicorn
pip安装gunicorn
pip install gunicorn
其实不需要nginx,gunicorn便可运行简单的flask应用,创建如下文件
#main.py from flask import Flask app = Flask(__name__) @app.route('/') def index(): return 'hello world' if __name__ == '__main__': app.run()
然后运行gunicorn
gunicorn -w 4 main:app -b 0.0.0.0:8000
可能会出现gunicorn command not found, 需要添加软连接
ln -s /usr/local/python3/bin/gunicorn /usr/bin/gunicorn
也可能出现[CRITICAL] WORKER TIMEOUT (pid:25867)
比如我在运行深度学习模型的时候,加载数据会话费大量的时间,因此需要增大gunicorn的超时时间
gunicorn -w 4 main:app -b 0.0.0.0:8000 -t 600
设置超时时间为600秒便可在浏览器中看到hello world信息了
但是gunicorn对静态文件支持不好,所以仍需要使用nginx做反向代理 -
gunicorn关闭操作如下
pstree -ap|grep gunicorn
得到以下结果
| |-grep,4698 --color=auto gunicorn | `-gunicorn,4238 /usr/bin/gunicorn -w 4 run_app:app -b 0.0.0.0:8000 | |-gunicorn,4243 /usr/bin/gunicorn -w 4 run_app:app -b 0.0.0.0:8000 | |-gunicorn,4248 /usr/bin/gunicorn -w 4 run_app:app -b 0.0.0.0:8000 | |-gunicorn,4249 /usr/bin/gunicorn -w 4 run_app:app -b 0.0.0.0:8000 | `-gunicorn,4250 /usr/bin/gunicorn -w 4 run_app:app -b 0.0.0.0:8000
重启gunicorn
kill -HUP 4238
关闭gunicorn
kill -9 4238
nginx
-
安装nginx
sudo yum install nginx -y
-
启动nginx
service nginx start
-
配置nginx
向/etc/nginx/nginx.conf
添加如下内容location / { proxy_pass http://127.0.0.1:8000; proxy_redirect off; proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; }
-
重新启动nginx
service nginx restart
-
再次启动gunicorn便可看到flask应用已经在运行了
supervisor
supervisor能守护进程,当gunicorn挂了之后,能自动重启
- 安装supervisor
每个应用单独写一个conf,再由supervisord.conf导入easy_install supervisor mkdir /etc/supervisor echo_supervisord_conf > /etc/supervisor/supervisord.conf mkdir /etc/supervisor/conf.d
- 修改supervisor.conf,在末尾加上
[include] files = /etc/supervisor/conf.d/app.conf # your_app.conf为你的app配置
- 添加app.conf
此处的message_app为groram_name[program:message_app] directory=/home/message command=gunicorn -w 4 run_app:app -b 0.0.0.0:8000
- 启动supervisor
sudo supervisord -c supervisord.conf #添加配置文件 sudo supervisorctl start message_app #启动应用
- 常用命令
sudo supervisorctl reload #修改配置文件后重新加载 sudo supervisorctl start app #启动app sudo supervisorctl stop app #停止app sudo supervisorctl restart app #重启app