亚洲一区精品自拍_2021年国内精品久久_男同十八禁gv在线观看_免费观看a级性爱黄片

Article / 文章中心

python編程:mac環(huán)境gunicorn+nginx部署flask項(xiàng)目

發(fā)布時(shí)間:2021-11-23 點(diǎn)擊數(shù):741

第一步,安裝配置gunicorn

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

1、安裝

pip install gunicorn

2、在flask項(xiàng)目中添加兩行代碼

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、啟動(dòng):

gunicorn 入口文件名:app

默認(rèn)是監(jiān)聽http://127.0.0.1:8000/

也可以修改配置

gunicorn -w 4 -b 127.0.0.1:8000 入口文件名:app

說明:

修改進(jìn)程:-w 4

修改端口:-b 127.0.0.1:8000

4、添加快捷方式

如果每次啟動(dòng)都那么麻煩,豈不是都沒興趣了,可以使用bash腳本進(jìn)行快捷啟動(dòng)

新建文件~/.flask.sh

#!/bin/bash  echo "flask starting..."  source ~/.bash_profile   # 導(dǎo)入PATH變量  workon py3  # 切換python虛擬環(huán)境 cd /Users/qmp/workspace/mouday.github.io/index_flask  # 切換到文件目錄 gunicorn -w 4 -b 127.0.0.1:8000 index:app   # 啟動(dòng)flask服務(wù)器 

在文件~/.bash_profile中添加falsk別名,運(yùn)行啟動(dòng)腳本

# flask服務(wù)啟動(dòng)的別名 alias flask="bash ~/.flask.sh"

好了,現(xiàn)在可以直接這樣啟動(dòng)了

$ flask

終端打印,啟動(dòng)成功!

flask starting... [2018-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) [2018-07-19 15:55:04 +0800] [5350] [INFO] Using worker: sync [2018-07-19 15:55:04 +0800] [5353] [INFO] Booting worker with pid: 5353 [2018-07-19 15:55:04 +0800] [5354] [INFO] Booting worker with pid: 5354 [2018-07-19 15:55:04 +0800] [5355] [INFO] Booting worker with pid: 5355 [2018-07-19 15:55:04 +0800] [5356] [INFO] Booting worker with pid: 5356

第二步,安裝配置nginx

Mac 下使用 Homebrew 安裝 Nginx

brew install nginx

使用brew的命令來運(yùn)行nginx

brew services start nginx  # 啟動(dòng) brew services stop nginx   # 停止

測(cè)試地址:http://127.0.0.1:8080

查看nginx配置文件路徑:

$ nginx -t  /usr/local/etc/nginx/nginx.conf
server {  listen 80;  server_name example.org; # 這是HOST機(jī)器的外部域名,用地址也行   location / {  proxy_pass http://127.0.0.1:8000; # 這里是指向 gunicorn host 的服務(wù)地址  proxy_set_header Host $host;  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;  }   }

作用是將80端口轉(zhuǎn)發(fā)到8000端口

檢查命令

nginx -t

成功會(huì)有 successful 提示

重啟nginx

訪問地址:http://127.0.0.1/

如果訪問不到,可以先關(guān)閉,在使用如下命令啟動(dòng)

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

報(bào)錯(cuò)及解決

1、Permission報(bào)錯(cuò)

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

有權(quán)限問題:直接使用

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

2、directory報(bào)錯(cuò)

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報(bào)錯(cuò)

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

端口被占用,嘗試使用如下命令來關(guān)閉nginx服務(wù)

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

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 測(cè)試配置是否有語法錯(cuò)誤

也可以新建目錄 vhosts單獨(dú)配置

nginx.conf 添加引入語句

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

參考文章