Article / 文章中心

python编程:mac环境gunicorn+nginx部署flask项目

发布时间:2021-11-23 点击数:403

第一步,安装配置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... [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的命令来运行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; } 

参考文章