天天看點

nginx+gunicorn+flask的Web服務部署詳細流程

插件的選擇

flask

  • 1、使用WSGI協定的web架構
  • 2、web部署靈活快捷
  • 3、架構自帶實作了WSGI協定的server ,但隻用于調試,性能不能得到保障

gunicorn

  • 1、實作了WSGI協定的伺服器層,主要是将HTTP協定轉換為WSGI協定,可供python解析
  • 2、内置了gevent來提高伺服器性能,想要發揮多核的優勢可配合多程序使用

nginx

  • 1、可了解為緩沖層,也是屬于伺服器層次
  • 2、對靜态檔案的支援好
  • 3、提高并發度
  • 4、負載均衡
  • 5、反向代理

Web部署流程

flask

安裝flask

sudo pip3 install flask
           

檢驗是否安裝成功

Python 3.5.2 (default, Oct  8 2019, 13:06:37)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import flask
>>> from flask import Flask
           

gunicorn

安裝gunicorn

sudo pip3 install greenlet
sudo pip3 install gevent
sudo pip3 install gunicorn
           

測試gunicorn,編寫一個簡單的web應用程式

檔案"demo.py"

from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello_wprld():
    return "hello world!"

if __name__ == "__main__":
    app.run()
           

終端輸入指令

sudo gunicorn -w 4 -b 0.0.0.0:10001 demo:app
           

或者編寫一個conf配置檔案"demo.conf"

bind = '0.0.0.0:10001'
workers = 4
backlog = 2048
worker_class = "gevent" #sync, gevent,meinheld
daemon = False           # 是否背景運作
           

執行指令

sudo gunicorn -c demo.conf demo:app
           

顯示為以下結果表示測試成功

[2019-10-22 18:57:51 +0800] [207368] [INFO] Starting gunicorn 19.9.0
[2019-10-22 18:57:51 +0800] [207368] [INFO] Listening at: http://0.0.0.0:10001 (207368)
[2019-10-22 18:57:51 +0800] [207368] [INFO] Using worker: sync
[2019-10-22 18:57:51 +0800] [207371] [INFO] Booting worker with pid: 207371
[2019-10-22 18:57:51 +0800] [207373] [INFO] Booting worker with pid: 207373
[2019-10-22 18:57:52 +0800] [207376] [INFO] Booting worker with pid: 207376
[2019-10-22 18:57:52 +0800] [207377] [INFO] Booting worker with pid: 207377
           

在浏覽器中輸入ip:port 可看到以下畫面

nginx+gunicorn+flask的Web服務部署詳細流程

nginx

nginx安裝

sudo apt install nginx
           

備份nginx的預設配置檔案

cp /etc/nginx/sites-available/default /etc/nginx/sites-availabel/default.bak
           

修改default檔案

server {
        listen 80 default_server;
        listen [::]:80 default_server ipv6only=on;

        root /usr/share/nginx/html;
        index index.html index.htm;

        # Make site accessible from http://localhost/
        server_name localhost;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                proxy_pass http://0.0.0.0:10001;
                proxy_redirect     off;
                proxy_set_header   Host                 $http_host;
                proxy_set_header   X-Real-IP            $remote_addr;
                proxy_set_header   X-Forwarded-For      $proxy_add_x_forwarded_for;
                proxy_set_header   X-Forwarded-Proto    $scheme;
                # ment to enable naxsi on this location
                # include /etc/nginx/naxsi.rules
        }
}
           

常用nginx指令

sudo /etc/init.d/nginx start # 啟動 
sudo /etc/init.d/nginx stop # 停止 
sudo /etc/init.d/nginx restart  # 重新開機
           

輸入指令

sudo /etc/init.d/nginx start
           

在浏覽器輸入0.0.0.0可看到

nginx+gunicorn+flask的Web服務部署詳細流程

繼續閱讀