#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

# Check if user is root
if [ $(id -u) != "0" ]; then
    echo "Error: You must be root to run this script, please use root to install"
    exit 1
fi
clear

printf "
#################################################################
#                                                               #      
#        Automatic Keepalived+Ipvsadm+Nginx Installation        #
#          For Keepalived Master On CentOS x86_64               #
#                    Version: 11.12.630                         #
#                    Author: Rex Chen                           #
#                  Website: http://rex.la                       # 
#                                                               #      
#################################################################
"
printf "
=========================================================================
Default Settings
The Master Keepalived IP=192.168.203.111
The Slave Keepalived IP=192.168.203.112
The Master Nginx IP=192.168.203.103:80
The Slave Nginx IP=192.168.203.104:80
Visual IP=192.168.203.113
Be Sure Your Kernel Version is 2.6.32-71.29.1.el6.x86_64
=========================================================================
"
MK=192.168.203.111
SK=192.168.203.112
MN=192.168.203.103:80
SN=192.168.203.104:80
VIP=192.168.203.113

	get_char()
	{
	SAVEDSTTY=`stty -g`
	stty -echo
	stty cbreak
	dd if=/dev/tty bs=1 count=1 2> /dev/null
	stty -raw
	stty echo
	stty $SAVEDSTTY
	}
	echo ""
	echo "Press any key to start..."
	char=`get_char`
	
clear
rpm -qa|grep  httpd
rpm -e httpd
yum -y remove httpd
yum -y install yum-fastestmirror wget make tar gcc* flex bison file libtool libtool-libs kernel-devel libxml2 libxml2-devel zlib zlib-devel glib2 glib2-devel bzip2 bzip2-devel libevent libevent-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel vim-minimal nano fonts-chinese gettext gettext-devel ncurses-devel gmp-devel pspell-devel

rm -rf /etc/localtime
ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

yum install -y ntp
ntpdate -d cn.pool.ntp.org
date

if [ -s /etc/selinux/config ]; then
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
fi

cur_dir=$(pwd)
wget -c http://nginx.org/download/nginx-0.8.54.tar.gz
wget -c http://soft.vpser.net/web/pcre/pcre-8.12.tar.gz
wget -c http://rpm5.org/files/popt/popt-1.16.tar.gz
wget -c http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz
wget -c http://www.keepalived.org/software/keepalived-1.2.2.tar.gz

clear
echo "========================================================================="
echo "Nginx for CentOS/RadHat Linux "
echo "========================================================================="
echo ""
echo "Installing Nginx"
echo ""
echo "========================================================================="
cd $cur_dir

groupadd www
useradd -s /sbin/nologin -g www www
mkdir -p /home/wwwroot
chmod +w /home/wwwroot
mkdir -p /home/wwwlogs
chmod 777 /home/wwwlogs
chown -R www:www /home/wwwroot

cd $cur_dir
tar zxvf pcre-8.12.tar.gz
cd pcre-8.12/
./configure
make && make install
cd ../

cd $cur_dir
tar zxvf nginx-0.8.54.tar.gz
cd nginx-0.8.54/
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-ipv6
make && make install
cd ../

cd $cur_dir
rm -rf /usr/local/nginx/conf/nginx.conf
touch /usr/local/nginx/conf/nginx.conf
cat >>/usr/local/nginx/conf/nginx.conf<<EOF
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    upstream backend
    {
	server $MN;
	server $SN;
    }
    server {
        listen       80;
        server_name  $MK;
        location / {
            root   html;
            index  index.html index.htm;	    
	    proxy_pass http://backend;
	    proxy_redirect default;  	    
        }    
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
EOF
wget -c http://soft.vpser.net/lnmp/ext/init.d.nginx
cp init.d.nginx /etc/init.d/nginx
chmod +x /etc/init.d/nginx
/etc/init.d/nginx start

clear
echo "========================================================================="
echo "Popt 1.16 for CentOS/RadHat Linux "
echo "========================================================================="
echo ""
echo "Installing Popt 1.16"
echo ""
echo "========================================================================="
cd $cur_dir
tar -zxvf popt-1.16.tar.gz
cd popt-1.16
chmod 777 ./configure
./configure
make && make install

clear
echo "========================================================================="
echo "Ipvsadm 1.24 for CentOS/RadHat Linux "
echo "========================================================================="
echo ""
echo "Installing Ipvsadm 1.24"
echo ""
echo "========================================================================="
cd $cur_dir
tar -zxvf ipvsadm-1.24.tar.gz
cd ipvsadm-1.24
ln -s /usr/src/kernels/2.6.32-71.29.1.el6.x86_64 /usr/src/linux
make && make install
clear

echo "========================================================================="
echo "Keepalived 1.2.2 for CentOS/RadHat Linux "
echo "========================================================================="
echo ""
echo "Installing Keepalived 1.2.2"
echo ""
echo "========================================================================="
cd $cur_dir
tar -zxvf keepalived-1.2.2.tar.gz
cd keepalived-1.2.2
chmod 777 ./configure
./configure -prefix=/usr/local/keepalived
make && make install
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
chmod +x /etc/init.d/keepalived
clear

mkdir /etc/keepalived
rm -rf /etc/keepalived/keepalived.conf
touch /etc/keepalived/keepalived.conf
cat >>/etc/keepalived/keepalived.conf<<EOF
global_defs {  
   notification_email {  
         noreply@rex.la  
   }  
   notification_email_from noreply@rex.la 
   smtp_server 127.0.0.1  
   smtp_connect_timeout 30  
   router_id LVS_DEVEL  
}

vrrp_sync_group VGM {
group {
  VI_1
  }
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {  
         auth_type PASS  
         auth_pass 1111  
     }  
    virtual_ipaddress {
        $VIP
    }
}
EOF

/sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up
service keepalived start
chkconfig keepalived on
chkconfig --level 345 nginx on

clear
echo "========================================================================="
echo "ALL DONE "
echo "========================================================================="
echo ""
echo "Automatic Keepalived+Ipvsadm+Nginx Installation is Completed"
echo ""
echo "========================================================================="
