原文:https://note.youdao.com/ynoteshare1/index.html?id=c91446020ebd25b05190799c50ede8e6&type=note (1)应用环境 mkdir myapp cd myapp 新建run.py 脚本 from flask import Flask app = Flask(\_\_name\_\_) app.route('/') def index(): return 'hello world':q if \_\_name\_\_ =='\_\_main\_\_': app.run() (2)gunicorn配置

  • 安装gunicorn

pip install gunicorn - 启动gunicorn

gunicorn -D -w 3 -b 0.0.0.0:8000 run:app D 表示后台运行 w 表示有3 个 工作线程(感觉有些类似 nginx 的 master-worker 模型) b 指定ip 和端口 这里采用本机访问, 主要是为了使用nginx 进行代理, 方便管理 ,可以用0.0.0.0来让其他主机访问,但是记得要看后面的防火墙端口,记得开启这里的8000 run表存放 写着全局变量 app 的那个工程文件 在我们的这个工程中, 即包含 init.py 的那个文件 (3)supervisor配置 - 安装:

pip install supervisor - 进到工程目录,在项目目录添加supervisor的配置文件,


echo\_supervisord\_conf > supervisord.conf - 会生成一个配置文件

vi supervisord.conf 添加信息: [program:myapp] directory =/clh/myapps command =/clh/myapps/myenv/bin/gunicorn -w 4 -b 0.0.0.0:8000 run:app myapp 自己起的项目名 directory运行项目所在的工程文件 command ,gunicorn 的启动命令 - 配置文件启动

supervisord -c supervisor.conf - 查看supervisor状态

supervisorctl -c supervisor.conf status supervisord -c supervisor.conf 通过配置文件启动supervisor,注意它和其他命令不一样 supervisorctl -c supervisor.conf status 察看supervisor的状态 supervisorctl -c supervisor.conf reload 重新载入 配置文件 supervisorctl -c supervisor.conf start [all]|[appname] 启动指定/所有 supervisor管理的程序进程 supervisorctl -c supervisor.conf stop [all]|[appname] 关闭指定/所有 supervisor管理的程序进程 (4)nginx配置 - nginx安装

apt-get install nginx - 查看nginx进程

ps aux|grep nginx - 首先先备份配置

sudo cp /etc/nginx/sites-available/default default.bak - 修改配置

sudo vi /etc/nginx/sites-available/default Ctrl + O 保存 Ctrl + X 退出 把配置改为 server { listen 80; server\_name 115.159.146.108; # 这是HOST机器的外部域名,用地址也行 location / { proxy\_pass http://0.0.0.0:8000; # 这里是指向 gunicorn host 的服务地址 } } 这样启动之后80端口就可以转发到8000端口了 - 检查配置

nginx -t 若出现 nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful 则配置成功 杀nginx进程 重启nginx sudo service nginx restart 在已经运行了Gunicorn的前提下,在浏览器访问115.159.146.108就会出现Hello World!了 https://clayandmore.github.io/2017/03/19/flask+gunicorn+supervisorn+nginx%E9%A1%B9%E7%9B%AE%E9%83%A8%E7%BD%B2/flask项目部署小结:https://gist.github.com/binderclip/f6b6f5ed4d71fa64c7c5Ubuntu下Supervisor安装、配置和使用:https://my.oschina.net/u/2396236/blog/1594853

标签: nginx, conf, linux, 项目, 部署, supervisor, gunicorn

相关文章推荐

添加新评论,含*的栏目为必填