nginx安装
1. 前置依赖安装
yum install gcc gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel -y
2. 编译安装nginx
nginx下载地址:
https://nginx.org/en/download.html
## 安装包位置:信息港16.11:/root/shl
tar xvf nginx-1.20.2.tar.gz
tar xvf nginx-module-vts-0.2.2.tar.gz
mv nginx-module-vts-0.2.2 nginx-module-vts
cd nginx-1.20.2
#编译时注意修改 nginx-module-vts的路径
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-stream --with-http_v2_module --add-module=/root/nginx-module-vts
make && make install
3.配置systemd启动
cat >> /lib/systemd/system/nginx.service << EOF
[Unit]
Description=nginx service
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl start nginx
systemctl enable nginx
4. 添加监控
vim /usr/local/nginx/conf/nginx.conf
加在http模块:
vhost_traffic_status_zone;
加在server模块:
location /status {
vhost_traffic_status_display;
vhost_traffic_status_display_format html;
}
location /nginx-status {
stub_status on;
}
5.配置keepalived
yum -y install keepalived
cat /etc/keepalived/keepalived.conf
global_defs {
router_id LVS_100
}
vrrp_script check_nginx {
script "/etc/keepalived/check_nginx.sh"
interval 2
weight -20
fall 2
rise 1
}
vrrp_instance VI_1 {
state MASTER ## 另一台修改成BACKUP
nopreempt
interface enp4s1 ## 修改为本机的网卡名
virtual_router_id 100 ## 两个keepalived一样,可以写成vip后面几位
priority 100 ## 另一台优先级修改成50
advert_int 1
authentication {
auth_type PASS
auth_pass just0kk
}
virtual_ipaddress {
172.22.143.100/24 ## 修改vip
}
track_script {
check_nginx
}
}
6.检测脚本
cat /etc/keepalived/check_nginx.sh
#!/bin/bash
run=`ps -C nginx --no-heading|wc -l`
if [ $run -eq 0 ]; then
systemctl stop keepalived
fi
chmod + x /etc/keepalived/check_nginx.sh
7.测试:
systemctl start keepalived
关闭nginx看vip是否漂移
重启nginx和keepalived