#!/bin/bash




##############################################################

## /usr/local/src software source

## /opt software

## /var/www web root dir

##############################################################

mkdir -p /var/www

mkdir -p /opt


##wget http://mirrors.hust.edu.cn/apache/tomcat/tomcat-7/v7.0.42/bin/apache-tomcat-7.0.42.tar.gz

##wget http://mirror.esocc.com/apache//httpd/httpd-2.4.6.tar.gz

##http://apr.apache.org/download.cgi

##wget http://apache.dataguru.cn/apr/apr-1.4.8.tar.gz

##wget http://apache.dataguru.cn/apr/apr-util-1.5.2.tar.gz

##wget http://sourceforge.net/projects/pcre/files/pcre/8.33/pcre-8.33.tar.gz

##wget http://www.bzip.org/1.0.6/bzip2-1.0.6.tar.gz

##wget http://archive.apache.org/dist/tomcat/tomcat-connectors/jk/source/jk-1.2.31/tomcat-connectors-1.2.31-src.tar.gz

##wget http://zlib.net/zlib-1.2.8.tar.gz

echo "##############thank you for using my shell################# "

echo "DO you want to install httpd-2.4.6 now?"

echo "These softwares will be installed :"

echo "apache-tomcat-7.0.42.tar.gz"

echo "apr-1.4.8.tar.gz"

echo "apr-util-1.5.2.tar.gz"

echo "bzip2-1.0.6.tar.gz"

echo "httpd-2.4.6.tar.gz"

echo "pcre-8.33.tar.gz"

echo "tomcat-connectors-1.2.37-src.tar.gz"

echo "zlib-1.2.8.tar.gz"

echo "are you ready??"

echo "press any key to continue."

echo "press n to stop."

read -p "(input y or n):" choose

if [ "$choose" = "n" ]; then

echo "good bye"

exit 0

else

echo "start............"

fi

echo "==========================="

echo "install now............."

echo "==========================="




echo "please choose no in the following table install to continue install!!!"

read abc


dpkg-reconfigure dash




function NeedGetsystemsoftware()

{

echo "============================apt get files=================================="

apt-get install -y gcc

apt-get install -y make

apt-get install -y perl

apt-get install -y build-essential

apt-get install -y libxtst-dev

}


function CheckAndDownloadFiles()

{

echo "============================check files=================================="


if [ -s apr-1.4.8.tar.gz ]; then

echo "apr-1.4.8.tar.gz [found]"

else

echo "Error: apr-1.4.8.tar.gz not found!!!download now......"

wget -c http://apache.dataguru.cn/apr/apr-1.4.8.tar.gz

fi

if [ -s apache-tomcat-7.0.42.tar.gz ]; then

echo "apache-tomcat-7.0.42.tar.gz [found]"

else

echo "Error: apache-tomcat-7.0.42.tar.gz not found!!!download now......"

wget -c http://mirrors.hust.edu.cn/apache/tomcat/tomcat-7/v7.0.42/bin/apache-tomcat-7.0.42.tar.gz

fi

if [ -s httpd-2.4.6.tar.gz ]; then

echo "httpd-2.4.6.tar.gz [found]"

else

echo "Error: httpd-2.4.6.tar.gz not found!!!download now......"

wget -c http://mirror.esocc.com/apache//httpd/httpd-2.4.6.tar.gz

fi

if [ -s apr-1.4.8.tar.gz ]; then

echo "apr-util-1.5.2.tar.gz [found]"

else

echo "Error: apr-util-1.5.2.tar.gz not found!!!download now......"

wget -c http://apache.dataguru.cn/apr/apr-util-1.5.2.tar.gz

fi

if [ -s pcre-8.33.tar.gz ]; then

echo "pcre-8.33.tar.gz [found]"

else

echo "Error: pcre-8.33.tar.gz not found!!!download now......"

wget -c http://sourceforge.net/projects/pcre/files/pcre/8.33/pcre-8.33.tar.gz

fi

if [ -s bzip2-1.0.6.tar.gz ]; then

echo "bzip2-1.0.6.tar.gz [found]"

else

echo "Error: bzip2-1.0.6.tar.gz not found!!!download now......"

wget -c http://www.bzip.org/1.0.6/bzip2-1.0.6.tar.gz

fi

if [ -s zlib-1.2.8.tar.gz ]; then

echo "zlib-1.2.8.tar.gz [found]"

else

echo "Error: zlib-1.2.8.tar.gz not found!!!download now......"

wget -c http://zlib.net/zlib-1.2.8.tar.gz

fi

if [ -s tomcat-connectors-1.2.37-src.tar.gz ]; then

echo "tomcat-connectors-1.2.37-src.tar.gz [found]"

else

echo "Error: tomcat-connectors-1.2.37-src.tar.gz not found!!!download now......"

wget -c http://mirrors.cnnic.cn/apache//tomcat/tomcat-connectors/jk/tomcat-connectors-1.2.37-src.tar.gz

fi

}


function Installsoftware

{

echo "============================install software files=================================="

cd /usr/local/src

##########apache need apr and apr-util

tar zxvf apr-1.4.8.tar.gz

cd apr-1.4.8/

./configure --prefix=/opt/apr-httpd

make&&make install


cd /usr/local/src

tar zxvf apr-util-1.5.2.tar.gz

cd apr-util-1.5.2

./configure --prefix=/opt/apr-util --with-apr=/opt/apr-httpd

make&&make install


#########pcre need libbz2

cd /usr/local/src

tar zxvf bzip2-1.0.6.tar.gz

cd bzip2-1.0.6

make&&make install


#########apache need pcre

cd /usr/local/src

tar zxvf pcre-8.33.tar.gz

cd pcre-8.33

./configure --prefix=/opt/pcre-httpd --enable-pcregrep-libbz2

make&&make install


#################apache need ###########################

cd /usr/local/src

tar zxvf zlib-1.2.8.tar.gz

cd zlib-1.2.8

./configure --prefix=/opt/zlib

make&&make install



cd /usr/local/src

tar zxvf httpd-2.4.6.tar.gz

cd httpd-2.4.6

./configure --prefix=/opt/apache --with-apr=/opt/apr-httpd/ --with-apr-util=/opt/apr-util/ --with-z=/opt/zlib --with-pcre=/opt/pcre-httpd --with-mpm=worker --enable-module=so --enable-mods-shared=all --enable-deflate=shared


make&&make install


cat >> /etc/rc.local <<EOF

/opt/apache/bin/apachectl start

EOF


}


NeedGetsystemsoftware 2>&1 | tee -a /usr/local/src/aptget.log

CheckAndDownloadFiles 2>&1 | tee -a /usr/local/src/find.log

Installsoftware 2>&1 | tee -a /usr/local/src/installsoft.log



function InstallJDKTOMCAT()

{

echo "Install tomcat and Jdk now>>>>>>>>>>>>>"

############### install JDK ###############

if [ -s jdk-6u35-linux-x64.bin ]; then

V64=1

echo "v64 found"

else

V64=0

echo "v64 no found"

fi

if [ -s jdk-6u45-linux-i586.bin ]; then

V32=1

echo "v32 found"

else

V32=0

echo "v32 no found"

fi


if [ "$V64" = 0 ] && [ "$V32" = 0 ]; then

echo "no jdk-6u found!!"

exit 0

fi





echo "==========================="

echo "instal now............."

echo "==========================="


if [ "$V64" != 0 ] && [ "$V32" != 0 ]; then

echo "which JDK version(default 32) you want to install (32=1 64=2) ?"

read -p "(please input 1 or 2):" JDKVER

if [ "$JDKVER" = "" ]; then

JDKVER=1

fi

echo "The version you choose is $JDKVER"

fi


cd /usr/local/src



if [ "$JDKVER" = 1 ]; then

chmod +x jdk-6u45-linux-i586.bin

./jdk-6u45-linux-i586.bin

mv jdk1* /opt/jdk6

fi



if [ "$JDKVER" = 2 ]; then

chmod +x jdk-6u35-linux-x64.bin

./jdk-6u35-linux-x64.bin

mv jdk1* /opt/jdk6

fi


if [ "$V64" = 0 ] && [ "$V32" = 1 ]; then

chmod +x jdk-6u45-linux-i586.bin

./jdk-6u45-linux-i586.bin

mv jdk1* /opt/jdk6

fi


if [ "$V64" = 1 ] && [ "$V32" = 0 ]; then

chmod +x jdk-6u35-linux-x64.bin

./jdk-6u35-linux-x64.bin

mv jdk1* /opt/jdk6

fi









cat >> /etc/profile <<EOF

export JAVA_HOME=/opt/jdk6

export CLASSPATH=\$CLASSPATH:$JAVA_HOME/lib:\$JAVA_HOME/jre/lib

export PATH=\$JAVA_HOME/bin:\$JAVA_HOME/jre/bin:\$PATH:\$HOME/bin

EOF


source /etc/profile


java -version


#echo "is that right please input enter to continue?"

#read test


############### install TOMCAT ###############

cd /usr/local/src

tar zxvf apache-tomcat-7.0.42.tar.gz

mv apache-tomcat-7.0.42 /opt/tomcat


cat >> /opt/tomcat/bin/startup.sh <<EOF


JAVA_HOME=/opt/jdk6

KRE_HOME=/opt/jkd6/jre


EOF




}


InstallJDKTOMCAT 2>&1 | tee -a /usr/local/src/installjdktomcat.log


function TOMCATINAPACHE()

{


BJIP=$(/sbin/ifconfig eth0|sed -n '/inet addr/s/^[^:]*:\([0-9.]\{7,15\}\) .*/\1/p')

echo "do you have a domain name"

read -p "(please input your domainanme):" DOMANNAME

if [ "$DOMANNAME" = "" ]; then

DOMANNAME=test.com

fi



########################## set apache ##################

echo "Include conf/extra/httpd-vhosts.conf" >> /opt/apache/conf/httpd.conf


mv /opt/apache/conf/extra/httpd-vhosts.conf /opt/apache/conf/extra/httpd-vhosts.conf.bak


cat >> /opt/apache/conf/extra/httpd-vhosts.conf <<EOF

<VirtualHost *:80>

ServerAdmin webmaster@dummy-host.example.com

DocumentRoot "/var/www"

ServerName $DOMANNAME

ServerAlias $BJIP

ErrorLog "logs/$DOMANNAME-error_log"

CustomLog "logs/$DOMANNAME-access_log" common

</VirtualHost>


#<VirtualHost *:80>

# ServerAdmin webmaster@dummy-host2.example.com

# DocumentRoot "/opt/apache/docs/dummy-host2.example.com"

# ServerName dummy-host2.example.com

# ErrorLog "logs/dummy-host2.example.com-error_log"

# CustomLog "logs/dummy-host2.example.com-access_log" common

#</VirtualHost>

EOF

sed -i 's/\/opt\/apache\/htdocs/\/var\/www/' /opt/apache/conf/httpd.conf

sed -i 's/ServerName www.example.com:80/& \nServerName '"$BJIP"'/' /opt/apache/conf/httpd.conf


cd /usr/local/src

tar zxvf tomcat-connectors-1.2.37-src.tar.gz

cd tomcat-connectors-1.2.37-src/native

./configure --with-apxs=/opt/apache/bin/apxs --with-java-home=/opt/jdk6 --with-tomcat=/opt/tomcat

sed -i 's/remote_ip/client_ip/g' apache-2.0/mod_jk.c

make

make install


sed -i '/DirectoryIndex index.html/s//& index.jsp/' /opt/apache/conf/httpd.conf


###########################jk set

cat >> /opt/apache/conf/httpd.conf <<EOF

AddType application/x-httpd-jsp .jsp

Include conf/extra/mod_jk.conf

EOF


cat >> /opt/apache/conf/extra/mod_jk.conf <<EOF


LoadModule jk_module modules/mod_jk.so


<IfModule jk_module>


# We need a workers file exactly once

# and in the global server

JkWorkersFile conf/extra/workers.properties


# Our JK error log

# You can (and should) use rotatelogs here

JkLogFile logs/mod_jk.log


# Our JK log level (trace,debug,info,warn,error)

JkLogLevel info


# Our JK shared memory file

JkShmFile logs/mod_jk.shm

JkMount /*.jsp worker1

JkmountCopy All

# Define a new log format you can use in any CustomLog in order

# to add mod_jk specific information to your access log.

# LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" \"%{Cookie}i\" \"%{Set-Cookie}o\" %{pid}P %{tid}P %{JK_LB_FIRST_NAME}n %{JK_LB_LAST_NAME}n ACC %{JK_LB_LAST_ACCESSED}n ERR %{JK_LB_LAST_ERRORS}n BSY %{JK_LB_LAST_BUSY}n %{JK_LB_LAST_STATE}n %D" extended_jk



JkWatchdogInterval 60


<Location /jk-status>

# Inside Location we can omit the URL in JkMount

JkMount jk-status

Order deny,allow

Deny from all

Allow from 127.0.0.1

</Location>

<Location /jk-manager>

# Inside Location we can omit the URL in JkMount

JkMount jk-manager

Order deny,allow

Deny from all

Allow from 127.0.0.1

</Location>




</IfModule>


EOF





cat >> /opt/apache/conf/extra/workers.properties <<EOF

worker.list=worker1

worker.worker1.port=8009

worker.worker1.host=localhost

worker.worker1.type=ajp13

worker.woker1.lbfactor=1

EOF


sed -i "s/webapps/\/var\/www/g" /opt/tomcat/conf/server.xml

##<Context path="" docBase="/var/www/" debug="0"/>


cp -Rp /opt/tomcat/webapps/* /var/www

mv /opt/tomcat/conf/tomcat-users.xml /opt/tomcat/conf/tomcat-users.xml.bak

mv /opt/tomcat/conf/server.xml /opt/tomcat/conf/server.xml.bak

cp /usr/local/src/tomcat-users.xml /opt/tomcat/conf/

cp /usr/local/src/server.xml /opt/tomcat/conf/


}


TOMCATINAPACHE 2>&1 | tee -a /usr/local/src/TOMCATapache.log


function Checkinstall()

{

echo " "

echo " "

echo " "

echo "Check your install now,please input enter to see the logs which have the key words "ERROR" "

echo " "

echo " "

echo " "

read nothing

cat /usr/local/src/aptget.log|grep error >>Error.log

cat /usr/local/src/find.log|grep error >>Error.log

cat /usr/local/src/installsoft.log|grep error >>Error.log

cat /usr/local/src/TOMCATapache.log|grep error >>Error.log

Logs=$(cat /usr/local/src/Error.log)

echo "$Logs"

echo "No error? input y or n"

read ToSee

if [ "$ToSee" = "n" ]; then

echo "##########################################################################"

echo "########## please check the Error logs #############"

echo "########## /usr/local/src/Error.log #############"

echo "##########################################################################"

exit 0

else

echo "##########################################################################"

echo "########## Congratulations!!!!!!!! #############"

echo "########## please startup your tomcat and apache server! #############"

echo "########## as it is the first time use jdk please use: #############"

echo "########## source /etc/profile #############"

echo "########## /opt/tomcat/bin/startup.sh #############"

echo "########## /opt/apache/bin/apachectl start #############"

echo "##########################################################################"

echo " "

echo "visit your website at http://\$ip to test your web server"

echo "note: May it can not access to tomcat manager website"

echo " "

echo "visit website at http://\$ip:8080 to manage your tomcat server"

echo "use the default username :admin passwd:admin"

echo " "

echo " "

echo " "

echo "note:use apt-get install mysql-server to install your mysqlserver"

echo " "

echo " "

echo " "

fi




}


Checkinstall 2>&1 | tee -a /usr/local/src/checkinstall.log