安装 gunicorn

 pip install gunicorn

pip 是一个重要的工具,python 用来管理包。还有一个最佳生产就是每次使用 pip 安装的库,都写入一个 requirement 文件里面,既能知道自己安装了什么库,也方便别人部署时,安装相应的库。

 pip freeze > requirements.txt

以后每次 pip 安装了新的库的时候,都需freeze 一次。

当我们安装好 gunicorn 之后,需要用 gunicorn 启动 flask,注意 flask 里面的name里面的代码启动了 app.run(),这个含义是用 flask 自带的服务器启动 app。这里我们使用了 gunicorn,myapp.py 就等同于一个库文件,被 gunicorn 调用。

 gunicron -w4 -b0.0.0.0:8000 myapp:app

此时,我们需要用 8000 的端口进行访问,原先的5000并没有启用。其中 gunicorn 的部署中,,-w 表示开启多少个 worker,-b 表示 gunicorn 开发的访问地址。

想要结束 gunicorn 只需执行 pkill gunicorn,有时候还的 ps 找到 pid 进程号才能 kill。可是这对于一个开发来说,太过于繁琐,因此出现了另外一个神器---supervisor,一个专门用来管理进程的工具,还可以管理系统的工具进程。

安装 supervisor

pip install supervisor
echo_supervisord_conf > supervisor.conf # 生成 supervisor 默认配置文件
vim supervisor.conf # 修改 supervisor 配置文件,添加 gunicorn 进程管理

在myapp supervisor.conf 配置文件底部添加 (注意我的工作路径是/home/rsj217/rsj217/)

[program:myapp]
command=/home/rsj217/rsj217/myproject/venv/bin/gunicorn -w4 -b0.0.0.0:2170 myapp:app ; supervisor启动命令
directory=/home/rsj217/rsj217/myproject ; 项目的文件夹路径
startsecs=0 ; 启动时间
stopwaitsecs=0 ; 终止等待时间
autostart=false ; 是否自动启动
autorestart=false ; 是否自动重启
stdout_logfile=/home/rsj217/rsj217/myproject/log/gunicorn.log ; log 日志
stderr_logfile=/home/rsj217/rsj217/myproject/log/gunicorn.err ; 错误日志

supervisor的基本使用命令

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管理的程序进程

supervisor 还有一个web的管理界面,可以激活。更改下配置

[inet_http_server]         ; inet (TCP) server disabled by default
port=127.0.0.1:9001 ; (ip_address:port specifier, *:port for all iface)
username=user ; (default is no username (open server))
password=123 ; (default is no password (open server)) [supervisorctl]
serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL for a unix socket
serverurl=http://127.0.0.1:9001 ; use an http:// url to specify an inet socket
username=user ; should be same as http_username if set
password=123 ; should be same as http_password if set
;prompt=mysupervisor ; cmd line prompt (default "supervisor")
;history_file=~/.sc_history ; use readline history if available

现在可以使用 supervsior 启动 gunicorn啦。运行命令 supervisord -c supervisor.conf

访问 http://127.0.0.1:9001 可以得到 supervisor的web管理界面,访问 http://127.0.0.1:2170 可以看见gunciron 启动的返回的 hello world

安装配置 nginx

采用 apt-get方式安装最简单。运行 sudo apt-get install nginx。安装好的nginx的二进制文件放在 /usr/sbin/文件夹下面。而nginx的配置文件放在 /etc/nginx下面。

使用 supervisor 来管理 nginx。这里需要注意一个问题,linux的权限问题。nginx是sudo的方式安装,启动的适合也是 root用户,那么我们现在也需要用 root用户启动supervisor。增加下面的配置文件

[program:nginx]
command=/usr/sbin/nginx
startsecs=0
stopwaitsecs=0
autostart=false
autorestart=false
stdout_logfile=/home/rsj217/rsj217/myproject/log/nginx.log
stderr_logfile=/home/rsj217/rsj217/myproject/log/nginx.err

到此为止,进步的 web 部属已经完成。当然,最终我们需要把项目代码部属到服务器上.批量的自动化部属需要另外一个神器 fabric.具体使用,就不再这篇笔记阐述。项目源码中包含了fabric文件。下载fabric,更改里面的用户名和秘密,就可以部属在自己或者远程的服务器上了。


作者:人世间
链接:http://www.jianshu.com/p/be9dd421fb8d
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

最新文章

  1. 程序员装B指南
  2. ie8兼容性(不支持trim 、readonly光标、乱码encodeURI())
  3. 细说IIS异常日志 — 你必须知道的功能
  4. GC之七--gc日志分析工具
  5. sessions 表的架构过程
  6. 创建类模式(五):单例(Singleton)
  7. INTELLIJ IDEA集成CHECKSTYLE(转)
  8. image即时上传
  9. PAT1084:Broken Keyboard
  10. 【codeforces 983E】NN country
  11. James Munkres Topology: Theorem 20.4
  12. JavaScript 里 var a =a ||{}
  13. sort排序在苹果与安卓端不一致问题
  14. 微信、企业微信和支付窗 SDK 三合一,JeeWx-api 1.2.0 版本发布
  15. Android检查手机上是否安装了第三方软件的方法------本文以百度地图为例
  16. Features + Git + Drush,打造你的Drupal开发与维护标准工作流
  17. Splash Lua 脚本
  18. DataTable进行排序Asc升序,Desc降序
  19. C#操作Excel 单元格的格式处理[xyytIT]
  20. SELECT INTO 和 INSERT INTO SELECT

热门文章

  1. 图解堆算法、链表、栈与队列(Mark)
  2. SQL SERVER 2008查看sql执行的时间
  3. poj2349
  4. 【BZOJ4128】Matrix BSGS+hash
  5. 【BZOJ2806】[Ctsc2012]Cheat 广义后缀自动机+二分+单调队列优化DP
  6. zookeeper curator CRUD
  7. Django多对多的创建
  8. Spring注解式与配置文件式
  9. IDEA运行后控制台输出乱码
  10. memcpy使用