#!/bin/bash
##################################################
## Author : shanks
## Create Date: 2011-06-29
## Modify Date: 2011-07-01
## 自动安装配置nginx、tomcat
##################################################

##颜色定义
Red=”\E[31;40m"; Green="\E[32;40m"; Blue="\E[34;40m";Yellow="\E[33;40m"; C_begin0="\033[1m"; C_begin1="\033[1;5m"; C_end="\033[0m"

###zhidao上面/home/wangpo/nginx做下载服务,这些是安装文件
NGINX_SOURCE="http://bb-art-zhidao01.bb01:8127/soft/nginx/nginx-1.0.0.tar.gz"
JVM_SOURCE="http://bb-art-zhidao01.bb01:8127/soft/nginx/nginx-upstream-jvm-route-0.1.tar.gz"
PCRE_SOURCE="http://bb-art-zhidao01.bb01:8127/soft/nginx/pcre-8.02.tar.gz"
JDK_source="http://bb-art-zhidao01.bb01:8127/soft/jdk/jdk1.6.0_13-linux-x64.bin"
TOMCAT_SOURCE="http://bb-art-zhidao01.bb01:8127/soft/tomcat/apache-tomcat-6.0.0.tar.gz"

###配置文件和控制文件
NGINX_CONF="http://bb-art-zhidao01.bb01:8127/conf/nginx.conf"
TOMCAT_CONF="http://bb-art-zhidao01.bb01:8127/conf/server.xml"
NGINX_CTL="http://bb-art-zhidao01.bb01:8127/conf/nginxctl.sh"
TOMCAT_CTL="http://bb-art-zhidao01.bb01:8127/conf/catalina.sh"

###日志,记录下每一步操作
LOG=/home/wangpo/auto_install.log
TIME=`date +%Y-%m-%d=!=%H:%M:%S`

USAGE(){
echo -e "${C_begin0}${Blue}hey man,what are you doing here!!!${C_end}"
exit 1;
}

##确定运行用户,根据/home下是否存在该用户名的目录,判断用户是否存在
USER(){
echo -e "${C_begin0}${Green}====================================BEGIN USER CREATE==============================++${C_end}"
echo -e "${C_begin0}${Blue}give me a username you what that to run you application${C_end}"
read -p "now:" User
if [ -d /home/${User} ]
then
echo -e “${C_begin0}${Red}${User} is exist,do you want to continue${C_end}”;
read -p “yes|no:” user_continue
case ${user_continue} in
yes|y) :;
echo “${TIME} ${User} is exist && you go” >> ${LOG};;
no|n) echo “bye” && exit 1;
echo “${TIME} ${User} is exist && exit” >> ${LOG};;
esac
else
/usr/sbin/useradd ${User} && echo -e “${C_begin0}${Red}${User} create OK ${C_end}”;
cd /home/${User} && /bin/mkdir src && /bin/chown -R ${User}.${User} /home/${User};
echo “${TIME} ${User} create ok && src create ok” > ${LOG}
fi
echo -e “${C_begin0}${Red}====================================++CREATE USER FINISHED=====================++${C_end}”;
echo “”;
echo “”;
}

###下载nginx还是tomcat,下载至定义好的用户的家目录下的src下,不会重复下载
WHICH_ONE(){
echo -e “${C_begin0}${Green}==============================++BEGIN CHOOSE APPLICATION=====================++${C_end}”;
unset WHICH
until [[ "$WHICH" == nginx || "$WHICH" == n || "$WHICH" == tomcat || "$WHICH" == t || "$WHICH" == q ]];
do
echo -e “${C_begin0}${Blue}you want to install [nginx|tomcat] you can give me ‘q’ to exit${C_end}”
read -p “[nginx(n)|tomcat(t)|q]:” WHICH
done
echo “”;
echo “ download now,please wait ”
case ${WHICH} in
nginx|n) if [ -f /home/${User}/src/${NGINX_SOURCE##*/} -a -f /home/${User}/src/${JVM_SOURCE##*/} -a -f /home/${User}/src/${PCRE_SOURCE##*/} ]
then
echo -e “${C_begin0}${Yellow}${NGINX_SOURCE##*/}${C_end} and ${C_begin0}${Yellow}${JVM_SOURCE##*/}${C_end} and ${C_begin0}${Yellow}${PCRE_SOURCE##*/}${C_end} is exist,go on”
echo “${TIME} ${NGINX_SOURCE##*/} and ${JVM_SOURCE##*/} and ${PCRE_SOURCE##*/} is exist,go on” >> ${LOG}
else
cd /home/${User}/src && /usr/bin/wget -q ${NGINX_SOURCE} ${JVM_SOURCE} ${PCRE_SOURCE} ${NGINX_SOURCE}.md5 ${JVM_SOURCE}.md5 ${PCRE_SOURCE}.md5 && echo “${TIME} ${WHICH} soft download OK” >> ${LOG}
fi;;
tomcat|t) if [ -f /home/${User}/src/${JDK_source##*/} -a -f /home/${User}/src/${TOMCAT_SOURCE##*/} ]
then
echo -e “${C_begin0}${Yellow}${JDK_source##*/}${C_end} and ${C_begin0}${Yellow}${TOMCAT_SOURCE##*/}${C_end} is exist,go on”
echo “${TIME} ${JDK_source##*/} and ${TOMCAT_SOURCE##*/} is exist,go on”>> ${LOG}
else
cd /home/${User}/src && /usr/bin/wget -q ${JDK_source} ${TOMCAT_SOURCE} ${JDK_source}.md5 ${TOMCAT_SOURCE}.md5 && echo “${TIME} ${WHICH} soft download OK” >> ${LOG}
fi;;
q) echo “${TIME} soft download break” >> ${LOG}
exit 1;;
esac
sleep 10;
echo -e “${C_begin0}${Red}=================================CHOOSE FINISHED==============================${C_end}”;
echo “”;
echo “”;
}

###是个二级主目录,调用USER和WHICH_ONE
GET_SOFT(){
USER;
sleep 2;
WHICH_ONE;
}

###是个二级主目录,校验下载下来的源码包,注意查看输出,如果出现校验失败,按照提示,退出程序,但是用户不会删除,源码包不会删除
MD5_CHECK(){
echo -e “${C_begin0}${Green}===========================+BEGIN MD5 CHECK==============================++${C_end}”
sleep 2;
cd /home/${User}/src
for MD5_FILE in `ls /home/${User}/src/*.md5`
do
/usr/bin/md5sum -wc ${MD5_FILE}
done
echo -e “${C_begin0}${Red}===========================+MD5 CHECK FINISHED==============================++${C_end}”
echo “”;
echo “”;

unset GO
until [[ "$GO" == y || "$GO" == yes || "$GO" == n || "$GO" == no ]];
do
echo -e “${C_begin0}${Blue}next,we while install ${WHICH} ,here we go?${C_end}”
read -p “[yes|no]:” GO
done
case ${GO} in
y|yes) :;;
n|no) exit 1;;
esac
echo “”;
echo “”;
}

###自动安装pcre至/home/${User}/pcre,判断pcre是否存在,避免重复安装,出现故障
PCRE_INSTALL(){
echo -e “${C_begin0}${Blue}first,we install PCRE${C_end}”
PCRE=${PCRE_SOURCE##*/}
if [ -d /home/${User}/src/${PCRE%%.tar.gz} ]
then
echo -e “${C_begin0}${Red}/home/${User}/src/${PCRE%%.tar.gz} is exist && we don’t install it again${C_end}”
echo “${TIME} /home/${User}/src/${PCRE%%.tar.gz} is exist && we don’t install it again” >> ${LOG};
else
cd /home/${User}/src && /bin/tar zxf ${PCRE_SOURCE##*/}
cd /home/${User}/src/${PCRE%%.tar.gz}
./configure –prefix=/home/${User}/pcre
make && make install
echo “${TIME} pcre install to /home/${User}/pcre” >> ${LOG};
fi
}

###解压jvm,会判断jvm目录是否存在,不会重复解压
JVM_UNTAR(){
if [ -d /home/${User}/src/nginx_upstream_jvm_route ]
then
echo “${TIME} jvm is exist ,here we go”>>${LOG};
else
cd /home/${User}/src && /bin/tar zxf ${JVM_SOURCE##*/};
echo “${TIME} untar jvm OK”>>${LOG};
fi
}

###编译安装nginx,编译之前修改nginx版本信息,有局限性。版本更新后需要手动修改(脚本只支持1.0.0)
NGINX_INSTALL(){
echo -e “${C_begin0}${Blue}give me a filename you what,the name shuld be nginx base_file name${C_end}”
####判断一下你想要的nginx的家目录是不是已经存在
while :
do
read -p “give me:” patch
if [ -d /home/${User}/${patch} ]
then
echo -e “${C_begin0}${Red}Hey man ,${patch} is exist,chenge it!!${C_end}”
unset patch;
else
break;
fi
done

###解压源码包
cd /home/${User}/src && /bin/tar zxf ${NGINX_SOURCE##*/}
NGINX=${NGINX_SOURCE##*/}
###修改http头部信息显示
/bin/sed -i ‘s/#define NGINX_VERSION “1.0.0″/#define NGINX_VERSION “2.0″/g’ /home/${User}/src/${NGINX%%.tar.gz}/src/core/nginx.h
/bin/sed -i ‘s/#define NGINX_VER “nginx\/” NGINX_VERSION/#define NGINX_VER “Apache:” NGINX_VERSION/g’ /home/${User}/src/${NGINX%%.tar.gz}/src/core/nginx.h

###编译喽
cd /home/${User}/src/${NGINX%%.tar.gz}
patch -p0<../nginx_upstream_jvm_route/jvm_route.patch
./configure –prefix=/home/${User}/${patch} \
–with-http_stub_status_module \
–add-module=/home/${User}/src/nginx_upstream_jvm_route \
–with-http_ssl_module \
–with-pcre=/home/${User}/src/${PCRE%%.tar.gz}
make && make install
echo “${TIME} nginx install to /home/${User}/${patch}” >> ${LOG};

}

###先编译安装tomcat,交互定义tomcat目录的名称
TOMCAT_INSTALL(){
cd /home/${User}/src && /bin/tar zxf ${TOMCAT_SOURCE##*/}
TOM_name=${TOMCAT_SOURCE##*/}
TOMCAT=${TOM_name%%.tar.gz}
echo -e “${C_begin0}${Blue}now,give me a filename you what,the name shuld be tomcat base_file name${C_end}”
while :
do
read -p “give me:” tom_patch
if [ -d /home/${User}/${tom_patch} ]
then
echo -e “${C_begin0}${Red}Hey man ,${tom_patch} is exist,chenge it!!${C_end}”
unset tom_patch;
else
break;
fi
done
/bin/mv /home/${User}/src/${TOMCAT} /home/${User}/${tom_patch}
echo “${TIME} /home/${User}/${tom_patch} install OK”>>${LOG}
}

BACKUP_WEBAPPS(){
cd /home/${User}/${tom_patch} && /bin/tar -zcf webapps_`date +%y%m%d`.tar.gz webapps/ && /bin/rm -rf /home/${User}/${tom_patch}/webapps/* && echo “${TIME} /home/${User}/${tom_patch}/webapps tar and rm OK” >> ${LOG};
}

###将jdk安装到你上面定义好的tomcat目录下,也就是每个tomcat都会有自己独立的jdk
JDK_INSTALL(){
cd /home/${User}/src && /bin/chmod +x ${JDK_source##*/} && ./${JDK_source##*/}
JDK_name=${JDK_source##*/}
/bin/mv /home/${User}/src/${JDK_name%%-linux-x64.bin} /home/${User}/${tom_patch}
JAVA_HOME=/home/${User}/${tom_patch}/${JDK_name%%-linux-x64.bin}
echo “${TIME} JAVA_HOME=${JAVA_HOME}”>>${LOG}
}

###安装,没什么好说的,注意看输出
INSTALL(){
echo -e “${C_begin0}${Green}========================+BEGIN INSTALL=====================+${C_end}”;
case ${WHICH} in
nginx|n)
PCRE_INSTALL;
JVM_UNTAR;
NGINX_INSTALL;
;;
tomcat|t)
TOMCAT_INSTALL;
BACKUP_WEBAPPS;
JDK_INSTALL;
;;
esac
echo -e “${C_begin0}${Red}========================+INSTALL FINISHED=====================++${C_end}”;
echo “”;
echo “”;
}

###备份将会被修改的nginx的文件
BACKUP_NGINX(){
/bin/mv /home/${User}/${patch}/conf/nginx.conf /home/${User}/${patch}/conf/nginx.conf_${TIME} && echo “${TIME} backup nginx conf OK” >> ${LOG};
}
###备份将会被修改的tomcat的文件
BACKUP_TOMCAT(){
/bin/mv /home/${User}/${tom_patch}/conf/server.xml /home/${User}/${tom_patch}/conf/server.xml_${TIME} && echo “${TIME} backup tomcat conf OK” >> ${LOG};
/bin/mv /home/${User}/${tom_patch}/bin/catalina.sh /home/${User}/${tom_patch}/bin/catalina.sh_${TIME} && echo “${TIME} backup tomcat bin/catalina.sh OK” >> ${LOG};
}
###修改nginx配置文件的相关信息,修改nginxctl脚本的nginx家目录,照着提示来。
CONFIG_NGINX(){
cd /home/${User}/${patch}/conf && /usr/bin/wget -q ${NGINX_CONF} && echo “${TIME} nginx.conf download OK” >> ${LOG};
sleep 3;
cd /home/${User}/${patch}/ && /usr/bin/wget -q ${NGINX_CTL} && /bin/chmod +x /home/${User}/${patch}/${NGINX_CTL##*/} && echo “${TIME} nginxctl.sh download OK” >> ${LOG};
sleep 3;
echo -e “${C_begin0}${Blue}now,we config your nginx,you shuld give me some help${C_end}”
read -p “how many worker_processes(default is 8):” Processes
read -p “what’s the name of your back-server group(default is back):” Group
read -p “what’s your first real-server name(default is worker1):” Worker1
read -p “what’s your second real-server name(default is worker2):” Worker2
read -p “what’s the name and port of your first back-server(default is realserver1:port):” Realserver1
read -p “what’s the name and port of your second back-server(default is realserver2:port):” Realserver2
while :
do
read -p “now,give me a number for nginx-http port(must in 8000~9000):” Port
Port_now=$(/bin/netstat -nltp|/bin/grep ${Port}|awk -F: ‘{print $2}’|awk ‘{print $1}’)
if [ "aa${Port}aa" = "aa${Port_now}aa" ]
then
echo -e “${C_begin0}${Red}Hey man ,${Port} is exist,chenge it!!${C_end}”
unset Port;
unset Port_now;
else
break;
fi
done
read -p “now,give me the nginx-server’s server-name(default is localhost):” Server_name
echo -e “${C_begin0}${Blue}thank you ,here we go.${C_end}”;
/bin/sed -i “s/user aa aa/user ${User} ${User}/g” /home/${User}/${patch}/conf/nginx.conf;
if [ -z ${Processes} ]
then
Processes=8;
else
/bin/sed -i “s/worker_processes 8/worker_processes ${Processes}/g” /home/${User}/${patch}/conf/nginx.conf;
fi
echo “${TIME} Nginx_server have ${Processes} Processes” >> ${LOG};

if [ -z ${Group} ]
then
Group=back;
else
/bin/sed -i “s/upstream back/upstream ${Group}/g” /home/${User}/${patch}/conf/nginx.conf;
fi
echo “${TIME} Nginx_back_server_group_name:${Group}” >> ${LOG};

if [ -z ${Worker1} ]
then
Worker1=worker1;
else
/bin/sed -i “s/srun_id=worker1/srun_id=${Worker1}/g” /home/${User}/${patch}/conf/nginx.conf;
fi
echo “${TIME} your first back_server is ${Worker1}” >> ${LOG};

if [ -z ${Worker2} ]
then
Worker2=worker2;
else
/bin/sed -i “s/srun_id=worker2/srun_id=${Worker2}/g” /home/${User}/${patch}/conf/nginx.conf;
fi
echo “${TIME} your second back_server is ${Worker2}” >> ${LOG};

/bin/sed -i “s/realserver1:port/${Realserver1}/g” /home/${User}/${patch}/conf/nginx.conf && echo “${TIME} Realserver1:${Realserver1}” >> ${LOG};
/bin/sed -i “s/realserver2:port/${Realserver2}/g” /home/${User}/${patch}/conf/nginx.conf && echo “${TIME} Realserver2:${Realserver2}” >> ${LOG};
/bin/sed -i “s/listen 8127/listen ${Port}/g” /home/${User}/${patch}/conf/nginx.conf && echo “${TIME} Nginx_HTTP_PORT:${Port}” >> ${LOG};
/bin/sed -i “s/server_name localhost/server_name ${Server_name}/g” /home/${User}/${patch}/conf/nginx.conf && echo “${TIME} Nginx_Server_Name:${Server_name}” >> ${LOG};
/bin/sed -i “s/proxy_pass http:\/\/back/proxy_pass http:\/\/${Group}/g” /home/${User}/${patch}/conf/nginx.conf && echo “${TIME} proxy_pass to :${Group}” >> ${LOG};
/bin/sed -i “s/NGINX_HOME=\/home\/user\/nginx/NGINX_HOME=\/home\/${User}\/${patch}/g” /home/${User}/${patch}/nginxctl.sh && echo “${TIME} Nginxctl.sh modify OK” >> ${LOG};
echo -e “${C_begin0}${Blue}good job,man${C_end}”;
}
###修改tomcat的server.xml,端口、web-root目录、启用线程池;同时修改tomcat的启动文件,添加jdk信息
CONFIG_TOMCAT(){
cd /home/${User}/${tom_patch}/conf && /usr/bin/wget -q ${TOMCAT_CONF} && echo “${TIME} server.xml download OK” >> ${LOG};
sleep 3;
cd /home/${User}/${tom_patch}/bin && /usr/bin/wget -q ${TOMCAT_CTL} && echo “${TIME} catalina.sh download OK” >> ${LOG};
sleep 3;
echo -e “${C_begin0}${Blue}now,you must give me some help${C_end}”;
###好吧,开始定义tomcat的3个端口
while :
do
read -p “now,give me a number for tomcat-shutdown port(must in 8000~9000):” Port_shutdown
Port_now_shutdown=$(/bin/netstat -nltp|/bin/grep ${Port_shutdown}|awk -F: ‘{print $2}’|awk ‘{print $1}’)
if [ "aa${Port_shutdown}aa" = "aa${Port_now_shutdown}aa" ]
then
echo -e “${C_begin0}${Red}Hey man ,${Port_shutdown} is exist,chenge it!!${C_end}”
unset Port_shutdown;
unset Port_now_shutdown;
else
break;
fi
done

while :
do
read -p “now,give me a number for tomcat-http port(must in 8000~9000):” Port_http
Port_now_http=$(/bin/netstat -nltp|/bin/grep ${Port_http}|awk -F: ‘{print $2}’|awk ‘{print $1}’)
if [ "aa${Port_http}aa" = "aa${Port_now_http}aa" ]
then
echo -e “${C_begin0}${Red}Hey man ,${Port_http} is exist,chenge it!!${C_end}”
unset Port_http;
unset Port_now_http;
else
break;
fi
done

while :
do
read -p “now,give me a number for tomcat-AJP port(must in 8000~9000):” Port_ajp
Port_now_ajp=$(/bin/netstat -nltp|/bin/grep ${Port_ajp}|awk -F: ‘{print $2}’|awk ‘{print $1}’)
if [ "aa${Port_ajp}aa" = "aa${Port_now_ajp}aa" ]
then
echo -e “${C_begin0}${Red}Hey man ,${Port_ajp} is exist,chenge it!!${C_end}”
unset Port_ajp;
unset Port_now_ajp;
else
break;
fi
done

read -p “now,give me this tomcat in cluster name(default is worker1):” cluster_name
###这块是定义tomcat的web-root,目录的,没有判断,别搞错了
read -p “now,give me the tomcat’s web-root(give me full patch like /home/aaa/bbb):” tomcat_web_root
echo -e “${C_begin0}${Blue},thank you very mush,OK,here we go.${C_end}”
sleep 2;

###现在,开始真正修改文件了,首先是server.xml
/bin/sed -i “s/<Server port=\”8009\” shutdown=\”SHUTDOWN\”>/<Server port=\”${Port_shutdown}\” shutdown=\”SHUTDOWN\”>/g” /home/${User}/${tom_patch}/conf/server.xml && echo “${TIME} Tomcat_shutdown port is :${Port_shutdown}”>> ${LOG};
/bin/sed -i “s/<Connector port=\”8080\” protocol=\”HTTP\/1.1\”/<Connector port=\”${Port_http}\” protocol=\”HTTP\/1.1\”/g” /home/${User}/${tom_patch}/conf/server.xml && echo “${TIME} Tomcat_http port is :${Port_http}” >> ${LOG};
/bin/sed -i “s/<Connector port=\”8030\” protocol=\”AJP\/1.3\” redirectPort=\”8443\” \/>/<Connector port=\”${Port_ajp}\” protocol=\”AJP\/1.3\” redirectPort=\”8443\” \/>/g” /home/${User}/${tom_patch}/conf/server.xml && echo “${TIME} Tomcat_ajp port is :${Port_ajp}”>> ${LOG};
if [ -z ${cluster_name} ]
then
cluster_name=worker1;
else
/bin/sed -i “s/jvmRoute=\”worker1\”/jvmRoute=\”${cluster_name}\”/g” /home/${User}/${tom_patch}/conf/server.xml;
fi
echo “${TIME} in server.xml,tomcat_cluster_name is :${cluster_name}” >> ${LOG};

###改变一下sed调用的tomcat_web_root的’形状’
twr=$(echo “${tomcat_web_root}” | sed ‘s#/#\\/#g’)
/bin/sed -i “s/docBase=\”\/home\/aaa\/bbb\”/docBase=\”${twr}\”/g” /home/${User}/${tom_patch}/conf/server.xml;

###轮到catalina.sh了,再接再厉
/bin/sed -i “s/CATALINA_HOME=\/home\/aaa\/bbb/CATALINA_HOME=\/home\/${User}\/${tom_patch}/g” /home/${User}/${tom_patch}/bin/catalina.sh && echo “${TIME} CATALINA_HOME=/home/${User}/${tom_patch}”>> ${LOG};
/bin/sed -i “s/JAVA_HOME=\/home\/aaa\/bbb/JAVA_HOME=\/home\/${User}\/${tom_patch}\/${JDK_name%%-linux-x64.bin}/g” /home/${User}/${tom_patch}/bin/catalina.sh && echo “${TIME} JAVA_HOME=/home/${User}/${tom_patch}/${JDK_name%%-linux-x64.bin}/” >> ${LOG};
/bin/sed -i “s/JRE_HOME=\/home\/aaa\/bbb\/jre/JAVA_HOME=\/home\/${User}\/${tom_patch}\/${JDK_name%%-linux-x64.bin}\/jre/g” /home/${User}/${tom_patch}/bin/catalina.sh && echo “${TIME} JRE_HOME=/home/${User}/${tom_patch}/${JDK_name%%-linux-x64.bin}/jre” >> ${LOG};
echo -e “${C_begin0}${Blue}good job,man${C_end}”;
}

###配置,会在配置之前,对将要修改的文件备份,格式为原名_%Y%m%d-!-%H%M%S,在修改配置之前,会检查端口占用
CONFIG(){
echo -e “${C_begin0}${Green}========================+BEGIN CONFIG=====================++${C_end}”;
case ${WHICH} in
nginx|n)
BACKUP_NGINX;
CONFIG_NGINX;
;;
tomcat|t)
BACKUP_TOMCAT;
CONFIG_TOMCAT;
;;
esac
echo -e “${C_begin0}${Red}========================+CONFIG FINISHED=====================++${C_end}”;
echo “”;
echo “”;
}

MAIN(){
GET_SOFT;
sleep 2;
MD5_CHECK;
sleep 2;
INSTALL;
sleep 2;
CONFIG;
}
MAIN;
echo “=============================================================================”>> ${LOG}
echo “”>>${LOG}