supervisor是用python开发的一个在linux系统下的进程管理工具, 可以方便的监听, 启动, 停止一个或多个进程。当一个进程被意外*死后, supervisor监听到后, 会自动重新拉起进程。
Supervisord 是用 Python 实现的一款非常实用的进程管理工具, supervisord 还要求管理的程序是非 daemon 程序, supervisord 会帮你把它转成 daemon 程序, 因此如果用 supervisord 来管理 NGINX 的话,
必须在 nginx 的配置文件里添加一行设置 daemon off 让 nginx 以非 daemon 方式启动。
程序崩溃或者退出Supervisord会自动启动程序。这样就再不也不怕go的执行文件挂掉或者退出或者死掉, Supervisord只要监控到异常状态就会重启执行文件并且记录日志。
官网:
http://supervisord.org/
一、supervisor的安装1、通过easy_install安装
yum -y install python-setuptools
easy_install supervisor
2、通过yum安装
yum -y install supervisor
3、通过pip安装
yum -y install epel-release
yum -y install python-pip
pip install supervisor
安装好后, 会生成三个执行命令, echo_supervisord_conf, supervisorctl, supervisord。
注意:安装方式推荐第三种方式, 能够优先下载最新版本
二、supervisor的配置文件supervisor的默认配置文件在 /etc/supervisord.conf 下, 如果没有可以通过如下命令生成
echo_supervisord_conf > /etc/supervisord.conf
常用的配置项如下:
[unix_http_server]
file=/tmp/supervisor.sock ; unix socket文件,supervisorctl会使用
;chmod=0700 ; socket文件权限
;chown=nobody:nogroup ; socket文件所属用户和用户组
[inet_http_server] ; web管理界面
port=127.0.0.1:9001 ; 管理界面的IP和端口
username=admin ; 登陆管理界面的用户名
password=123456 ; 登陆管理界面的密码
[supervisord]
logfile=/tmp/supervisord.log ; 日志文件
logfile_maxbytes=50MB ; 日志文件大小,为0表示不限制
logfile_backups=10 ; 日志文件备份数量,为0表示不备份
loglevel=info ; 日志级别,也可设置为 debug,warn,trace
pidfile=/tmp/supervisord.pid ; PID文件路径
nodaemon=false ; 是否前台启动,为false表示守护进程方式
minfds=1024 ; 打开文件描述符的最小值
minprocs=200 ; 创建进程数的最小值
[supervisorctl]
serverurl=unix:///tmp/supervisor.sock ; 通过 unix sokcet 连接supervisord
;serverurl=http://127.0.0.1:9001 ; 通过http方式连接supervisord
[include]
files = /etc/supervisord/confs/*.conf ; 包含其他配置文件,可以是.conf或.ini
我们需要把 [include] 前面的注释打开,并配置 files 的路径。
创建 files 中配置的目录。
mkdir -p /etc/supervisord/confs/
启动supervisor
> # supervisord -c /etc/supervisord.conf
三、管理进程。管理进程, 需要我们启动 supervisor 服务, 这里我们配置 systemctl, 开机自动启动 supervisor。
创建 /usr/lib/systemd/system/supervisord.service 文件, 配置如下:
[Unit]
Description=Supervisor daemon
[Service]
Type=forking
ExecStart=/usr/bin/supervisord
ExecStop=/usr/bin/supervisorctl $OPTIONS shutdown
ExecReload=/usr/bin/supervisorctl $OPTIONS reload
KillMode=process
Restart=on-failure
RestartSec=42s
[Install]
WantedBy=multi-user.target
启用配置
systemctl enable supervisord.service
启动 supervisord
systemctl start supervisord.service
成功后, 就可以通过 supervisorctl 交互命令管理进程脚本了。
读取有更新的配置文件
supervisorctl reread
更新配置文件修改过的程序
supervisorctl update
如果修改过 /etc/supervisord.conf 请使用如下命令
supervisorctl reload
启动, 停止, 重启, 程序。
supervisorctl start 程序名
supervisorctl stop 程序名
supervisorctl restart 程序名
supervisorctl restart all; 重启所有应用
supervisorctl stop all; 停止所有应用
supervisorctl start all; 启动所有应用
四、supervisor图形化管理界面需要开启 /etc/supervisord.conf 文件中的 [inet_http_server]
[inet_http_server]
port=0.0.0.0:9001
username=admin
password=123456
设置完后, 要开放 9001 端口, 并重启 supervisor
># firewall-cmd --zone=public --add-port=9001/tcp --permanent
># firewall-cmd --reload
重启 supervisor
># supervisorctl reload
访问面板
http://192.168.199.128:9001/
账号:admin 密码:123456
获取安装的版本:
> # supervisorctl version
4.2.0
五、安装nginx> # cd ~
> # wget http://nginx.org/download/nginx-1.13.8.tar.gz
> # tar -zxvf nginx-1.13.8.tar.gz
> # cd nginx-1.13.8
> # ./configure --prefix=/usr/local/nginx --with-http_ssl_module
> # make && make install
> # echo $?
0
给Nginx添加启动脚本配置文件, Nginx启动脚本配置文件不自带的, 需要在/etc/init.d目录下创建配置文件nginx。
vim /etc/init.d/nginx
#!/bin/bash
# chkconfig: - 30 21
# description: http service.
# Source Function Library
. /etc/init.d/functions
# Nginx Settings
NGINX_SBIN="/usr/local/nginx/sbin/nginx"
NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
NGINX_PID="/usr/local/nginx/logs/nginx.pid"
RETVAL=0
prog="Nginx"
start()
{
echo -n $"Starting $prog: "
mkdir -p /dev/shm/nginx_temp
daemon $NGINX_SBIN -c $NGINX_CONF
RETVAL=$?
echo
return $RETVAL
}
stop()
{
echo -n $"Stopping $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -TERM
rm -rf /dev/shm/nginx_temp
RETVAL=$?
echo
return $RETVAL
}
reload()
{
echo -n $"Reloading $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -HUP
RETVAL=$?
echo
return $RETVAL
}
restart()
{
stop
start
}
configtest()
{
$NGINX_SBIN -c $NGINX_CONF -t
return 0
}
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
restart
;;
configtest)
configtest
;;
*)
echo $"Usage: $0 {start|stop|reload|restart|configtest}"
RETVAL=1
esac
exit $RETVAL
> # chmod 755 /etc/init.d/nginx
> # chkconfig --add nginx
> # chkconfig nginx on
Nginx本身自带nginx.conf配置文件, 这里不使用它自带的
> # cd /usr/local/nginx/conf
> # mv nginx.conf nginx.conf.bak
> # vim nginx.conf
user nobody nobody;
worker_processes 2;
error_log /usr/local/nginx/logs/nginx_error.log crit;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;
events
{
use epoll;
worker_connections 6000;
}
http
{
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 3526;
server_names_hash_max_size 4096;
log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
' $host "$request_uri" $status'
' "$http_referer" "$http_user_agent"';
sendfile on;
tcp_nopush on;
keepalive_timeout 30;
client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;
connection_pool_size 256;
client_header_buffer_size 1k;
large_client_header_buffers 8 4k;
request_pool_size 4k;
output_buffers 4 32k;
postpone_output 1460;
client_max_body_size 10m;
client_body_buffer_size 256k;
client_body_temp_path /usr/local/nginx/client_body_temp;
proxy_temp_path /usr/local/nginx/proxy_temp;
fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
fastcgi_intercept_errors on;
tcp_nodelay on;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 8k;
gzip_comp_level 5;
gzip_http_version 1.1;
gzip_types text/plain application/x-javascript text/css text/htm
application/xml;
server
{
listen 80;
server_name 192.168.0.33;
index index.html index.htm index.php;
root /usr/local/nginx/html;
location ~ \.php$
{
include fastcgi_params;
fastcgi_pass unix:/tmp/php-fcgi.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
}
}
}
配置环境变量
> # vim /etc/profile.d/nginx.sh
export PATH=/usr/local/nginx/sbin:$PATH
> # source /etc/profile.d/nginx.sh
测试nginx配置语法
> # nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
> # /etc/init.d/nginx restart
开放端口号
> # firewall-cmd --permanent --zone=public --add-port=80/tcp
> # firewall-cmd --reload
浏览器访问:
http://192.168.117.131:9001/
> # vim /etc/supervisord/confs/nginx.conf
[program:nginx]
command = /usr/local/nginx/sbin/nginx -g 'daemon off;'
startsecs = 3
autostart = true
autorestart = true
user = root
stderr_logfile = /tmp/nginx-error.log
stdout_logfile = /tmp/nginx.out.log
修改后记得更新 Supervisor 以及重启 Nginx 进程, 命令:
> # supervisorctl reread # 重新读取配置
> # supervisorctl update # 更新配置
> # supervisorctl reload # 重启 supervisor
六、邮件报警安装superlance
> # pip install superlance
下载和安装sendEmail
> # wget http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1.56.tar.gz
> # tar -zxvf sendEmail-v1.56.tar.gz
> # cp sendEmail-v1.56/sendEmail /usr/bin/sendemail
发送邮件的格式
sendemail -f 发件人邮箱 -t 收件人邮箱 -s 发件人邮箱smtp服务器 -u "sendEmail" -m "haha" -xu 发件人 -xp 发件人邮箱密码
-f 发件人
-t 收件人
-s 发件人smtp服务器
-u 主题
-m 内容
-xu 发件人用户名
-xp 发件人密码
> # vim /etc/supervisord/confs/mail.conf
[eventlistener:crashmail-exited]
command=crashmail -a -s "/usr/bin/sendemail -f xxxxxx@163.com -t xxxxxx@qq.com -s smtp.163.com -u 'nginx' -xu xxxxxxx@163.com -xp xxxxxxxx -m" -m xxxxxxxx@qq.com
events=PROCESS_STATE_EXITED
redirect_stderr=false
问题:
> # supervisorctl reread
Server requires authentication
......
解决方法: 需要输入账号和密码
> # supervisorctl
Server requires authentication
Username:admin
Password:123456
发布日期: 20220623
操作系统: CentOS7.6
PHP版本: PHP7.2.31
测试日期: 20220623