29. Flask 部署項目 nginx + gunicorn + flask

部署

當(dāng)我們執(zhí)行下面的hello.py時,使用的flask自帶的服務(wù)器,完成了web服務(wù)的啟動。在生產(chǎn)環(huán)境中,flask自帶的服務(wù)器,無法滿足性能要求。

需要采用uWsgi或者Gunicorn來啟動web服務(wù),我們這里采用Gunicorn做wsgi容器,來部署flask程序。

Gunicorn(綠色獨角獸)是一個Python WSGI的HTTP服務(wù)器。從Ruby的獨角獸(Unicorn )項目移植。該Gunicorn服務(wù)器與各種Web框架兼容,實現(xiàn)非常簡單,輕量級的資源消耗。Gunicorn直接用命令啟動,不需要編寫配置文件,相對uWSGI要容易很多。

區(qū)分幾個概念:

WSGI:全稱是Web Server Gateway Interface(web服務(wù)器網(wǎng)關(guān)接口),它是一種規(guī)范,它是web服務(wù)器和web應(yīng)用程序之間的接口。它的作用就像是橋梁,連接在web服務(wù)器和web應(yīng)用框架之間。

uwsgi:是一種傳輸協(xié)議,用于定義傳輸信息的類型。

uWSGI:是實現(xiàn)了uwsgi協(xié)議WSGI的web服務(wù)器。

本次介紹的部署方式: nginx + gunicorn + flask

# hello.py

from flask import Flask
app = Flask(__name__)

@app.route('/hello')
def hello():
    return '<h1>hello world</h1>'

if __name__ == '__main__':
    app.run(debug=True)

使用Gunicorn:

web開發(fā)中,部署方式大致類似。簡單來說,前端代理使用Nginx主要是為了實現(xiàn)分流、轉(zhuǎn)發(fā)、負(fù)載均衡,以及分擔(dān)服務(wù)器的壓力。Nginx部署簡單,內(nèi)存消耗少,成本低。

Nginx既可以做正向代理,也可以做反向代理。

正向代理:請求經(jīng)過代理服務(wù)器從局域網(wǎng)發(fā)出,然后到達互聯(lián)網(wǎng)上的服務(wù)器。

特點:服務(wù)端并不知道真正的客戶端是誰。

反向代理:請求從互聯(lián)網(wǎng)發(fā)出,先進入代理服務(wù)器,再轉(zhuǎn)發(fā)給局域網(wǎng)內(nèi)的服務(wù)器。

特點:客戶端并不知道真正的服務(wù)端是誰。

區(qū)別:正向代理的對象是客戶端。反向代理的對象是服務(wù)端。

下面來寫一個完整的部署示例。

安裝gunicorn

安裝命令如下:

pip3 install gunicorn

安裝信息如下:

[root@server01 ~]# pip3 install gunicorn
Looking in indexes: https://mirrors.aliyun.com/pypi/simple/
Collecting gunicorn
  Downloading https://mirrors.aliyun.com/pypi/packages/69/ca/926f7cd3a2014b16870086b2d0fdc84a9e49473c68a8dff8b57f7c156f43/gunicorn-20.0.4-py2.py3-none-any.whl (77kB)
     |████████████████████████████████| 81kB 7.1MB/s 
Requirement already satisfied: setuptools>=3.0 in /usr/local/python3/lib/python3.7/site-packages (from gunicorn) (39.0.1)
Installing collected packages: gunicorn
Successfully installed gunicorn-20.0.4
WARNING: You are using pip version 19.2.1, however version 19.3.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
[root@server01 ~]# 

配置全局命令:安裝完畢之后,全局環(huán)境是無法直接執(zhí)行的,需要找到二進制文件軟鏈接到/usr/bin路徑下。

# 安裝之后,無法直接執(zhí)行命令
[root@server01 ~]# gunicorn -h
-bash: gunicorn: command not found

搜索安裝之后,gunicorn二進制可執(zhí)行文件的位置:

[root@server01 ~]# find / -name "*gunicorn*" -ls | grep python3 | grep bin
405121    4 -rwxr-xr-x   1 root     root          236 Dec 12 08:31 /usr/local/python3/bin/gunicorn
[root@server01 ~]# 

設(shè)置軟鏈接如下:

[root@server01 ~]# ln -s /usr/local/python3/bin/gunicorn /usr/bin/gunicorn
[root@server01 ~]# 
[root@server01 ~]# ls -ll /usr/bin/gunicorn
lrwxrwxrwx 1 root root 31 Dec 12 08:38 /usr/bin/gunicorn -> /usr/local/python3/bin/gunicorn
[root@server01 ~]# 

配置軟鏈接之后,就可以全局環(huán)境使用gunicorn了,例如查看版本如下:

[root@server01 ~]# gunicorn -v
gunicorn (version 20.0.4)
[root@server01 ~]# 

使用gunicorn啟動flask項目

首先準(zhǔn)備好一個flask項目:啟動flask項目之后,測試訪問hello視圖如下:

[root@server01 ~]# curl 127.0.0.1:5000/hello
<h1>hello world</h1>[root@server01 ~]# 

測試項目能夠正常訪問之后,停止flask,下面來使用gunicorn啟動。

查看命令行選項: 安裝gunicorn成功后,通過命令行的方式可以查看gunicorn的使用信息。

[root@server01 ~]# gunicorn -h
usage: gunicorn [OPTIONS] [APP_MODULE]

optional arguments:
  -h, --help            show this help message and exit
  -v, --version         show program's version number and exit
  -c CONFIG, --config CONFIG
                        The Gunicorn config file. [None]
  -b ADDRESS, --bind ADDRESS
                        The socket to bind. [['127.0.0.1:8000']]
 ....
[root@server01 ~]# 

通常使用的參數(shù):

-c CONFIG, --config=CONFIG
設(shè)定配置文件。
-b BIND, --bind=BIND
設(shè)定服務(wù)需要綁定的端口。建議使用HOST:PORT。
-w WORKERS, --workers=WORKERS
設(shè)置工作進程數(shù)。建議服務(wù)器每一個核心可以設(shè)置2-4個。
-k MODULE
選定異步工作方式使用的模塊。

直接運行:

#直接運行,默認(rèn)啟動的127.0.0.1::8000
gunicorn 運行文件名稱:Flask程序?qū)嵗?

啟動如下:

測試訪問hello視圖:

[root@server01 ~]# curl 127.0.0.1:8000/hello
<h1>hello world</h1>[root@server01 ~]# 

可以看到正常訪問,下面來看看指定端口號的啟動方式,如下。

指定進程和端口號: -w: 表示進程(worker)。 -b:表示綁定ip地址和端口號(bind)。 -D: 后臺運行

$ gunicorn -w 4 -b 127.0.0.1:5001 運行文件名稱:Flask程序?qū)嵗?

指定日志文件

  --access-logfile FILE
                        The Access log file to write to. [None]
  --access-logformat STRING
                        The access log format. [%(h)s %(l)s %(u)s %(t)s
  --error-logfile FILE, --log-file FILE
                        The Error log file to write to. [-]

指定access.log和error.log日志文件如下:

gunicorn -w 4 -b 127.0.0.1:5001 --access-logfile access.log --error-logfile error.log main:app

查看access.log日志信息:

查看error.log日志信息:

Nginx配置訪問gunicorn

Nginx配置:

打開 /usr/local/nginx/conf/nginx.conf文件

upstream flask{
    server 127.0.0.1:5000;
    server 127.0.0.1:5001;
}

server {
    # 監(jiān)聽80端口
    listen 80;
    # 本機
    server_name localhost; 
    # 默認(rèn)請求的url
    location / {
        #請求轉(zhuǎn)發(fā)到gunicorn服務(wù)器
        proxy_pass http://flask; 
        #設(shè)置請求頭,并將頭信息傳遞給服務(wù)器端 
        proxy_set_header Host $host; 
    }
}

可以看到nginx轉(zhuǎn)發(fā)gunicorn只需要通過端口號直接proxy_pass代理即可。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容