nginx+keepalived搭建
搭建环境
系统版本:CentOS Linux release 7.9.2009 (Core)
nginx版本:nginx-1.12.2.tar.gz
keepalived版本:keepalived-1.2.13.tar.gz
服务器4台
设备1:192.168.217.15 安装nginx keepalived
设备2:192.168.217.16 安装 nginx keepalived
设备3:192.168.217.17 安装httpd*
设备4:192.168.217.18 安装httpd*
设备1、设备2
安装ngixn keepalived
nginx
[root@localhost ~]# yum -y install gcc gcc-c++ zlib zlib-devel pcre pcre-devel openssl openssl-devel
[root@localhost ~]# tar -zxf nginx-1.12.2.tar.gz -C /usr/src
[root@localhost ~]# cd /usr/src/nginx-1.12.2/
[root@localhost nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
[root@localhost nginx-1.12.2]# make && make install
[root@localhost nginx-1.12.2]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/
[root@localhost nginx-1.12.2]# vim /usr/local/nginx/conf/nginx.conf
35行添加 (反向代理)
upstream apache{
server 192.168.217.17 max_fails=2 fail_timeout=30; #两台Apache的IP
server 192.168.217.18 max_fails=2 fail_timeout=30;
}
……
location / {
# root html; #注释掉
# index index.html index.htm; #注释掉
proxy_pass http://apache; #添加此信息 (负载均衡)
}
[root@localhost nginx-1.12.2]# vim /etc/rc.d/init.d/nginx
#!/bin/bash
#chkconfig: 35 85 21
case $1 in
start)
/usr/local/sbin/nginx
;;
stop)
killall -9 nginx
rm -f /var/run/nginx.pid
;;
restart)
$0 stop
$0 start
;;
*)
echo "start|stop|restart"
;;
esac
[root@localhost nginx-1.12.2]# chmod +x /etc/rc.d/init.d/nginx
[root@localhost nginx-1.12.2]# chkconfig --add nginx
[root@localhost nginx-1.12.2]# chkconfig nginx on
[root@localhost nginx-1.12.2]# systemctl restart nginx
Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.
此处如有报错 检查配置文件或杀掉进程
root@localhost nginx-1.12.2]# killall -9 nginx
检查配置文件
[root@localhost nginx-1.12.2]# nginx -t
重启
[root@localhost nginx-1.12.2]# nginx
或
[root@localhost nginx-1.12.2]# systemctl restart nginx
查看状态
root@localhost nginx-1.12.2]# systemctl status nginx.service
[root@localhost keepalived-1.2.13]# systemctl stop firewalld
安装keepalived依赖
[root@localhost ~]# yum -y install popt