#!/bin/bash
#by 2015-7-25--lijq
#directiof for use:源码包和脚本在同一目录且在root目录下的文件里
DIR=/mnt/soft/
SCRIPT_DIR=/root/autolamp
        if [ $UID -ne 0 ]; then
        echo -e "\033[32monly for root to install this LAMP\033[0m"
                exit 3
        fi
        echo -e "\033[32mcreate soft dowload dir....\033[0m"
        if [ ! -d $DIR ];then
        mkdir -p $DIR;
        fi
        echo -e  "\033[32minstal some dependent packages,please wait for .... \033\0m"
        yum install gcc gcc-c++ make cmake ncurses-devel bison perl lrzsz epel-release pcre-devel openssl-devel -y;
        yum install wget unzip libxml2-devel libmcrypt-devel bzip2-devel libxml2-devel openssl-devel bzip2 bzip2-devel -y
;
APR_FILE=apr-1.5.2.tar.gz
APR=`echo $APR_FILE | sed 's/\.tar\.gz//g'`
APR_UTILS=apr-util-1.5.2.tar.gz
APR_UTIL=`echo $APR_UTILS | sed 's/\.tar\.gz//g'`
APACHE_FILE=httpd-2.4.16.tar.gz
APACHE=`echo $APACHE_FILE | sed 's/\.tar\.gz//g'`
APR_DIR=/usr/local/apr/
APRUTIL_DIR=/usr/local/apr-util/
APACHE_DIR=/usr/local/apache/
function httpd_install()
{       if [ -f $APR_FILE -o -f $APR_UTILS -o -f $APACHE_FILE ];then
        cp $APR_FILE $APR_UTILS $APACHE_FILE $DIR -a
 else
        cd $DIR;
        wget -c http://archive.apache.org/dist/apr/$APR_FILE;
        wget -c http://archive.apache.org/dist/apr/$APR_UTILS;
        wget -c http://www.apache.org/dist/httpd/$APACHE_FILE;
        fi
        cd $DIR
        tar xzvf $APR_FILE;
        cd $APR;./configure --prefix=$APR_DIR && make && make install;
        if  [ $? -eq 0  ]; then
        echo -e "\033[32mThis apr is install successfully...\033[0m"
        else
        echo -e "\033[32mThis apr is install failed,please check....\033[0m"
        exit
        fi
        cd $DIR;
        tar xzvf $APR_UTILS;
        cd $APR_UTIL;
        ./configure --prefix=$APRUTIL_DIR --with-apr=$APR_DIR &&make && make install;
        if [ $? -eq 0 ];then
        echo -e "\033[32mThis apr-util is install successfully....\033[0m"
        else
        echo -e "\033[32mThis arp in install failed, please check....\033[0m"
        exit
        fi
        cd $DIR;
        tar xzvf $APACHE_FILE; cd $APACHE;
./configure --prefix=$APACHE_DIR  --enable-so --enable-ssl --enable-cgi--enable-rewrite --with-zlib --with-pcre -
-with-apr=$APR_DIR --with-apr-util=$APRUTIL_DIR --enable-modules=most --enable-mpms-shared=all --with-mpm=event && make &
& make install
        if [ $? -eq 0 ]; then
        echo -e "\033[32mThe apache is install successful..\033[0m"
        $APACHE_DIR/bin/apachectl restart;
        else
        echo -e "\033[32mTHE apache is install failed,please check....\033[0m"
        exit
        fi
}
        sleep 3
MYSQL_FILE='mysql-5.5.44.tar.gz'
MYSQL=`echo $MYSQL_FILE | sed 's/\.tar\.gz//g'`
DATADIR=/data/mysql3306
BASEDIR=/data/app/mysql3306
MYSQL_STATUS=`ps -ef |grep mysqld_safe |awk 'NR==1 {print $9}' |sed 's#\/data\/app\/mysql3306\/bin\/##g'`
function mysql_install()
{       cd $SCRIPT_DIR ;
        if [ -f "$MYSQL_FILE" ];then
        cp  ${MYSQL_FILE} $DIR -a ;
        sleep 2
        else
        cd $DIR;
        wget -c  http://download.softagency.net/MySQL/Downloads/MySQL-5.5/$MYSQL_FILE;
        fi
        cd $DIR;
        tar xzvf $MYSQL_FILE;cd $MYSQL;mkdir -p {$DATADIR,$DATADIR};
  cmake \-DCMAKE_INSTALL_PREFIX=$BASEDIR \-DMYSQL_DATADIR=$DATADIR \-DWITH_MYISAM_STORAGE_ENGINE=1 \-DWITH_INNOBASE
_STORAGE_ENGINE=1 \-DWITH_MEMORY_STORAGE_ENGINE=1 \-DWITH_READLINE=1 \-DMYSQL_TCP_PORT=3306 \-DENABLED_LOCAL_INFILE=1 \-D
WITH_PARTITION_STORAGE_ENGINE=1 \-DEXTRA_CHARSETS=all \-DDEFAULT_CHARSET=utf8 \-DDEFAULT_COLLATION=utf8_general_ci \-DWIT
H_DEBUG=0 \-DMYSQL_USER=mysql \-DMYSQL_UNIX_ADDR=$DATADIR/mysql3306.sock && make && make install
         if [ $? -eq 0 ]; then
        echo -e "\033[32mthe mysql is install successful..\033[0m"
        else
        echo -e "\033[32mThis  mysql is install failed.....\033[0m"
        exit 2
        fi
        useradd mysql;
        chown -R  mysql.mysql $DATADIR;
        chown -R mysql:mysql $BASEDIR;
        cp -a support-files/my-large.cnf $DATADIR/my.cnf;
        chown -R  mysql.mysql $DATADIR/my.cnf;
        cp -a $BASEDIR/support-files/mysql.server /etc/init.d/mysqld;
#       sed  -i '/mysql3306/i datadir = /data/mysql3306\nbasedir = /data/app/mysql3306' /data/mysql3306/my.cnf;
        sed -i "26a\datadir = $DATADIR\nbasedir = $BASEDIR" $DATADIR/my.cnf;
        cd $BASEDIR;./scripts/mysql_install_db --basedir=$BASEDIR --datadir=$DATADIR --user=mysql;
echo "export PATH="\$PATH":${BASEDIR}/bin/" >>/root/.bash_profile && source /root/.bash_profile && /etc/init.d/mysqld res
tart;pkill mysql;/etc/init.d/mysqld start;/etc/init.d/mysqld restart;
        sleep 5
        if [ ${MYSQL_STATUS} = 'mysqld_safe' ];then
        echo -e "\033[32mThe Mysql was installed successfully....\033[0m"
        else
        echo -e "\033[31mThe Mysql is installed failed,Please check...\033[0m"
        exit
   fi
        sleep 3
}
PHP_FILES=php-5.4.42.tar.bz2
PHP=`echo $PHP_FILES | sed 's/\.tar\.bz2//g'`
PHP_DIR=/usr/local/php
function php_install()
{       cd $SCRIPT_DIR;
        if [ -f $PHP_FILES ];then
        cp $PHP_FILES $DIR -a ;
        else
        cd $DIR;
        wget http://mirrors.sohu.com/php/$PHP_FILES;
        fi
        cd $DIR;
        if [ -d $BASEDIR ] ; then
        tar xjvf $PHP_FILES ; cd $PHP;./configure --prefix=$PHP_DIR --with-mysql=$BASEDIR --with-openssl --with-mysqli=$B
ASEDIR/bin/mysql_config --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-di
r=/usr --enable-xml --enable-sockets --enable-fpm --with-mcrypt --with-bz2 --with-config-file-path=/etc/php/php.ini --wit
h-config-file-scan-dir=/etc/php/php.d/ --with-apxs2=${APACHE_DIR}bin/apxs && make && make install
        else
        echo -e "\033[32mplease install this mysql....\033[0m"
        fi
        if [ $? -eq 0 ]; then
        echo -e "\033[32mthe php is install successful..\033[0m"
  else
        echo "the php is install failed,,,,please quit"
        exit
        fi
}
IP=`ifconfig eth0 |grep Bcast |awk '{print $2}'|sed 's/addr://g'`
function php_apache()
{
        echo -e "\033[32mIt will configure the Apache and PHP\033[0m"
        cp ${APACHE_DIR}conf/httpd.conf ${APACHE_DIR}conf/httpd.conf.bak
        echo "Addtype application/x-httpd-php  .php" >> ${APACHE_DIR}conf/httpd.conf
        sed -i 's/DirectoryIndex index.html/DirectoryIndex index.php index.html/g' ${APACHE_DIR}conf/httpd.conf
        if [ $? -eq 0 ];then
        echo -e "\033[32mIt's successfully, Will test the PHP... \033[0m"
        rm -rf ${APACHE_DIR}htdocs/*;
         cat >${APACHE_DIR}htdocs/index.php <<EOF
<?php
phpinfo();
?>
EOF
        if [ $? -eq 0 ];then
#启动apache服务
        ${APACHE_DIR}bin/apachectl restart ;
        ${APACHE_DIR}bin/apachectl restart ;
        echo -e "\033[32mYou can access http://${IP}/index.php\033[0m"
 fi
        else
        echo -e "\033[31mThe PHP is installed failed,Please check...\033[0m"
        exit
        fi
}
Discuz_file=Discuz_X3.1_SC_UTF8.zip
Discuz=Discuz_X3.1_SC_UTF8
PASSWORD=123456
function discuz_install()
{       cd $SCRIPT_DIR;
        if [ -f $Discuz_file ];then
        cp $Discuz_file $DIR -a ;
        sleep 2
        else
        cd $DIR;
        wget -c http://download.comsenz.com/DiscuzX/3.1/$Discuz_file;
        fi
        cd $DIR;
        rm -rf ${APACHE_DIR}htdocs/* && unzip $Discuz_file -d ${APACHE_DIR}htdocs/ ;
        cd ${APACHE_DIR}htdocs/ && mv upload/* . && chmod -R o+w data/ config/ uc_server/ uc_client/;
        if [ $? -eq 0 ];then
        echo -e '\033[32mThis discuz is files configure ok....\033[0m'
#设置数据库密码和创建数据库
        $BASEDIR/bin/mysqladmin -uroot password $PASSWORD &&
        mysql -uroot -h 127.0.0.1 -p$PASSWORD -e "create database discuz;"&&
#授权discuz用户
        mysql -uroot -h 127.0.0.1 -p$PASSWORD -e "grant all on *.* to discuz@'${IP}' identified by '$PASSWORD';flush priv
ileges;"
 if [ $? -eq 0 ];then
        echo -e "\033[32mThis discuz  database configure is  success.....\033[0m"
        else
        echo -e "\033[32mThis discuz database configure is failed..please check..\033[0m"
        fi
#重启apache服务和mysql
        ${APACHE_DIR}bin/apachectl restart&& /etc/init.d/mysqld restart;
        if [ $? -eq 0 ];then
        echo -e "\033[32mThe apache and Mysql Start Success!\033[0m"
        else
        echo -e "\033[31mThis discuz configure is failed..please check.\033[0m"
        echo -e  "\033[32mThis discuz is install success.......\033[32m"
        fi
        else
        echo -e  "\033[32mThe mysql and apache is failed....please check.\033[0m"
               exit 2
        fi
}
PS3="please select your install menu:"
        select i in "httpd" "mysql" "php" "php+apache" "discuz" "quit" "all_auto"
        do
        case $i in
        httpd )
        httpd_install
        ;;
        mysql )
        mysql_install
        ;;
   php )
        php_install
        ;;
        php+apache )
        php_apache
        ;;
        discuz )
        discuz_install
        ;;
        all_auto )
        httpd_install;
        mysql_install;
        php_install;
        php_apache;
        discuz_install
        ;;
        quit )
        exit
        ;;
        * )
        echo "Usage: $0 {httpd|mysql|php|php+apache|discuz|all_auto|quit}"
esac
        done