1、Nginx编译部署
下载解压安装包
wget http://nginx.org/download/nginx-1.16.1.tar.gz
tar zxf /opt/nginx-1.16.1.tar.gz -C /opt/
cd nginx-1.16.1
添加用户
useradd -M -s /sbin/nologin nginx
编译安装
./configure --user=nginx --group=nginx --prefix=/data/nginx --with-http_stub_status_module --with-http_ssl_module --with-stream --with-pcre
make && make install
解决依赖关系
错误提示:./configure: error: C compiler cc is not found
#yum -y install gcc gcc-c++ autoconf automake make
错误提示:./configure: error: the HTTP cache module requires md5 functions from OpenSSL library.
#yum -y install openssl openssl-devel
错误提示:./configure: error: the HTTP rewrite module requires the PCRE library.
#yum -y install pcre-devel
初始化配置
ln -s /data/nginx/sbin/* /usr/local/sbin/
chown -R nginx:nginx /data/nginx/
chmod +x /etc/rc.d/rc.local
vim /etc/rc.d/rc.local
添加 /usr/local/sbin/nginx -c /data/nginx/conf/nginx.conf
2、Nginx主备配置
注:使用keepalived抢占模式,Nginx-1节点为更改nginx配置节点。
yum install -y keepalived
cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.init
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id lbh_nginx
script_user root
enable_script_security
}
vrrp_script chk_nginx {
script "/etc/keepalived/check_nginx.sh"
interval 2
}
vrrp_instance VI_1 {
state MASTER ##两个节点都必须是BACKUP状态
#nopreempt ##非抢占模式
interface eth0
virtual_router_id 99
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.130.1.244
}
track_script {
chk_nginx
}
}
router_id nginx_master
script_user root ##解决WARNING告警
nginx-2 把priority 150 替换成 priority 100
把state MASTER 改成BACKUP
vim /etc/keepalived/chk_nginx.sh
#!/bin/bash
counter=`ps -C nginx --no-header |wc -l`