#!/bin/bash

#write by zhang_pc

#at 2015.07.25

#apache2.4 php.5.4 mysql5.5

#脚本说明,如果当前机器可以上网,就从互联网下载源码包安装,如果不能上网,就用本地的源码包,需要把源码包上传到脚本所在的目录



APR_FILES=apr-1.5.2.tar.gz

APR_DIR=apr-1.5.2

ARP_PRE=/usr/local/apr

APR_URL=http://mirror.bit.edu.cn/apache/apr/apr-1.5.2.tar.gz


APR_U_FILES=apr-util-1.5.4.tar.gz

APR_U_DIR=apr-util-1.5.4

APR_U_PRE=/usr/local/apr-util

APR_U_URL=http://mirror.bit.edu.cn/apache/apr/apr-util-1.5.4.tar.gz


A_FILES=httpd-2.4.16.tar.gz

A_DIR=httpd-2.4.16

A_PRE=/usr/local/apache

A_URL=http://mirrors.sohu.com/apache/httpd-2.4.16.tar.gz


M_FILES=mysql-5.5.44.tar.gz

M_DIR=mysql-5.5.44

M_PRE=/usr/local/mysql

M_URL=http://download.softagency.net/MySQL/Downloads/MySQL-5.5/mysql-5.5.44.tar.gz


P_FILES=php-5.4.22.tar.gz

P_DIR=php-5.4.22

P_PRE=/usr/local/php

P_URL=http://mirrors.sohu.com/php/php-5.4.22.tar.gz



function apr_install() {

tar -zxvf $APR_FILES;cd $APR_DIR;./configure --prefix=$APR_PRE && make && make install

if [ $? -eq 0 ];then

 echo "the apr install is successful"

else

 echo "the apr install is failed"

fi

}



function apr_util_install() {

        tar -zxvf $APR_U_FILES;cd $APR_U_DIR;./configure --prefix=$APR_U_PRE  --with-apr=$APR_PRE && make && make install

if [ $? -eq 0 ];then

 echo "the apr_util install is successful"

else

 echo "the apr_util install is failed"

fi

}



function apache_install() {

yum install  pcre-devel  openssl-devel-y

tar -zxvf $A_FILES;cd $A_DIR

./configure --prefix=$A_PRE   --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre                        --with-arp=$APR_PRE --with-apr-util=$APR_U_PRE --enable-modules=most --enable-mpms-shared=all --with-mpm=prefork

make -j2 && make install -j2

  if [ $? -eq 0 ];then 

echo "the apache install is successful"

  else

echo "the apache install is failed"

        exit

  fi

}


function mysql_install() {

yum install  gcc-c++ cmake ncurses-devel bison perl -y

useradd  -s  /sbin/nologin  mysql 

mkdir  -p  /data/mysql

chown  -R  mysql.mysql  /data/mysql

tar -zxvf $M_FILES;cd $M_DIR

cmake  -DCMAKE_INSTALL_PREFIX=$M_PRE -DMYSQL_DATADIR=/data/mysql -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 -DWITH_PARTITION_STORAGE_ENGINE=1  -DEXTRA_CHARSETS=all  -DDEFAULT_CHARSET=utf8  

        -DDEFAULT_COLLATION=utf8_general_ci -DWITH_DEBUG=0 -DMYSQL_USER=mysql -DMYSQL_UNIX_ADDR=/data/mysql/mysql.sock

        make -j2 && make install

  if [ $? -eq 0 ];then

    echo "the mysql is successful"

  else

    echo "the mysql is failed"

    exit

  fi

}



function php_install() {

yum install libxml2-devel libmcrypt-devel bzip2-devel libxml2-devel openssl-devel bzip2 bzip2-devel -y

tar -zxvf $P_FILES;cd $P_DIR

./configure --prefix=$P_PRE --with-config-file-path=$P_PRE/etc   --with-apxs2=$A_PRE/bin/apxs --with-mysql=$M_PRE

make -j2 && make install

  if [ $? -eq 0 ];then 

    echo "the php is successful"

  else

    echo "the php is failed"

    exit

  fi

}



function lamp_zh() {

sed -i "s/#ServerName www.example.com:80/ServerName www.example.com:80/g" /usr/local/apache/conf/httpd.conf

sed -i "s/DirectoryIndex index.html/DirectoryIndex index.php index.html/g"  /usr/local/apache/conf/httpd.conf

sed -i '310a AddType application/x-httpd-php .php'  /usr/local/apache/conf/httpd.conf

/usr/local/apache/bin/apachectl start


/bin/cp  ./$M_DIR/support-files/my-medium.cnf  /etc/my.cnf

/bin/cp  ./$M_DIR/support-files/mysql.server   /etc/init.d/mysqld

chmod 755  /etc/init.d/mysqld

sed -i '38a datadir=/data/mysql' /etc/my.cnf

sed -i '38a basedir=/usr/local/mysql' /etc/my.cnf

/usr/local/mysql/scripts/mysql_install_db  --basedir=/usr/local/mysql   --datadir=/data/mysql  --user=mysql

/etc/init.d/mysqld  start

chkconfig --add mysqld

echo "PATH=$PATH:/usr/local/mysql/bin">>/etc/profile

source  /etc/profile

}



if [ -z $1 ];then 

echo -e "\033[32m please select you count\033[0m"

echo -e "\033[32m 1 安装apr\033[0m"

echo -e "\033[32m 2 安装apr-util\033[0m"

echo -e "\033[32m 3 安装apache\033[0m"

echo -e "\033[32m 4 安装mysql\033[0m"

echo -e "\033[32m 5 安装php\033[0m"

echo -e "\033[32m 6 lamp整合并启动服务\033[0m"

elif [ $1 -eq 1 ];then

ping -c 2 www.baidu.com

if [ $? -eq 0 ];then

 wget $APR_URL && apr_install

else

 apr_install

fi

elif [ $1 -eq 2 ];then

ping -c 2 www.baidu.com

if [ $? -eq 0 ];then

 wget $APR_U_URL  &&  apr_util_install

else

 apr_util_install

fi

elif [ $1 -eq 3 ];then

ping -c 2 www.baidu.com

if [ $? -eq 0 ];then

 wget $A_URL  &&  apache_install

else

 apache_install

fi

elif [ $1 -eq 4 ];then

ping -c 2 www.baidu.com

if [ $? -eq 0 ];then

 wget $M_URL  &&  mysql_install

else

 mysql_install

fi

elif [ $1 -eq 5 ];then

ping -c 2 www.baidu.com

if [ $? -eq 0 ];then

 wget $P_URL  &&  php_install

else

 php_install

fi

elif [ $1 -eq 6 ];then

lamp_zh

else

exit

fi