#!/bin/bash
#2017年11月9日22:00:49
#by author lee
#auto intall lamp with make
#########################
PACKAGE_DIR="/root/package"
IP_ADDR=`ifconfig | grep "Bcast:" | awk '{ print $2 }' | sed 's/^.*addr://g'`

#HTTP PATH
H_NAME=httpd-2.2.34.tar.gz
H_DIR=httpd-2.2.34
H_URL=http://mirror.bit.edu.cn/apache/httpd
H_PREFIX=/usr/local/apache2

#MYSQL PATH
M_NAME=mysql-5.5.20.tar.gz
M_DIR=mysql-5.5.20
M_URL=http://down1.chinaunix.net/distfiles
M_PREFIX=/usr/local/mysql55
M_DATA=/data/mysql

#PHP PATH
P_NAME=php-5.3.28.tar.bz2
P_DIR=php-5.3.28
P_URL=http://mirrors.sohu.com/php
P_PREFIX=/usr/local/php5

# echo "3s later to install lamp!"
# sleep 3

check_error(){
if [ $? != 0 ]
then
    echo "there is error you should check!!!"
    exit 1
fi
}
mkdir -p $PACKAGE_DIR

#########################
install_httpd(){
yum install apr apr-devel apr-util apr-util-devel -y
cd $PACKAGE_DIR
wget $H_URL/$H_NAME
tar zxvf $H_NAME
cd $H_DIR
./configure --prefix=$H_PREFIX/ --enable-rewrite --enable-so
check_error
make && make install
check_error
echo "apache install success!!!!!!!!!!!!"
}

#########################
install_mysql(){
yum  install  cmake  gcc-c++ ncurses-devel ncurses cmake bison library* libncurses5-dev g++ kdelibs5-dev make -y
groupadd mysql
useradd -r -g mysql mysql
cd $PACKAGE_DIR
wget $M_URL/$M_NAME
tar -zxvf $M_NAME
cd $M_DIR
mkdir -p $M_DATA
mkdir -p $M_PREFIX
cmake  .  -DCMAKE_INSTALL_PREFIX=$M_PREFIX/ \
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
-DMYSQL_DATADIR=$M_DATA \
-DSYSCONFDIR=/etc \
-DMYSQL_USER=mysql \
-DMYSQL_TCP_PORT=3306 \
-DWITH_XTRADB_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_EXTRA_CHARSETS=1 \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DEXTRA_CHARSETS=all \
-DWITH_BIG_TABLES=1 \
-DWITH_DEBUG=0
check_error
make && make install
check_error
cd $M_DATA
chown -R mysql:mysql .
cd $M_PREFIX
chown -R mysql:mysql .
/bin/cp support-files/my-large.cnf /etc/my.cnf
/bin/cp support-files/mysql.server /etc/init.d/mysqld 
chmod o+x /etc/init.d/mysqld
chkconfig --add mysqld 
chkconfig --level 35 mysqld on
/usr/local/mysql55/scripts/mysql_install_db --user=mysql --datadir=$M_DATA --basedir=$M_PREFIX/
ln  -s  /usr/local/mysql55/bin/* /usr/bin/
service  mysqld  restart
check_error
echo "mysql install success!!!!!!!!!!!!"
}

#########################
install_php(){
yum install -y libxml2 libxml2-devel
cd $PACKAGE_DIR
wget $P_URL/$P_NAME
tar -xjf $P_NAME
cd $P_DIR
./configure --prefix=$P_PREFIX --with-config-file-path=$P_PREFIX/etc   --with-apxs2=$H_PREFIX/bin/apxs  --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd
check_error
make && make install
check_error
echo "php install success!!!!!!!!!!!!"
sed -i '/AddType .*.gz .tgz$/a\AddType application\/x-httpd-php-source .php5' $H_PREFIX/conf/httpd.conf
sed -i '/AddType .*.gz .tgz$/a\AddType application\/x-httpd-php .php' $H_PREFIX/conf/httpd.conf
sed -i 's/DirectoryIndex index.html/DirectoryIndex index.php index.html index.htm/' $H_PREFIX/conf/httpd.conf
sed -i 's/\#ServerName www.example.com/ServerName 127.0.0.1/' $H_PREFIX/conf/httpd.conf
touch $H_PREFIX/htdocs/index.php
echo "<?php
phpinfo();
?>" > $H_PREFIX/htdocs/index.php
$H_PREFIX/bin/apachectl restart
service iptables stop
setenforce 0
echo "lamp install success!!please input http://$IP_ADDR/"
}

#########################
# set_apache_php(){
# sed -i '/AddType .*.gz .tgz$/a\AddType application\/x-httpd-php-source .php5' $H_PREFIX/conf/httpd.conf
# sed -i '/AddType .*.gz .tgz$/a\AddType application\/x-httpd-php .php' $H_PREFIX/conf/httpd.conf
# sed -i 's/DirectoryIndex index.html/DirectoryIndex index.php index.html index.htm/' $H_PREFIX/conf/httpd.conf
# touch $H_PREFIX/htdocs/index.php
# echo "<?php
# phpinfo();
# ?>" > $H_PREFIX/htdocs/index.php
# $H_PREFIX/bin/apachectl restart
# }

#########################
# install_lamp(){
# install_httpd
# install_mysql
# install_php
# set_apache_php
# service iptables stop
# setenforce 0
# echo "lamp install success!!please input http://$IP_ADDR/"
# }
# install_lamp
#########################
echo "输入对应的数字来完成你要做的操作"
echo "1)    安装apache"
echo "2)    安装mysql"
echo "3)    安装php"
echo "0)    退出"
read -t 10 -p "please input the num you want to do:" input
time="sleep 2"
case $input in 
1)
    install_httpd
    ;;
2)
    install_mysql
    ;;
3)
    install_php
    ;;
0)
    exit 0
    ;;
*)
    echo "please input the right choice"
    ;;
esac