声明,本博客写给自己看的,相当于云笔记,亲爱的陌生人请勿尝试!!!
所需环境
- gunicorn
- Nginx
- supervisor
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
便可在浏览器中看到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 supervisor -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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。