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)裏面

相關文章
相關標籤/搜索