為什么生產(chǎn)上不使用Web框架自帶的服務
Python有內(nèi)置的WSGI服務器,用來做Python Web開發(fā)的框架,例如Flask,Django等,也會對WSGI做實現(xiàn)。因此寫完代碼,想要調(diào)試的時候,就可以使用Python內(nèi)置的,或者Web框架提供的WSGI服務器。
既然這樣,為什么生產(chǎn)環(huán)境的部署,和我們調(diào)試時的方式不太一樣呢?
因為不管是Python內(nèi)置的,還是Web框架的服務,它們都不是專注于實現(xiàn)服務器的,只是提供了一個可用的功能,卻并不適合生產(chǎn)上復雜的環(huán)境。
舉個簡單的例子:生產(chǎn)上用戶訪問眾多,那就來看看Flask自帶的服務器性能表現(xiàn)如何。
進行壓測,我這里只用了10用戶并發(fā)循環(huán)訪問10次(大家也可以自己試試),這個服務就掛了。說明Flask框架提供的WSGI服務器并不能承受很大的壓力,如果生產(chǎn)上這樣部署,一定會出問題的。
生產(chǎn)環(huán)境的Web部署
所以本文主要講講生產(chǎn)環(huán)境上的部署,這里使用的是Nginx+Gunicorn+Flask+Supervisor的方式。
nginx 不用多說,一個高性能的web服務器。
webservice常見的有FastCGI,WSGI等。我們這里使用gunicorn為WSGI容器。使用Python編寫服務,采用Flask框架。同時采用supervisor管理服務器進程。
安裝 gunicorn
pip install gunicorn
pip 是一個重要的工具,python 用來管理包。每次使用 pip 安裝的庫,都寫入一個 requirement 文件里面,既能知道自己安裝了什么庫,也方便別人部署時,安裝相應的庫。
pip freeze > requirements.txt
以后每次 pip 安裝了新的庫的時候,都需freeze 一次。
在我的manage.py文件里面,有如下代碼:
if __name__ == '__main__':
app.run(host = '0.0.0.0' ,port = 8001, debug=True)
這個含義是用 flask 自帶的服務器啟動 app。當我們使用gunicorn 啟動 flask時,manage.py 就等同于一個庫文件,被 gunicorn 調(diào)用。
所以gunicorn后面的參數(shù)如下:
gunicorn -w4 -b0.0.0.0:8001 manage:app
gunicron就是替代Flask自帶的一個web server。
其中 gunicorn 的部署中,-w 表示開啟多少個 worker,-b 表示 gunicorn開發(fā)的訪問地址。
想要結(jié)束 gunicorn 只需執(zhí)行 pkill gunicorn,有時候還要用 ps 找到 pid 進程號才能 kill??墒沁@對于一個開發(fā)來說,太過于繁瑣,因此出現(xiàn)了另外一個神器---supervisor
,一個專門用來管理進程的工具,還可以管理系統(tǒng)的工具進程。
安裝supervisor
pip install supervisor
echo_supervisord_conf > supervisor.conf # 生成 supervisor 默認配置文件
vim supervisor.conf # 修改 supervisor 配置文件,添加 gunicorn 進程管理
在supervisor.conf 配置文件底部添加 (其中,我的工作路徑是/Users/cynthia/Codes/project1,我的虛擬環(huán)境是用conda建立的,是在其安裝目錄下的envs下)
[program: project1]
command=/Users/cynthia/miniconda3/envs/project1/bin/gunicorn -w4 -b0.0.0.0:8001 project1:app ; supervisor啟動命令
directory=/Users/cynthia/Codes/testwk ; 項目的文件夾路徑
startsecs=0 ; 啟動時間
stopwaitsecs=0 ; 終止等待時間
autostart=false ; 是否自動啟動
autorestart=false ; 是否自動重啟
stdout_logfile=/Users/cynthia/Codes/project1/log/gunicorn.log ; log 日志
stderr_logfile=/Users/cynthia/Codes/project1/log/gunicorn.err ; 錯誤日志
supervisor的基本使用命令
supervisord -c supervisor.conf 通過配置文件啟動supervisor
supervisorctl -c supervisor.conf status 察看supervisor的狀態(tài)
supervisorctl -c supervisor.conf reload 重新載入 配置文件
supervisorctl -c supervisor.conf start [all]|[appname] 啟動指定/所有 supervisor管理的程序進程
supervisorctl -c supervisor.conf stop [all]|[appname] 關閉指定/所有 supervisor管理的程序進程
supervisor 還有一個web的管理界面,可以激活。在supervisor.conf 配置文件中找到這兩段,并激活。
[inet_http_server] ; inet (TCP) server disabled by default
port=127.0.0.1:9001 ; ip_address:port specifier, *:port for all iface
username=user ; default is no username (open server)
password=123 ; default is no password (open server)
……
[supervisorctl]
serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL for a unix socket
serverurl=http://127.0.0.1:9001 ; use an http:// url to specify an inet socket
username= user ; should be same as in [*_http_server] if set
password=123 ; should be same as in [*_http_server] if set
;prompt=mysupervisor ; cmd line prompt (default "supervisor")
;history_file=~/.sc_history ; use readline history if available
進行簡單的配置,就可以使用 supervsior 啟動 gunicorn啦。運行命令 supervisord -c supervisor.conf
訪問 http://127.0.0.1:9001 可以得到 supervisor的web管理界面,訪問 [http://127.0.0.1:8001](https://link.jianshu.com?t=http://127.0.0.1: 8001) 可以看見gunciron 啟動的返回的結(jié)果
安裝配置 nginx
一般是這樣安裝:
sudo apt-get install nginx
可以參考http://www.cnblogs.com/Ray-liang/p/4837850.html進行部署
但是我是Mac,所以通過brew來安裝,如果沒有的話請先安裝brew:
brew install nginx
安裝之后,我本機的相關路徑如下:
/usr/local/etc/nginx/nginx.conf #nginx 的配置文件
/usr/local/Cellar/nginx/1.15.11/bin #啟動nginx的路徑
nginx相關命令
nginx -t #檢查配置文件ngnix.conf的正確性命令
nginx -s reload # 重新載入配置文件
nginx -s reopen # 重啟 Nginx
nginx -s stop # 停止 Nginx
使用 supervisor 來管理 nginx。增加下面的配置文件
[program:nginx]
command=/usr/local/Cellar/nginx/1.15.11/bin/nginx
startsecs=0
stopwaitsecs=0
autostart=false
autorestart=false
stdout_logfile=/Users/cynthia/Codes/project1/log/nginx.log
stderr_logfile=/Users/cynthia/Codes/project1/log/nginx.err
至于為啥要用ngnix:
有了gunicorn,為何還需要nginx
小結(jié)
- 寫Flask Web程序,調(diào)試時可以運行在自帶的web server上
- 在生產(chǎn)環(huán)境的服務器上,使用gunicorn
- 外面套一層ngnix服務器,好處上文有說