CentOS7离线安装supervisor

安装 setuptools

下载setuptools 46.2.0离线包

参考:installing

1
2
3
4
5
6
7
8
9
## 解压
unzip -o -d setuptools-46.2.0 setuptools-46.2.0.zip
# 把 setuptools-46.2.0.zip 文件解压到 setuptools-46.2.0
# -o :不提示的情况下覆盖文件
# -d:将文件解压缩到指定目录下
cd setuptools-46.2.0

## 安装
python setup.py install

需要安装 uzip 命令:

1
2
yum install zip
yum install unzip

从pip下载supervisor

下载supervisor 4.2.0离线包

参考:installing

进入文件所在目录:

1
2
3
4
5
6
## 解压
tar -zxvf supervisor-4.2.0.tar.gz
cd supervisor-4.2.0

## 安装
python setup.py install

验证安装是否成功

1
supervisorctl --help

配置supervisor

1
2
3
4
5
6
7
8
##创建supervisor所需目录
mkdir /etc/supervisord.d/

##创建supervisor配置文件
echo_supervisord_conf > /etc/supervisord.conf

##编辑supervisord.conf文件
vim /etc/supervisord.conf

[include] 标签中添加 /etc/supervisord.d/*.conf

1
2
[include]
files = relative/directory/*.ini /etc/supervisord.d/*.conf

长时间运行会报错 unix:///tmp/supervisor.sock no such file,参考

解决unix-tmp-supervisor-sock-no-such-file

测试supervisor

常用命令

1
2
3
4
5
6
7
supervisord -c /etc/supervisord.conf ## 启动
supervisorctl shutdown ## 关闭
supervisord -c /etc/supervisord.conf ## 通过配置文件启动supervisor
supervisorctl -c /etc/supervisord.conf status ## 查看状态
supervisorctl -c /etc/supervisord.conf reload ## 重新载入配置文件
supervisorctl -c /etc/supervisord.conf start [all]|[x] ## 启动所有/指定的程序进程
supervisorctl -c /etc/supervisord.conf stop [all]|[x] ## 关闭所有/指定的程序进程

部署测试站点

新建 WebApplication1.conf:

1
2
touch /etc/supervisord.d/WebApplication1.conf
vim /etc/supervisord.d/WebApplication1.conf

内容:

注意:supervisor配置文件中的注释使用英文分号 ;

配置文件名需要与program标签内名称一致!!!

asp net core 站点:

1
2
3
4
5
6
7
[program:WebApplication1]
command=/root/dotnet/dotnet WebApplication1.dll
directory=/test/webapi
environment=ASPNETCORE_ENVIRONMENT=Production
stopsignal=INT
stderr_logfile=/var/log/WebApplication1/WebApplication1.err.log
stdout_logfile=/var/log/WebApplication1/WebApplication1.out.log

注意:如果配置Linux开机启动服务报错,则需要配置 dotnet 命令全路径:/root/dotnet/dotnet

supervisor FATAL can't find command 'dotnet'

webapp 站点,使用 npm http-server:

1
2
3
4
5
6
[program:webapp]
command=http-server /test/webapp -p 8013
directory=/test/webapp
stopsignal=INT
stderr_logfile=/var/log/webapp/webapp.err.log
stdout_logfile=/var/log/webapp/webapp.out.log

详细命令参考:Configuration File

查看站点日志:

1
2
vim /var/log/WebApplication1.err.log
vim /var/log/WebApplication1.out.log

配置防火墙

注意:针对各种云,因为有专门策略,所以不需要操作防火墙;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
firewall-cmd --zone=public --add-port=5000/tcp --permanent
firewall-cmd --zone=public --add-port=5001/tcp --permanent
firewall-cmd --reload

## 重新查询端口是否开放
firewall-cmd --query-port=5000/tcp

## 查看监听的端口
netstat -lnpt

#关闭端口
firewall-cmd --zone=public --remove-port=5000/tcp --permanent

## 查看防火墙所有开放的端口
firewall-cmd --zone=public --list-ports

测试站点

1
2
3
4
5
6
7
curl -X GET "http://localhost:5000/weatherforecast" -H "accept: application/json"
curl -X GET "https://127.0.0.1:5001/weatherforecast" -H "accept: application/json"
curl -X GET "http://192.125.30.82:5000/weatherforecast" -H "accept: application/json"

# 查看状态
curl -o /dev/null -s -w %{http_code} -X GET "192.125.30.82:5100/weatherforecast" -H "accept: application/json"
curl -o /dev/null -s -w %{http_code} -X GET "192.125.30.82:8013/pages/tsjb/djsl.html" -H "accept: text/html"

开机启动

脚本:centos-systemd-etcs

1
2
3
4
5
6
7
8
9
[root@host supervisor-4.2.0]# touch /usr/lib/systemd/system/supervisord.service
[root@host supervisor-4.2.0]# vim /usr/lib/systemd/system/supervisord.service
[root@host supervisor-4.2.0]# systemctl enable supervisord
Created symlink from /etc/systemd/system/multi-user.target.wants/supervisord.service to /usr/lib/systemd/system/supervisord.service.
[root@host supervisor-4.2.0]# systemctl is-enabled supervisord
enabled
[root@host supervisor-4.2.0]# systemctl daemon-reload
[root@host supervisor-4.2.0]# systemctl restart supervisord
[root@host supervisor-4.2.0]# systemctl status supervisord

supervisord.service:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# supervisord service for systemd (CentOS 7.0+)
# by ET-CS (https://github.com/ET-CS)
[Unit]
Description=Supervisor daemon

[Service]
Type=forking
ExecStart=/usr/bin/supervisord -c /etc/supervisord.conf
ExecStop=/usr/bin/supervisorctl $OPTIONS shutdown
ExecReload=/usr/bin/supervisorctl $OPTIONS reload
KillMode=process
Restart=on-failure
RestartSec=42s

[Install]
WantedBy=multi-user.target

设置成功后,可以使用下列命令:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 重启指定应用
supervisorctl restart <application name>

# 停止指定应用
supervisorctl stop <application name>

# 启动指定应用
supervisorctl start <application name>

# 查看状态
supervisorctl status

# 重启所有应用
supervisorctl restart all

# 停止所有应用
supervisorctl stop all

# 启动所有应用
supervisorctl start all

参考:

Installing

Supervisor离线安装、管理