nginx源码安装
systemctl stop firewalld
mkdir /opt
cd /opt
yum install -y gcc gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel psmisc
wget http://nginx.org/download/nginx-1.8.1.tar.gz
tar zxvf nginx-1.8.1.tar.gz
cd nginx-1.8.1
./configure --prefix=/usr/local/nginx
make && make install
/usr/local/nginx/sbin/nginx #启动
/usr/local/nginx/sbin/nginx -s stop/reload
/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/conf/nginx.conf #配置文件
/usr/local/nginx/html/index.html #首页
vim /lib/systemd/system/nginx.service #添加服务,可不添加
[Unit]
Description=nginx
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
pkill nginx
systemctl stop nginx
systemctl start nginx
systemctl reload nginx
systemctl enable nginx #开机启动
keepalived安装
yum -y install keepalived systemctl enable keepalived systemctl start keepalived
keepalived master配置
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id master
}
vrrp_script chk_nginx {
script "/etc/keepalived/nginx_check.sh"
interval 2
weight -20
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
mcast_src_ip 192.168.163.100
priority 100
nopreempt
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_nginx
}
virtual_ipaddress {
192.168.163.200
}
}
keepalived backup配置
router_id slave
mcast_src_ip 192.168.163.110
state BACKUP
priority 99
配置nginx检测脚本
vim /etc/keepalived/nginx_check.sh
#!/bin/bash
A=`ps -C nginx --no-header | wc -l`
if [ $A -eq 0 ];then
/usr/local/nginx/sbin/nginx #尝试重新启动nginx
sleep 2 #睡眠2秒
if [ $A -eq 0 ];then
killall keepalived #启动失败,将keepalived服务杀死。将vip漂移到其它备份节点
fi
fi
systemctl restart keepalived
测试
systemctl stop keepalived
/usr/local/nginx/sbin/nginx -s stop
IP add #查看VIP
systemctl restart keepalived