天天看點

python:mac環境gunicorn+nginx部署flask項目 第一步,安裝配置gunicorn 第二步,安裝配置nginx

第一步,安裝配置gunicorn

文檔:http://docs.gunicorn.org/en/stable/run.html

1、安裝

pip install gunicorn
           

2、在flask項目中添加兩行代碼

from flask import Flask

app = Flask(__name__)


@app.route('/')
def hello_world():
    return 'Hello World!'

if __name__ == '__main__':
    #------------------- 需要添加的兩行代碼
    from werkzeug.contrib.fixers import ProxyFix
    app.wsgi_app = ProxyFix(app.wsgi_app)
    #------------------- 
    app.run()
           

3、啟動:

gunicorn 入口檔案名:app
           

預設是監聽http://127.0.0.1:8000/

也可以修改配置

gunicorn -w 4 -b 127.0.0.1:8000 入口檔案名:app
           

說明:

修改程序:-w 4

修改端口:-b 127.0.0.1:8000

4、添加快捷方式

如果每次啟動都那麼麻煩,豈不是都沒興趣了,可以使用bash腳本進行快捷啟動

建立檔案~/.flask.sh

#!/bin/bash

echo "flask starting..."

source ~/.bash_profile   # 導入PATH變量
 
workon py3  # 切換python虛拟環境
cd /Users/qmp/workspace/mouday.github.io/index_flask  # 切換到檔案目錄
gunicorn -w 4 -b 127.0.0.1:8000 index:app   # 啟動flask伺服器
           

在檔案~/.bash_profile中添加falsk别名,運作啟動腳本

# flask服務啟動的别名

alias flask="bash ~/.flask.sh"
           

好了,現在可以直接這樣啟動了

$ flask
           

終端列印,啟動成功!

flask starting...
[2020-07-19 15:55:04 +0800] [5350] [INFO] Starting gunicorn 19.8.1
[2018-07-19 15:55:04 +0800] [5350] [INFO] Listening at: http://127.0.0.1:8080 (5350)
[2020-07-19 15:55:04 +0800] [5350] [INFO] Using worker: sync
[2020-07-19 15:55:04 +0800] [5353] [INFO] Booting worker with pid: 5353
[2020-07-19 15:55:04 +0800] [5354] [INFO] Booting worker with pid: 5354
[2020-07-19 15:55:04 +0800] [5355] [INFO] Booting worker with pid: 5355
[2020-07-19 15:55:04 +0800] [5356] [INFO] Booting worker with pid: 5356
           

第二步,安裝配置nginx

Mac 下使用 Homebrew 安裝 Nginx

brew install nginx
           

使用brew的指令來運作nginx

brew services start nginx  # 啟動
brew services stop nginx   # 停止
           

測試位址:http://127.0.0.1:8080

檢視nginx配置檔案路徑:

$ nginx -t
           

/usr/local/etc/nginx/nginx.conf

server {
    listen 80;
    server_name example.org; # 這是HOST機器的外部域名,用位址也行

    location / {
        proxy_pass http://127.0.0.1:8000; # 這裡是指向 gunicorn host 的服務位址
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

  }
           

作用是将80端口轉發到8000端口

檢查指令

nginx -t
           

成功會有 successful 提示

重新開機nginx

通路位址:http://127.0.0.1/

如果通路不到,可以先關閉,在使用如下指令啟動

sudo nginx -c /usr/local/etc/nginx/nginx.conf
           

報錯及解決

1、Permission報錯

nginx: [emerg] open() "/usr/local/var/run/nginx.pid" failed 
(13: Permission denied)
           

有權限問題:直接使用

sudo chown -R $(whoami)  /usr/local/var/run
           

2、directory報錯

nginx: [error] open() "/usr/local/var/run/nginx.pid" failed
 (2: No such file or directory)
           

使用如下指令指定配置檔案

nginx -c /usr/local/nginx/conf/nginx.conf
           

3、Address報錯

nginx: [emerg] bind() to 0.0.0.0:8080 failed (48: Address already in use)
           

端口被占用,嘗試使用如下指令來關閉nginx服務

sudo nginx -s stop
brew services stop nginx  # 第一個不行再用這個
           

4、Permission denied

nginx: [emerg] bind() to 0.0.0.0:80 failed (13: Permission denied)
           

Linux隻有root使用者可以使用1024一下的端口,80端口改為1024以上

brew指令

安裝: sudo brew install [軟體名]

搜尋: sudo brew search [軟體名]

檢視: sudo brew info [軟體名]

卸裝: sudo uninstall [軟體名]

nginx -V 檢視版本,以及配置檔案位址

nginx -v 檢視版本

nginx -c filename 指定配置檔案

nginx -h 幫助

nginx指令

重新加載配置|重新開機|停止|退出 nginx

nginx -s reload|reopen|stop|quit

sudo nginx 打開 nginx

nginx -t 測試配置是否有文法錯誤

也可以建立目錄 vhosts單獨配置

nginx.conf 添加引入語句

http{
    
    # 最下面添加
    include vhosts/*.conf;
}
           

繼續閱讀