python web 部署方式為:nginx + gunicorn + supervisor + flask
一、準備工作,先來安裝pip
詳細教程如:
1、首先檢查linux有沒有安裝python-pip包,直接執行 yum install python-pip
※顯示No package python-pip available. 則沒有,繼續如下操作
2、沒有python-pip包就執行命令 yum -y install epel-release
3、執行成功之后,再次執行yum install python-pip
4,、對安裝好的pip進行升級 pip install --upgrade pip
至此,pip工具就安裝好了。
二、創建一個項目和 python 虛擬環境
使用python的virtualenv創建虛擬環境 。用來在一個系統中創建不同的 python 隔離環境。相互之間還不會影響,而且使用相當簡單。
1
2
3
|
mkdir myflask cd myflask virtualenv venv |
創建了 venv 環境之后,激活就可以了
1
|
source venv/bin/activate |
三、安裝 python web 框架 — flask
flask 是一個 python web 輕型框架,簡潔高效。flask 依賴兩個庫 werkzeug 和 jinjia2。可采用 pip 方式安裝:
1
|
pip install flask |
測試我們的 flask 安裝是否成功,并使用 flask 寫一個簡單的 web 服務。
vim run.py
1
2
3
4
5
6
7
8
9
10
|
from flask import Flask app = Flask(__name__) @app .route( '/' ) def index(): return 'hello world!' if __name__ = = '__main__' : app.run() |
啟動 flask
1
|
python run.py |
此時,用瀏覽器訪問 http://127.0.0.1:5000 就能看到網頁顯示 hello world!
三、使用 gunicorn 部署 python web
現在我們使用 flask 自帶的服務器,完成了 web 服務的啟動。生產環境下,flask 自帶的 服務器,無法滿足性能要求。我們這里采用 gunicorn 做 wsgi容器,用來部署 python,用pip直接安裝。
1
|
pip install gunicorn |
pip 是python 用來管理包的一個重要工具。每次安裝新庫后寫入一個 requirement 文件里面,既能知道自己安裝了什么庫,也方便別人部署時,安裝相應的庫。
1
|
pip freeze > requirements.txt |
以后每次 pip 安裝了新的庫的時候,都需freeze 一次。完整保存好requirement文本,重新安裝庫則只需要執行如下操作:
1
|
pip install - r requirements.txt |
當我們安裝好 gunicorn 之后,需要用 gunicorn 啟動 flask,注意 flask 里面的name里面的代碼啟動了 app.run(),這個含義是用 flask 自帶的服務器啟動 app。這里我們使用了 gunicorn,run.py 就等同于一個庫文件,被 gunicorn 調用。
1
|
gunicorn -w4 -b0.0.0.0:5000 run:app #-w worker數量 -b 訪問地址 |
此時,我們可以用 5000 的端口進行訪問。
想要結束 gunicorn 只需執行 pkill gunicorn,有時候還需要用 ps 找到 pid 進程號才能 kill。
四、使用nginx
nginx,一個高性能的web服務器。通常用來在前端做反向代理服務器。代理服務,簡而言之,一個請求經過代理服務器從局域網發出,然后到達互聯網上服務器,這個過程的代理為正向代理。如果一個請求,從互聯網過來,先進入代理服務器,再由代理服務器轉發給局域網的目標服務器,這個時候,代理服務器為反向代理(相對正向而言)。
1. gcc 安裝
安裝 nginx 需要先將官網下載的源碼進行編譯,編譯依賴 gcc 環境,如果沒有 gcc 環境,則需要安裝:
1
|
yum install gcc-c++ |
2. PCRE pcre-devel 安裝
PCRE(Perl Compatible Regular Expressions) 是一個Perl庫,包括 perl 兼容的正則表達式庫。nginx 的 http 模塊使用 pcre 來解析正則表達式,所以需要在 linux 上安裝 pcre 庫,pcre-devel 是使用 pcre 開發的一個二次開發庫。nginx也需要此庫。命令:
1
|
yum install -y pcre pcre-devel |
3. zlib 安裝
zlib 庫提供了很多種壓縮和解壓縮的方式, nginx 使用 zlib 對 http 包的內容進行 gzip ,所以需要在 Centos 上安裝 zlib 庫。
1
|
yum install -y zlib zlib-devel |
4. OpenSSL 安裝
OpenSSL 是一個強大的安全套接字層密碼庫,囊括主要的密碼算法、常用的密鑰和證書封裝管理功能及 SSL 協議,并提供豐富的應用程序供測試或其它目的使用。
nginx 不僅支持 http 協議,還支持 https(即在ssl協議上傳輸http),所以需要在 Centos 安裝 OpenSSL 庫。
1
|
yum install -y openssl openssl-devel |
5. 使用wget下載nginx:
1
2
3
4
5
|
wget https: //nginx .org /download/nginx-1 .13.12. tar .gz tar -zxvf nginx-1.13.12. tar .gz cd nginx-1.13.12 make make install #編譯安裝 |
然后配置Nginx,剛才安裝了Nginx之后,我們打開/etc/nginx/conf.d/default.conf,然后修改默認的default.conf為:
1
2
3
4
5
6
7
8
9
10
11
12
|
server { listen 80; server_name localhost; location / { proxy_pass http: //127 .0.0.1:5000; proxy_redirect off; proxy_set_header Host $host:80; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } |
其中server_name就是你的域名,這里用localhost代表通過ip訪問,配置好default.conf之后試著啟動Nginx!
1
2
3
|
[root@server ~] # service nginx start Starting nginx: [ OK ] [root@server ~] # nginx -s reload |
ok!到這一步,整個部署過程就完成了!
六、為了方便管理使用supervisor
1.安裝 supervisor
1
2
3
|
pip install supervisor echo_supervisord_conf > supervisor.conf # 生成 supervisor 默認配置文件 vim supervisor.conf # 修改 supervisor 配置文件,添加 gunicorn和nginx |
2.添加gunicorn到supervisor中,添加到配置最下面。此處因為使用了python虛擬環境,可先在虛擬環境找到gunicorn地址,使用whereis 如:/usr/local/bin/gunicorn,記住此位置加入到supervisor的command中,如下:
1
2
3
4
5
6
7
8
9
|
【program:myflask】 command = /usr/local/bin/gunicorn -w4 -b0.0.0.0:5000 run:app directory = /home/myflask #位置 autostart = true #自動啟動 startsecs = 5 autorestart = true #自動重啟 startretries = 3 #啟動失敗時的最多重試次數 redirect_stderr = true #重定向stderr到stdout stdout_logfile = /var/log/flask_supervisor.log |
3、添加nginx進程到supervisor
先新建一個log文件夾
1
|
mkdir /home/myflask/log |
1
2
3
4
5
6
7
8
9
10
11
|
[program:up_nginx] command = /usr/sbin/nginx autostart = true #隨著supervisord的啟動而啟動 autorestart = true #自動重啟 startretries = 10 #啟動失敗時的最多重試次數 exitcodes = 0 #正常退出代碼 stopsignal = KILL #用來殺死進程的信號 stopwaitsecs = 10 #發送SIGKILL前的等待時間 redirect_stderr = true #重定向stderr到stdout stdout_logfile = /home/myflask/log/nginx.log stdout_logfile = /home/myflask/log/nginx.err |
4.啟用supervisor管理工具
1
2
3
|
supervisord -c supervisord.conf #sudo unlink /tmp/supervisor.sock #如上面啟動失敗先使用此命令之后在使用上面命令 supervisorctl start all |
至此所有配置結束,試試網站是否如正常運行吧,還可增加supervisor開機自啟動(這個未試驗,大家試試看吧):
1
2
3
4
5
|
#開機任務自動啟動 vi /etc/rc .d /rc . local #并在最后添加 supervisord -c /home/myflask/supervisord .conf #尾聲:通過以上步驟,那么基本上就可以開啟啟動網站了!reboot試一試~~ |
※supervisor的基本使用命令
1
2
3
4
5
|
supervisord -c supervisor.conf #通過配置文件啟動supervisor supervisorctl status #察看supervisor的狀態 supervisorctl reload #重新載入 配置文件 supervisorctl start [all]|[appname] #啟動指定/所有 supervisor管理的程序進程 supervisorctl stop [all]|[appname] #關閉指定/所有 supervisor管理的程序進程 |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://www.cnblogs.com/lethon/p/8808368.html