Nginx的安装
bubtu平台编译环境可以使用以下指令apt-get install build-essentialapt-get install libtoolcentos平台编译环境使用如下指令安装make:yum -y install gcc automake autoconf libtool make安装g++:yum install gcc gcc-c++</span>5.2选定源码目录
可以是任何目录,本文选定的是/usr/local/src
cd /usr/local/src
5.3安装PCRE库
1)下载最新的 PCRE 源码包,使用下面命令下载编译和安装 PCRE 包:
<span style="font-size:18px;">2)cd /usr/local/src
3)wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.34.tar.gz
tar -zxvf pcre-8.34.tar.gz
cd pcre-8.34
./configure
make
make install</span>
5.4安装zlib库
<span style="font-size:18px;">1)http://zlib.net/zlib-1.2.8.tar.gz 下载最新的 zlib 源码包,使用下面命令下载编译和安装 zlib包:
2)cd /usr/local/src
3)wget http://zlib.net/zlib-1.2.8.tar.gz
4)tar -zxvf zlib-1.2.8.tar.gz
5)cd zlib-1.2.8
6)./configure
7)make
8)make install</span>
5.5安装ssl(某些vps默认没装ssl)
<span style="font-size:18px;">cd /usr/local/src
wget http://www.openssl.org/source/openssl-1.0.1c.tar.gz
tar -zxvf openssl-1.0.1c.tar.gz</span>
<span style="font-size:18px;">cd /usr/local/src
wget http://www.openssl.org/source/openssl-1.0.1c.tar.gz
tar -zxvf openssl-1.0.1c.tar.gz
</span>
./configuremake && make install
5.6安装nginx
Nginx 一般有两个版本,分别是稳定版和开发版,您可以根据您的目的来选择这两个版本的其中一个,下面是把 Nginx 安装到 /usr/local/nginx 目录下的详细步骤:
<span style="font-size:18px;">cd /usr/local/src
wget http://nginx.org/download/nginx-1.4.2.tar.gz
tar -zxvf nginx-1.4.2.tar.gz
cd nginx-1.4.2
./configure --sbin-path=/usr/local/nginx/nginx \
--conf-path=/usr/local/nginx/nginx.conf \
--pid-path=/usr/local/nginx/nginx.pid \
--with-http_ssl_module \
--with-pcre=/usr/local/src/pcre-8.34 \
--with-zlib=/usr/local/src/zlib-1.2.8 \
--with-openssl=/usr/local/src/openssl-1.0.1c
make
make install</span>
--with-pcre=/usr/src/pcre-8.34 指的是pcre-8.34 的源码路径。
--with-zlib=/usr/src/zlib-1.2.7 指的是zlib-1.2.7 的源码路径。
安装成功后 /usr/local/nginx 目录下如下
fastcgi.conf koi-win nginx.conf.default
fastcgi.conf.default logs scgi_params
fastcgi_params mime.types scgi_params.default
fastcgi_params.default mime.types.default uwsgi_params
html nginx uwsgi_params.default
koi-utf nginx.conf win-utf
5.6启动
1)确保系统的 80 端口没被其他程序占用,运行/usr/local/nginx/nginx 命令来启动 Nginx
netstat -ano|grep 80
2)打开浏览器访问此机器的 IP,如果浏览器出现 Welcome to nginx! 则表示 Nginx 已经安装并运行成功
keepalived安装
<span style="font-size:18px;"> yum install openssl-devel
cd /tmp
wget http://www.keepalived.org/software/keepalived-1.2.2.tar.gz
tar xzf keepalived-1.2.2.tar.gz
cd keepalived-1.2.2
./configure
make && make install
cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/
cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
chmod +x /etc/init.d/keepalived
chkconfig --add keepalived
chkconfig keepalived on
mkdir /etc/keepalived
ln -s /usr/local/sbin/keepalived /usr/sbin/</span>
keepalived的配置
更详细的keepalived配置文件说明可以执行man keepalived.conf查看。
我们假设主服务器IP:192.168.1.103,从服务器ip:192.168.1.101 虚拟ip:192.168.1.110
下面对主服务器的keepalived进行配置:
<span style="font-size:18px;"> vi /etc/keepalived/keepalived.conf
global_defs {
notification_email {
admin@centos.bz
}
notification_email_from keepalived@domain.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_http_port {
script "/opt/nginx_pid.sh"
interval 2
weight 2
}
vrrp_instance VI_1 {
state MASTER ############ 辅机为 BACKUP
interface eth0
virtual_router_id 51
mcast_src_ip 192.168.1.103
priority 102 ########### 权值要比 back 高
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_http_port ### 执行监控的服务
}
virtual_ipaddress {
192.168.1.110
}
}</span>
从服务器:
global_defs {
notification_email {
admin@centos.bz
}
notification_email_from keepalived@domain.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_http_port {
script "/opt/nginx_pid.sh"
interval 2
weight 2
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
mcast_src_ip 192.168.1.101
priority 101 ##########权值 要比 master 低。。
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_http_port ### 执行监控的服务
}
virtual_ipaddress {
192.168.1.110
}
}
之后分别在主从服务器建立nginx的监控脚本:
三、分别在主和辅服务器上建立nginx的监控脚本
Keepalived管理虚IP是通过组播和优先级实现的,优先级高的会拥有虚IP的管理权,所以要实现IP自动切换,只需要建立一个监控脚本,让keepalived进行调用,一旦发现nginx出现故障就kill掉keepalived的进程,让其他keepalived管理此IP
vi /opt/nginx_pid.sh
#!/bin/bash
A=`ps -C nginx --no-header |wc -l`
if [ $A -eq 0 ];then
/usr/local/nginx/sbin/nginx
sleep 3
if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
killall keepalived
fi
fi
然后我们在主、从服务器上分别增加虚拟ip
ifconfig eth0:1 192.168.1.110 netmask 255.255.255.0
然后分别启动主从服务器的keepalived:
service keepalived start
keepalived的测试
我们在主服务器上执行命令ip a,显示如下:
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 00:0c:29:aa:a1:e4 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.103/24 brd 255.255.255.255 scope global eth0
inet 192.168.1.110/32 scope global eth0
证明主服务器已经绑定了虚拟ip 192.168.1.110
在从服务器上执行命令ip a,显示如下:
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 00:0c:29:2b:94:3b brd ff:ff:ff:ff:ff:ff
inet 192.168.1.101/24 brd 255.255.255.255 scope global eth0
显示表明从服务器上没有绑定vip 192.168.1.110,只有本机真实ip192.168.1.101
下面我们停止主服务器的nginx进程,再看看ip绑定情况:
主服务器的情况:
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 00:0c:29:aa:a1:e4 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.103/24 brd 255.255.255.255 scope global eth0
从服务器的情况:
<span style="font-size:18px;"> 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 00:0c:29:2b:94:3b brd ff:ff:ff:ff:ff:ff
inet 192.168.1.101/24 brd 255.255.255.255 scope global eth0
inet 192.168.1.110/32 scope global eth0</span>