Nginx-Keepalived一鍵安裝指令碼 001#!/bin/bash002################################003yum install openssl-devel popt-devel gcc pcre-devel -y004groupadd www005useradd -g www www006cd /tmp007##############pcre#####################008tar -xvf pcre*009if [ $? -eq 0 ];then010 echo "pcre tar ok"011else012 echo "pcre tar no"013 exit014fi015 016cd /tmp/pcre-*017./configure018 if [ $? -eq 0 ];then019 make && make install020 if [ $? -eq 0 ];then021 echo "pcre ok"022 else023 echo "pcre no"024 fi025 else026 echo "pcre configure no"027 fi028#############nginx######################029nginx_dir=/usr/local/nginx030nginx_conf=/usr/local/nginx/conf/nginx.conf031cd /tmp032tar -xvf nginx-*.tar.*033if [ $? -eq 0 ];then034 echo "nginx tar ok"035else036 echo "nginx tar no"037 exit038fi039cd /tmp/nginx*040./configure --user=www --group=www --prefix=$nginx_dir --with-http_stub_status_module --with-http_ssl_module041 if [ $? -eq 0 ];then042 make && make install043 if [ $? -eq 0 ];then044 sleep 5045 echo "nginx make ok"046 else047 echo "nginx no"048 fi049 else050 echo "nginx configure no"051 fi052cd $nginx_dir053 if [ ! -f $nginx_conf ];then054 touch $nginx_dir/conf/nginx.conf055 fi056######################nginx.conf####################057echo -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_conf058 059echo -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_nopush060on;\nkeepalive_timeout 60;" >> $nginx_conf061 062echo -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_conf063 064echo -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_conf065 066read -p "Requires several server " server067for ((a=1;a<=$server;a++))068do069 read -p "server ip address " ip070 echo -e "server $ip:80;" >> $nginx_conf071done072 073echo -e "}" >> $nginx_conf074 075echo -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_conf076 077echo -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_conf078 079echo -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_conf080###########################keepalived#################################081###########################keepalived###############################082mandir=/usr/local/src/man083kernel=$(uname -r)084keepalived_conf=/etc/keepalived/keepalived.conf085################################086cd /tmp087tar -xvf keepalived*.tar.gz088cd /tmp/keepalived-*089#############keepalived make##################090./configure --prefix=/ --mandir=$mandir --with-kernel-dir=/usr/src/kernels/$kernel*091 if [ $? -eq 0 ];then092 make093 if [ $? -eq 0 ];then094 make install095 if [ $? -eq 0 ];then096 echo "keepalived ok"097 else098 echo "keepalived no"099 exit100 fi101 else102 echo "keeplaived make no"103 exit104 fi105 else106 echo "keelalived make no"107 fi108 109touch /etc/keepalived/keepalived.conf110#######################keepalived.conf###################111read -p "$read_p NIC binding " network112####################keepalived.conf#####################113#############Global configuration#######################114echo -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_conf115############Virtual IP Configuration####################116read -p "Requires several server " server117for ((a=1;a<=$server;a++))118do119read -p "MASTER of BACKUP " states120read -p "priority " priority121read -p "virtual router id " id122read -p "vip address " vip123echo -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_conf124done125###########################Nginx.sh###############################126touch /etc/rc.d/haproxy-keepalived.sh127echo -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.sh128####################################################################129nohup sh /usr/local/nginx/nginx.sh &130/usr/local/nginx/sbin/nginx131/etc/init.d/keepalived restart132echo "nohup sh /usr/local/nginx/nginx.sh &" >> /etc/rc.local133echo "/usr/local/nginx/sbin/nginx" >> /etc/rc.local134echo "/etc/init.d/keepalived restart" /etc/rc.lcoal