python web 部署:nginx + gunicorn + supervisor + flask 部署筆記

python web 部署

轉自:http://www.jianshu.com/p/be9dd421fb8d

web開發中,各種語言爭奇鬥豔,web的部署方面,卻沒有太多的方式。簡單而已,大概都是 nginx 做前端代理,中間 webservice 調用程序腳本。大概方式:nginx + webservice + script

nginx 不用多說,一個高性能的web服務器。通常用來在前端做反向代理服務器。所謂正向與反向(reverse),只是英文說法翻譯。代理服務,簡而言之,一個請求經過代理服務器從局域網發出,然後到達互聯網上服務器,這個過程的代理爲正向代理。如果一個請求,從互聯網過來,先進入代理服務器,再由代理服務器轉發給局域網的目標服務器,這個時候,代理服務器爲反向代理(相對正向而言)。

正向代理:{ 客戶端 ---》 代理服務器 } ---》 服務器

反向代理:客戶端 ---》 { 代理服務器 ---》 服務器 }

{} 表示局域網

nginx既可以做正向,也可以做反向。
webservice 的方式同樣也有很多方式。常見的有FastCGIWSGI等。我們採用gunicorn爲 wsgi容器。python爲服務器script,採用flask框架。同時採用supervisor管理服務器進程。也就是最終的部署方式爲:
nginx + gunicorn + flask ++ supervisor

創建一個項目

mkdir myproject

創建 python 虛擬環境

virtualenv 可以說是 python 的一個大殺器。用來在一個系統中創建不同的 python 隔離環境。相互之間還不會影響,使用簡單到令人髮指。(我的工作路徑是/home/rsj217/rsj217

mkdir myproject
cd myproject
virtualenv venv

創建了 venv 環境之後,激活就可以了

source venv/bin/activate

安裝 python web 框架 ---flask

flask 是一個 python web micro framework。簡潔高效,使用也很簡單。flask 依賴兩個庫werkzeugjinjia2。採用 pip 方式安裝即可。

pip install flask

測試我們的 flask 安裝是否成功,並使用 flask 寫一個簡單的 web 服務。
vim myapp.py

from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
    return 'hello world'
if __name__ == '__main__':
    app.debug = True
    app.run()

啓動 flask

python myapp.py

此時,用瀏覽器訪問 http://127.0.0.1:5000 就能看到網頁顯示 hello world

使用 gunicorn 部署 python web

現在我們使用 flask 自帶的服務器,完成了 web 服務的啓動。生產環境下,flask 自帶的 服務器,無法滿足性能要求。我們這裏採用 gunicorn 做 wsgi容器,用來部署 python。

安裝 gunicorn

 pip install gunicorn

pip 是一個重要的工具,python 用來管理包。還有一個最佳生產就是每次使用 pip 安裝的庫,都寫入一個 requirement 文件裏面,既能知道自己安裝了什麼庫,也方便別人部署時,安裝相應的庫。

 pip freeze > requirements.txt

以後每次 pip 安裝了新的庫的時候,都需freeze 一次。

當我們安裝好 gunicorn 之後,需要用 gunicorn 啓動 flask,注意 flask 裏面的name裏面的代碼啓動了 app.run(),這個含義是用 flask 自帶的服務器啓動 app。這裏我們使用了 gunicorn,myapp.py 就等同於一個庫文件,被 gunicorn 調用。

 gunicron -w4 -b0.0.0.0:8000 myapp:app

此時,我們需要用 8000 的端口進行訪問,原先的5000並沒有啓用。其中 gunicorn 的部署中,,-w 表示開啓多少個 worker,-b 表示 gunicorn 開發的訪問地址。

想要結束 gunicorn 只需執行 pkill gunicorn,有時候還的 ps 找到 pid 進程號才能 kill。可是這對於一個開發來說,太過於繁瑣,因此出現了另外一個神器---supervisor,一個專門用來管理進程的工具,還可以管理系統的工具進程。

安裝 supervisor

pip install supervisor
echo_supervisord_conf > supervisor.conf   # 生成 supervisor 默認配置文件
vim supervisor.conf                       # 修改 supervisor 配置文件,添加 gunicorn 進程管理

在myapp supervisor.conf 配置文件底部添加 (注意我的工作路徑是/home/rsj217/rsj217/)

[program:myapp]
command=/home/rsj217/rsj217/myproject/venv/bin/gunicorn -w4 -b0.0.0.0:2170 myapp:app    ; supervisor啓動命令
directory=/home/rsj217/rsj217/myproject                                                 ; 項目的文件夾路徑
startsecs=0                                                                             ; 啓動時間
stopwaitsecs=0                                                                          ; 終止等待時間
autostart=false                                                                         ; 是否自動啓動
autorestart=false                                                                       ; 是否自動重啓
stdout_logfile=/home/rsj217/rsj217/myproject/log/gunicorn.log                           ; log 日誌
stderr_logfile=/home/rsj217/rsj217/myproject/log/gunicorn.err                           ; 錯誤日誌

supervisor的基本使用命令

supervisord -c supervisor.conf                             通過配置文件啓動supervisor
supervisorctl -c supervisor.conf status                    察看supervisor的狀態
supervisorctl -c supervisor.conf reload                    重新載入 配置文件
supervisorctl -c supervisor.conf start [all]|[appname]     啓動指定/所有 supervisor管理的程序進程
supervisorctl -c supervisor.conf stop [all]|[appname]      關閉指定/所有 supervisor管理的程序進程

supervisor 還有一個web的管理界面,可以激活。更改下配置

[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 http_username if set
password=123                ; should be same as http_password 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:2170 可以看見gunciron 啓動的返回的 hello world

安裝配置 nginx

採用 apt-get方式安裝最簡單。運行 sudo apt-get install nginx。安裝好的nginx的二進制文件放在/usr/sbin/文件夾下面。而nginx的配置文件放在 /etc/nginx下面。

使用 supervisor 來管理 nginx。這裏需要注意一個問題,linux的權限問題。nginx是sudo的方式安裝,啓動的適合也是 root用戶,那麼我們現在也需要用 root用戶啓動supervisor。增加下面的配置文件

[program:nginx]
command=/usr/sbin/nginx
startsecs=0
stopwaitsecs=0
autostart=false
autorestart=false
stdout_logfile=/home/rsj217/rsj217/myproject/log/nginx.log
stderr_logfile=/home/rsj217/rsj217/myproject/log/nginx.err

到此爲止,進步的 web 部屬已經完成。當然,最終我們需要把項目代碼部屬到服務器上.批量的自動化部屬需要另外一個神器 fabric.具體使用,就不再這篇筆記闡述。項目源碼中包含了fabric文件。下載fabric,更改裏面的用戶名和祕密,就可以部屬在自己或者遠程的服務器上了。

項目源碼: https://coding.net/u/rsj217/p/myproject/git

發佈了14 篇原創文章 · 獲贊 30 · 訪問量 15萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章