supervisor 使用及管理nginx+gunicorn

概述:

在我们正常的生产环境中,为了保证我们的进程一直保持UP的状态,我们都会想尽各种办法,监控、提供不间断服务,无论是使用脚本监控也好、已或者使用zabbix的触发器自动启动也好,都是为了提供不间断的服务,下边来介绍一个Linux下比较好用的进程管理器 supervisor。

supervisor是用 pathon写的一个工具,是比较好的服务管理工具,一个很重要的功能就是监控服务器主要服务,并且在出现问题时进行重启。在Ubuntu的环境下我们可以使用apt-get 来进行安装;下边将来具体介绍安装及使用方法:

1、使用apt-get 安装supervisor

apt-get install supervisor

2、编辑并查看配置文件

; supervisor config file

[unix_http_server]
file=/var/run/supervisor.sock   ; (the path to the socket file)
chmod=0700                       ; sockef file mode (default 0700)

[supervisord]
logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
childlogdir=/var/log/supervisor            ; ('AUTO' child log dir, default $TEMP)

; the below section must remain in the config file for RPC
; (supervisorctl/web interface) to work, additional interfaces may be
; added by defining them in separate rpcinterface: sections
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

[supervisorctl]
serverurl=unix:///var/run/supervisor.sock ; use a unix:// URL  for a unix socket

; The [include] section can just contain the "files" setting.  This
; setting can list multiple files (separated by whitespace or
; newlines).  It can also contain wildcards.  The filenames are
; interpreted as relative to this file.  Included files *cannot*
; include files themselves.

[include]
**files = /etc/supervisor/conf.d/*.conf

一定要注意最下边这一行,这一行代表将引用或者使用此路径下所有以.conf结尾的配置文件,因为我们新添加的进程,将都写在此目录下!

3、添加nginx进程到supervisor

[program:up_nginx]
command=/usr/sbin/nginx
process_name=%(program_name)s
numprocs=4                    ; 启动几个进程
autostart=true                ; 随着supervisord的启动而启动
autorestart=true              ; 自动重启。。当然要选上了
startretries=10               ; 启动失败时的最多重试次数
exitcodes=0                 ; 正常退出代码(是说退出代码是这个时就不再重启了吗?待确定)
stopsignal=KILL               ; 用来杀死进程的信号
stopwaitsecs=10               ; 发送SIGKILL前的等待时间
redirect_stderr=true          ; 重定向stderr到stdout

当然除了此处配置完成后就可以启动了。但是这样的话会有一个问题,就是当你是使用supervisorctl status 来查看你这个程序的状态时,你发现它不是running的状态,这是因为supervisor只是对于不能自动生成后台进程的程序有效,当启动nginx的命令执行完成后将会退出,所以我们需要再nginx的配置文件中加入:

daemon off

让nginx不生成后台进程,即可;

4、配置gunicorn程序:

[program:start_gunicorn]
command=/usr/local/bin/gunicorn wsgi:application -k gevent -w 8 --bind=unix:/var/log/gunicorn.sock --log-level=debug --log-file /var/log/yyc.log -p /var/log/gunicorn_test.pid 
autostart=true
redirect_stderr=true
user=root
directory=/usr/local/qs-project/web
stdout_logfile=/var/log/test_test.log

完成以上配置之后,重启supervisor即可。这样的话就算你使用kill来杀死nginx或者gunicorn进程,supervisor也会自动帮你启动的;

5、下边来简单介绍一下supervisor常用的命令以及参数等:

-c / -- 配置文件名 - 配置文件
-n / -- nodaemon - 在前台(在配置文件中一样的“nodaemon真”)运行
-h / -- help - 打印帮助信息
-v / --version - 显示版本信息
-u / - user用户 - 运行supervisord为这个用户(或数字UID-m / - 的umask UMASK - 使用的umask守护子(默认值是022-d / - 目录目录 - 目录CHDIR到时,进程化
-l / - 日志文件名 - 使用文件名作为日志文件路径
-y / - logfile_maxbytes字节数据 - 用字节来限制日志文件的最大尺寸
-z / - logfile_backups NUM - 备份的数量,以保持最大的时候达到字节
-e / - 日志级别LEVEL - 使用级别日志级别(调试,信息,警告,错误,严重)
-j / - pidfile进程文件名 - 写的守护进程,以文件名的PID文件
-i / - 标识符STR - 用于supervisord这个实例标识符
-q / - childlogdir目录 - 子进程日志的日志目录
-k / - nocleanup - 防止进程进行清理(去除
                   旧的自动孩子的日志文件)在启动时。
-a / - minfds NUM - 文件描述符为启动成功的最小数量
-t / - strip_ansi - 从过程输出带ANSI转义码
--minprocs NUM - 可用于启动成功进程的最小数目
--profile_options选项 - 运行supervisord下探查和输出
                             结果的基础上选择,这是一个逗号sep'd
                             的“累计”,“调用”,和/或“呼叫者的列表,
                             例如“累积的,来电者)
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值