ubuntu supervisor管理uwsgi+nginx

一、概述

superviosr是一个Linux/Unix系统上的进程监控工具,他/她upervisor是一个Python开发的通用的进程管理程序,可以管理和监控Linux上面的进程,能将一个普通的命令行进程变为后台daemon,并监控进程状态,异常退出时能自动重启。不过同daemontools一样,它不能监控daemon进程(也就是后台进程)

 

二、安装

apt-get install -y supervisor

 

安装成功后,会在/etc/supervisor目录下,生成supervisord.conf配置文件。

你也可以使用echo_supervisord_conf > supervisord.conf命令,生成默认的配置文件(不建议,内容比较多)。

supervisord.conf示例配置:

; 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

进程配置会读取/etc/supervisor/conf.d目录下的*.conf配置文件

 

安装完成之后,默认就启动了supervisor

 

三、管理uwsgi

在上一篇文章中,链接如下:

https://www.cnblogs.com/xiao987334176/p/11329906.html

 

已经配置好了uwsgi和nginx。这是2个比较关键的进程,任意一个进程死掉,都会导致网页无法访问。

 

修改uwsgi配置

关闭后台运行,为什么呢?因为supervisord无法管理后台进程

cd /www/mysite1/uwsgi
vim uwsgi.ini

 

注释掉daemonize

[uwsgi]

# Django-related settings
# the base directory (full path)
chdir           = /www/mysite1
# Django's wsgi file
module          = mysite1.wsgi
# the virtualenv (full path)
home            = /virtualenvs/venv
# process-related settings
# master
master          = true
# maximum number of worker processes
processes       = 1
# pid file
pidfile         = /www/mysite1/uwsgi/uwsgi.pid
# socket file path (full path)
socket          = /www/mysite1/uwsgi/mysite1.sock
# clear environment on exit
vacuum          = true
#The process runs in the background and types the log to the specified log file
#daemonize       = /www/mysite1/uwsgi/uwsgi.log

 

关闭uwsgi

/virtualenvs/venv/bin/uwsgi --stop uwsgi.pid

 

新增uwsgi 进程配置文件

cd /etc/supervisor/conf.d
vim uwsgi.conf

内容如下:

[program:uwsgi]
directory = /www/mysite1 ;程序的启动目录
command= /virtualenvs/venv/bin/uwsgi --ini uwsgi/uwsgi.ini ;启动命令
autostart = true     ; 在 supervisord 启动的时候也自动启动
startsecs = 5        ; 启动 5 秒后没有异常退出,就当作已经正常启动了
autorestart = true   ; 程序异常退出后自动重启
startretries = 3     ; 启动失败自动重试次数,默认是 3
user = root          ; 用哪个用户启动
redirect_stderr = true  ; 把 stderr 重定向到 stdout,默认 false
stdout_logfile_maxbytes = 20MB  ; stdout 日志文件大小,默认 50MB
stdout_logfile_backups = 20     ; stdout 日志文件备份数

;stdout 日志文件,需要注意当指定目录不存在时无法正常启动,所以需要手动创建目录(supervisord 会自动创建日志文件)
stdout_logfile = /www/mysite1/logs/stdout.log
;输出的错误文件
stderr_logfile = /www/mysite1/logs/stderr.log
;添加运行需要的环境变量, 这里用了虚拟环境
;environment=PYTHONPATH=$PYTHONPATH:/virtualenvs/venv/bin/
;然后确保杀死主进程后,子进程也可以停止
stopasgroup=true
killasgroup=true

 

创建日志目录

mkdir /www/mysite1/logs/

注意:supervisord不会自动帮你创建目录,因此需要手动创建。

 

加载配置

supervisorctl reload

 

如果出现:

error: <class 'socket.error'>, [Errno 2] No such file or directory: file: /usr/lib/python2.7/socket.py line: 228

先要确认进程是否存在:ps -ef | grep supervisord 然后使用命令 supervisord -c /etc/supervisor/supervisord.conf 启动。

 

查看状态

 第一个查看,状态为STARTING,第二次查看时,状态为RUNNING  

root@ubuntu:/etc/supervisor/conf.d# supervisorctl status
uwsgi                            STARTING  
root@ubuntu:/etc/supervisor/conf.d# supervisorctl status
uwsgi                            RUNNING   pid 20367, uptime 0:00:12

 

kill进程

root@ubuntu:/etc/supervisor/conf.d# ps -aux|grep uwsgi
root     20367  0.3  0.8 102680 34832 ?        S    13:50   0:00 /virtualenvs/venv/bin/uwsgi --ini uwsgi/uwsgi.ini
root     20369  0.0  0.7 102680 28768 ?        S    13:50   0:00 /virtualenvs/venv/bin/uwsgi --ini uwsgi/uwsgi.ini
root     20377  0.0  0.0  15984   976 pts/1    S+   13:52   0:00 grep --color=auto uwsgi
root@ubuntu:/etc/supervisor/conf.d# killall -9 uwsgi
root@ubuntu:/etc/supervisor/conf.d# ps -aux|grep uwsgi
root     20379  0.0  0.8 102676 34844 ?        S    13:52   0:00 /virtualenvs/venv/bin/uwsgi --ini uwsgi/uwsgi.ini
root     20381  0.0  0.7 102676 28488 ?        S    13:52   0:00 /virtualenvs/venv/bin/uwsgi --ini uwsgi/uwsgi.ini
root     20383  0.0  0.0  15984   968 pts/1    S+   13:52   0:00 grep --color=auto uwsgi

 

以上信息,可以发现。强制kill掉进程之后,supervisor会将uwsgi启动。

 

四、管理Nginx

由于supervisor不能监控后台程序,

command = /usr/local/bin/nginx 这个命令默认是后台启动,
加上-g ‘daemon off;’这个参数可解决这问题,这个参数的意思是在前台运行。

command = /usr/local/bin/nginx -g ‘daemon off;’

新增nginx 进程配置文件

cd /etc/supervisor/conf.d
vim nginx.conf

内容如下:

[program:nginx]
command = /usr/sbin/nginx -g 'daemon off;'
startsecs=0
autostart=true
autorestart=true
stdout_logfile=/var/log/nginx/stdout.log
stopasgroup=true
killasgroup=true

 

加载配置

supervisorctl reload

 

查看状态

root@ubuntu:/etc/supervisor/conf.d# supervisorctl status
nginx                            RUNNING   pid 20409, uptime 0:00:00
uwsgi                            RUNNING   pid 20404, uptime 0:00:07

 

kill掉nginx进程

root@ubuntu:/etc/supervisor/conf.d# ps -aux|grep nginx
root     20441  0.2  0.2 125116  9832 ?        S    13:58   0:00 nginx: master process /usr/sbin/nginx -g daemon off;
www-data 20442  0.0  0.0 125440  3228 ?        S    13:58   0:00 nginx: worker process
root     20446  0.0  0.0  15984  1084 pts/1    S+   13:58   0:00 grep --color=auto nginx
root@ubuntu:/etc/supervisor/conf.d# killall -9 nginx
root@ubuntu:/etc/supervisor/conf.d# ps -aux|grep nginx
root     20448  0.0  0.2 125116  9792 ?        S    13:58   0:00 nginx: master process /usr/sbin/nginx -g daemon off;
www-data 20449  0.0  0.0 125440  3132 ?        S    13:58   0:00 nginx: worker process
root     20451  0.0  0.0  15984   936 pts/1    S+   13:58   0:00 grep --color=auto nginx

 

 

本文参考:

https://www.cnblogs.com/xishuai/p/ubuntu-install-supervisor.html

https://blog.csdn.net/qq_32402917/article/details/80169366

https://blog.csdn.net/genglei1022/article/details/81239900

 

posted @ 2019-08-06 22:34  肖祥  阅读(1815)  评论(0编辑  收藏  举报