【Flask】Nginx+Gunicorn+Supervisor部署一个Flask项目:步骤总结
為什么要使用gunicorn?
Flask 作為一個 Web 框架,內置了一個 webserver, 但這自帶的 Server 到底能不能用?
官網的介紹:
While lightweight and easy to use, Flask’s built-in server is not suitable for production as it doesn’t scale well. Some of the options available for properly running Flask in production are documented here.
很顯然,內置的 webserver 是能用的。但不適合放在生產環境。這個 server 本來就是給開發者用的。框架本身并不提供生產環境的 web 服務器,SpringBoot 這種內置 Tomcat 生產級服務器 是例外。
假設我們使用的是 Nginx+Flask Run 來當作生產環境,全部部署在一臺機器上。劣勢如下:
只有一個進程在跑所有的請求,而由于實現的簡陋性,內置 webserver 很容易卡死。并且只有一個 Worker 在跑請求。在多核 CPU 下,僅僅占用一核。當然,其實也可以多起幾個進程。
接上,加入負載量上來了,Gunicorn 可以調節 Worker 的數量。而這個東西,內置的 Webserver 是不適合做這種事情的。一言以蔽之,太弱,幾個請求就打滿了。
Gunicorn 作為 Server 相對而言可以有什么提升?
搭建步驟
本文是簡化版的步驟描述。詳細步驟見:【Flask】Nginx / Gunicorn入門:部署你的Flask項目
環境:Centos7,python2.7+nginx+Flask +Gunicorn+mysql5.7
1、安裝Gunicorn
虛擬機里面安裝完成Gunicorn后,用flask寫一個python文件test.py
from flask import Flaskapp = Flask(__name__)@app.route('/') def hello_world():return 'Hello World,test python!'if __name__ == '__main__':from werkzeug.contrib.fixers import ProxyFixapp.wsgi_app = ProxyFix(app.wsgi_app)app.run()使用gunicorn啟動這個python文件,命令是:gunicorn test:app,然后就能在虛擬機里瀏覽器訪問127.0.0.1:8000,看到這個頁面。
2、安裝nginx
虛擬機里面安裝完成nginx后,修改配置文件,端口改成8089(避免和Apache沖突,但是這樣在訪問的時候需要寫上端口號,有時候跳轉之后又會變成默認的80端口,還要手動修改,暫時未解決,還是用默認的80端口吧…),增加下面內容:
然后./nginx -s reload 重新加載配置,虛擬機里用ifconfig查一下ip(我用的橋接模式),在實體機訪問虛擬機ip:8089即可看到flask的頁面
普通的運行nginx
su root切換管理員
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf指定配置文件并啟動nginx
./nginx直接啟動nginx
3、項目的部署
同樣的原理,把項目文件整個拷貝到虛擬機中,命令gunicorn 文件名:app啟動后,可以在127訪問
切換到nginx目錄下,以下命令運行ngnix:
su root,切換管理員權限
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf,設置配置文件路徑
./nginx,啟動nginx
虛擬機里用ifconfig查一下ip(不知道為什么連接家里wifi只能用橋接,連接校園網只能用NAT),在實體機訪問虛擬機ip:端口號,即可看到項目運行的頁面
4、使用supervisor管理Nginx+Gunicorn
supervisor是一個制作守護進程的工具,用戶可以在UNIX系統中監控、管理進程。
常用于管理與某個用戶或項目相關的進程。
去幫我們維護各種服務器的進程,即使有軟件崩了也能幫我們自動重啟。
用于在生產環境中,控制項目涉及的軟件的進程。
如果不使用supervisor,重啟gunicorn的步驟是:
pstree -ap|grep gunicorn
kill -9(gunicorn pid)
gunicorn -w6 -b 127.0.0.1:80 demo:app
使用supervisor
安裝:pip install supervisor
生成配置文件:echo_supervisord_conf > /etc/supervisord.conf(默認是沒有配置文件的)
在配置文件/etc/supervisord.conf中添加:
開啟supervisor,然后就可以訪問部署后的網站了。
supervisord -c /etc/supervisord.conf
查看進程狀態:
supervisorctl status
更新項目文件后,重啟supervisor命令:
supervisorctl restart all
supervisor 常用命令
一、開啟命令
supervisor的服務器端部分啟動命令: sudo unlink /var/run/supervisor.sock(執行第二行產生文件后執行) supervisord -c /etc/supervisord.conf 此時默認開啟了所有服務supervisor的客戶端部分命令: supervisorctl status 查看進程運行狀態 supervisorctl start 進程名 啟動進程 supervisorctl stop 進程名 關閉進程 supervisorctl restart 進程名 重啟進程 supervisorctl update 重新載入配置文件 supervisorctl shutdown 關閉supervisord supervisorctl clear 進程名 清空進程日志 supervisorctl 進入到交互模式下。使用help查看所有命令。例如: supervisorctl start all 啟動所有進程supervisorctl restart all 重啟所有進程supervisorctl stop all 停止所有進程二、關閉命令
supervisorctl stop all 先關閉supervisor服務 kill -9 pid 之后再關閉supervisord服務踩坑記錄
①
正常啟動Nginx后,在實體機瀏覽器輸入ip:8089報出:xxx拒絕了我們的連接請求。但是虛擬機127.0.0.1:8089可以訪問
原因:實體機瀏覽器在進行頁面跳轉后,又回到了默認使用80端口,在url中手動輸入一下端口號就好了。這是原因。解決方式待定。暫定解決方式為改回到默認的80端口。或者好像在py文件里指定一下端口。
②
解決方法
find / -name supervisor.sock
unlink /name/supervisor.sock
③
頁面可以正常訪問,但是看了一眼log,ERROR:端口被占用。
這是因為啟動了好幾個gunicorn。
ps ax|grep gunicorn查看gunicorn進程
關閉 supervisor 之后,停掉所有的 gunicorn 進程,再重新啟動 supervisor 即可。
kill -9 15357停止指定進程號
ps ax|grep nginx查看nginx進程
還是有gunicorn進程?gunicorn殺不掉??只好重啟一下機器。。原因未知。
④supervisor 監控nginx 一直在重啟的問題
supervisor 監控nginx ,寫好配置文件之后,發現一直在重啟,排查之后發現是命令不對:
command = /usr/local/bin/nginx 這個命令默認是后臺啟動,但是supervisor不能監控后臺程序,所以supervisor就一直執行這個命令。
加上-g 'daemon off;'這個參數可解決這問題,這個參數的意思是在前臺運行。
command = /usr/local/bin/nginx -g ‘daemon off;’
(轉載)完整的supervisor 監控nginx 配置如下:
[program:nginx]command = /usr/local/bin/nginx -g 'daemon off;' stdout_logfile=/Users/ddios/nginx_stdout.log stdout_logfile_maxbytes=10MB stderr_logfile=/Users/ddios/nginx_stderr.log user = ddios stderr_logfile_maxbytes=10MB autostart=true autorestart=true總結
以上是生活随笔為你收集整理的【Flask】Nginx+Gunicorn+Supervisor部署一个Flask项目:步骤总结的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Flask】Nginx / Gunic
- 下一篇: 【Nginx】Nginx配置文件参数/启