Nginx-Keepalived一键安装脚本
001
#!/bin/bash
002
################################
003
yum install openssl-devel popt-devel gcc pcre-devel -y
004
groupadd www
005
useradd -g www www
006
cd /tmp
007
##############pcre#####################
008
tar -xvf pcre*
009
if [ $? -eq 0 ];then
010
echo "pcre tar ok"
011
else
012
echo "pcre tar no"
013
exit
014
fi
015
016
cd /tmp/pcre-*
017
./configure
018
if [ $? -eq 0 ];then
019
make && make install
020
if [ $? -eq 0 ];then
021
echo "pcre ok"
022
else
023
echo "pcre no"
024
fi
025
else
026
echo "pcre configure no"
027
fi
028
#############nginx######################
029
nginx_dir=/usr/local/nginx
030
nginx_conf=/usr/local/nginx/conf/nginx.conf
031
cd /tmp
032
tar -xvf nginx-*.tar.*
033
if [ $? -eq 0 ];then
034
echo "nginx tar ok"
035
else
036
echo "nginx tar no"
037
exit
038
fi
039
cd /tmp/nginx*
040
./configure --user=www --group=www --prefix=$nginx_dir --with-http_stub_status_module --with-http_ssl_module
041
if [ $? -eq 0 ];then
042
make && make install
043
if [ $? -eq 0 ];then
044
sleep 5
045
echo "nginx make ok"
046
else
047
echo "nginx no"
048
fi
049
else
050
echo "nginx configure no"
051
fi
052
cd $nginx_dir
053
if [ ! -f $nginx_conf ];then
054
touch $nginx_dir/conf/nginx.conf
055
fi
056
######################nginx.conf####################
057
echo -e "user www www;/nworker_processes 8;/npid /usr/local/nginx/logs/nginx.pid;/nworker_rlimit_nofile 51200;/nevents/n}/nuse epoll;/nworker_connections 51200;/n}" > $nginx_conf
058
059
echo -e "http{/ninclude/tmime.types;/ndefault_type application/octet-stream;/nserver_names_hash_bucket_size 128;/nclient_header_buffer_size 32k;/nlarge_client_header_buffers 4 32k;/nclient_max_body_size 8m;/nsendfile on;/ntcp_nopush
060
on;/nkeepalive_timeout 60;" >> $nginx_conf
061
062
echo -e "tcp_nodelay on;/nfastcgi_connect_timeout 300;/nfastcgi_send_timeout 300;/nfastcgi_read_timeout 300;/nfastcgi_buffer_size 64k;/nfastcgi_buffers 4 64k;/nfastcgi_busy_buffers_size 128k;/nfastcgi_temp_file_write_size 128k;/n" >> $nginx_conf
063
064
echo -e "gzip on;/ngzip_min_length 1k;/ngzip_buffers 4 16k;/ngzip_http_version 1.0;/ngzip_comp_level 2;/ngzip_types text/plain application/x-javascript text/css application/xml;/ngzip_vary on/nupstream backend/n{ip_hash;" >> $nginx_conf
065
066
read -p "Requires several server " server
067
for ((a=1;a<=$server;a++))
068
do
069
read -p "server ip address " ip
070
echo -e "server $ip:80;" >> $nginx_conf
071
done
072
073
echo -e "}" >> $nginx_conf
074
075
echo -e 'log_format access $remote_addr - $remote_user [$time_local] $request/n$status $body_bytes_sent $http_referer/n$http_user_agent $http_x_forwarded_for;' >> $nginx_conf
076
077
echo -e "server {/nlisten 80;/nlocation / {/nroot /usr/local/tdoa/nginx/html ;/nindex index.php index.htm index.html;/nproxy_redirect off;/nproxy_set_header Host $host;/nproxy_set_header X-Real-IP $remote_addr;/nproxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;/nproxy_pass http://backend;/n}" >> $nginx_conf
078
079
echo -e "location /nginx {/naccess_log off;/nauth_basic "NginxStatus";/n#auth_basic_user_file /usr/local/nginx/htpasswd;/n}/naccess_log /var/log/access.log access;/n}/n}" >> $nginx_conf
080
###########################keepalived#################################
081
###########################keepalived###############################
082
mandir=/usr/local/src/man
083
kernel=$(uname -r)
084
keepalived_conf=/etc/keepalived/keepalived.conf
085
################################
086
cd /tmp
087
tar -xvf keepalived*.tar.gz
088
cd /tmp/keepalived-*
089
#############keepalived make##################
090
./configure --prefix=/ --mandir=$mandir --with-kernel-dir=/usr/src/kernels/$kernel*
091
if [ $? -eq 0 ];then
092
make
093
if [ $? -eq 0 ];then
094
make install
095
if [ $? -eq 0 ];then
096
echo "keepalived ok"
097
else
098
echo "keepalived no"
099
exit
100
fi
101
else
102
echo "keeplaived make no"
103
exit
104
fi
105
else
106
echo "keelalived make no"
107
fi
108
109
touch /etc/keepalived/keepalived.conf
110
#######################keepalived.conf###################
111
read -p "$read_p NIC binding " network
112
####################keepalived.conf#####################
113
#############Global configuration#######################
114
echo -e " ! Configuration File for keepalived/nglobal_defs {/n/tnotification_email {/n/tjqbash@gmail.com/n/t}/n/tnotification_email_from root@localhost/n/tsmtp_server 127.0.0.1/n/tsmtp_connect_timeout 30/n/trouter_id Nginx-Keepalived/n}" > $keepalived_conf
115
############Virtual IP Configuration####################
116
read -p "Requires several server " server
117
for ((a=1;a<=$server;a++))
118
do
119
read -p "MASTER of BACKUP " states
120
read -p "priority " priority
121
read -p "virtual router id " id
122
read -p "vip address " vip
123
echo -e "vrrp_instance VI_$a {/n/tstate $states/n/tinterface $network/n/tvirtual_router_id $id/n/t priority $priority/n/tadvert_int 1/n/tauthentication {/n/tauth_type PASS/n/tauth_pass tdoa/n/t}/n/tvirtual_ipaddress {/n/t$vip/24 dev $network/n/t}/n}" >> $keepalived_conf
124
done
125
###########################Nginx.sh###############################
126
touch /etc/rc.d/haproxy-keepalived.sh
127
echo -e '#!/bin/bash/nwhile/t:/ndo/n nginxid=`ps -C nginx --no-header | wc -l`/n/tif [ $nginxid -eq 0 ];then/n/t/tusr/local/src/nginx/sbin/nginx/n/t/tsleep 5/n/tnginxid=`ps -C nginx --no-header | wc -l`/n/t/tif [ nginxid -eq 0 ];then/n/t/t/t/t/etc/init.d/keepalived stop/n/t/tfi/n/tfi/n/tsleep 5/n nginxid=`ps -C nginx --no-header | wc -l`/n/tif (($nginxid!= 0));then/n/t/t/etc/init.d/keepalived start/n/tfi/n/tsleep 5/ndone' > /usr/local/nginx/nginx.sh
128
####################################################################
129
nohup sh /usr/local/nginx/nginx.sh &
130
/usr/local/nginx/sbin/nginx
131
/etc/init.d/keepalived restart
132
echo "nohup sh /usr/local/nginx/nginx.sh &" >> /etc/rc.local
133
echo "/usr/local/nginx/sbin/nginx" >> /etc/rc.local
134
echo "/etc/init.d/keepalived restart" /etc/rc.lcoal