nginx+keepalived部署

Nginxphp

下载html

wget http://nginx.org/download/nginx-1.9.9.tar.gznginx

依赖包安装web

yum install openssl openssl-devel pcre pcre-devel zlib zlib-devel gcc gd-develcentos

ssl功能:openssl gzip模块:zlib rewrite:pcrebash

php处理图形的扩展库:gd
编译服务器

安装 nginx-1.9.9.tar.gz(返回localhost)ide

tar -zxvf nginx-1.9.9.tar.gzui

cd nginx-1.9.9centos7

./configure

make && make install

启动

/usr/local/nginx/sbin/nginx

nginx默认为80端口,若访问该服务器IP未能访问,能够查看80端口是否被占用,或者防火墙未开放80端口

文档配置

在http{}里添加

upstream abc{
ip_hash;
server 192.168.1.1 weight=1; #反向代理的IP,及权重
server 192.168.1.2 weight=1;
}
server {
listen 80; 侦听端口
server_name www.abc.com abc.com; 服务器解析网址
if ($host = 'abc.com' ) {
rewrite ^/(.*)$ http://www.abc.com/$1 permanent;
}
#charset koi8-r;
#access_log logs/host.access.log main;

location / {
# root html;
#index index.html index.asp;
proxy_pass http://abc; #为upstream 的名称
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 300m;
client_body_buffer_size 128k;
proxy_connect_timeout 600;
proxy_read_timeout 600;
proxy_send_timeout 600;
proxy_buffer_size 64k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}

还有修改配置的pid路径为

pid=/var/run/nginx.pid

保存退出

建立pid文件

vi /var/run/nginx.pid

保存退出

检查配置文件是否正确

/usr/local/nginx/sbin/nginx -t

编写启动脚本

vi /etc/init.d/nginx

#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# it is v.0.0.2 version.
# chkconfig: - 85 15
# description: Nginx is a high-performance web and proxy server.
# It has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /var/run/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf
nginxd=/usr/local/nginx/sbin/nginx
nginx_config=/usr/local/nginx/conf/nginx.conf
nginx_pid=/var/run/nginx.pid
RETVAL=0
prog="nginx"
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
[ -x $nginxd ] || exit 0
# Start nginx daemons functions.
start() {
if [ -e $nginx_pid ];then
echo "nginx already running...."
exit 1
fi
echo -n $"Starting $prog: "
daemon $nginxd -c ${nginx_config}
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
return $RETVAL
}
# Stop nginx daemons functions.
stop() {
echo -n $"Stopping $prog: "
killproc $nginxd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid
}
# reload nginx service functions.
reload() {
echo -n $"Reloading $prog: "
#kill -HUP `cat ${nginx_pid}`
killproc $nginxd -HUP
RETVAL=$?
echo
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
stop
start
;;
status)
status $prog
RETVAL=$?
;;
*)
echo $"Usage: $prog {start|stop|restart|reload|status|help}"
exit 1
esac
exit $RETVAL

赋予权限

chmod a+x /etc/init.d/nginx
重启

/etc/init.d/nginx restart

若重启nginx不成功能够重启电脑,或者

lsof -i:80

查出pid后用kill杀掉,再启动一次就能够了

访问nginx  的ip,出现欢迎页便可

keepalived

下载

http://www.keepalived.org/software/keepalived-1.4.5.tar.gz

编译

tar -zxvf keepalived-1.4.5.tar.gz

cd keepalived-1.4.5/

./configure

make && make install

建立/etc/keepalived 放置keepalived.conf

mkdir /etc/keepalived

cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/

修改keepalived.conf配置

主节点

global_defs {

   notification_email {

       #报错后发送的邮箱

   }

   notification_email_from Alexandre.Cassen@firewall.loc

   smtp_server 127.0.0.1

   smtp_connect_timeout 30

   #标记

   router_id nginx_01

   #注释掉,貌似影响虚拟IP通信

   #vrrp_skip_check_adv_addr

   #vrrp_strict

   #vrrp_garp_interval 0

   #vrrp_gna_interval 0

}

 

vrrp_instance VI_1 {

#主节点,最好是两台都配置为BACKUP,这样主节点出事故重启后不会抢占虚拟IP

state MASTER

#网卡名

interface ens33

virtual_router_id 51

#权重

priority 200

#本机IP

unicast_src_ip 192.168.11.10

#其余服务器IP

    unicast_peer {

        192.168.11.20

    }

    advert_int 1

    authentication {

        auth_type PASS

        #校验密码,每台keepalived都必须同样

        auth_pass abc

    }

    virtual_ipaddress {

        #虚拟ip必须与两台keepalived服务器同一个网段

        192.168.11.200

    }

}

备用机

lobal_defs {

   notification_email {

       #邮箱

   }

   notification_email_from Alexandre.Cassen@firewall.loc

   smtp_server 172.0.0.1

   smtp_connect_timeout 30

   router_id nginx_02

   #vrrp_skip_check_adv_addr

   #vrrp_strict

   #vrrp_garp_interval 0

   #vrrp_gna_interval 0

}

 

vrrp_instance VI_1 {

state BACKUP

    interface ens33

    virtual_router_id 51

    priority 100

    unicast_src_ip 192.168.11.20

    unicast_peer {

        192.168.11.10

    }

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass abc

    }

    virtual_ipaddress {

        192.168.11.200

    }

}

建立启动脚本

vi  /etc/init.d/keepalived

#!/bin/sh
#
# keepalived High Availability monitor built upon LVS and VRRP
#
# chkconfig: - 86 14
# description: Robust keepalive facility to the Linux Virtual Server project \
# with multilayer TCP/IP stack checks.

### BEGIN INIT INFO
# Provides: keepalived
# Required-Start: $local_fs $network $named $syslog
# Required-Stop: $local_fs $network $named $syslog
# Should-Start: smtpdaemon httpd
# Should-Stop: smtpdaemon httpd
# Default-Start:
# Default-Stop: 0 1 2 3 4 5 6
# Short-Description: High Availability monitor built upon LVS and VRRP
# Description: Robust keepalive facility to the Linux Virtual Server
# project with multilayer TCP/IP stack checks.
### END INIT INFO

# Source function library.
. /etc/rc.d/init.d/functions

exec="/usr/local/sbin/keepalived"
prog="keepalived"
config="/etc/keepalived/keepalived.conf"

[ -e /usr/local/etc/sysconfig/$prog ] && /usr/local/etc/sysconfig/$prog

lockfile=/var/lock/subsys/keepalived

start() {
[ -x $exec ] || exit 5
[ -e $config ] || exit 6
echo -n $"Starting $prog: "
daemon $exec $KEEPALIVED_OPTIONS
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}

stop() {
echo -n $"Stopping $prog: "
killproc $prog
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}

restart() {
stop
start
}

reload() {
echo -n $"Reloading $prog: "
killproc $prog -1
retval=$?
echo
return $retval
}

force_reload() {
restart
}

rh_status() {
status $prog
}

rh_status_q() {
rh_status &>/dev/null
}


case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
restart
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
exit 2
esac
exit $?

保存退出

赋予权限

chmod a+x /etc/init.d/keepalived

chmod a+x /usr/local/etc/sysconfig/keepalived

chmod a+x /usr/local/sbin/keepalived

启动

/etc/init.d/keepalived start

或者 service keepalived start

centos7以上的也能够用systemctl start keepalived

查看进程,有3个进程表示正确

ps -ef|grep keep|grep -v grep

使用 ip a  看虚拟IP是否绑定

中止

/etc/init.d/keepalived stop

或者 service keepalived stop

centos7以上的也能够用systemctl stop keepalived

注:Keepalived部署到服务器上面时须要3个相同IP段的服务器,两台部署keepalived,其中一台的IP在keepalived的配置里填进虚拟IP(vip)里面

相关文章
相关标签/搜索