#!/bin/bash

#auto install LAMP

#by csy 2015-11-07


#Httpd define path variable

A_FILES=apr-1.5.2.tar.gz

A_FILES_DIR=apr-1.5.2

A_URL=http://mirrors.cnnic.cn/apache//apr/

A_PREFIX=/usr/local/apr

AU_FILES=apr-util-1.5.4.tar.gz

AU_FILES_DIR=apr-util-1.5.4

AU_URL=http://mirrors.cnnic.cn/apache//apr/

AU_PREFIX=/usr/local/apr-util

PC_FILES=pcre-8.37.tar.gz

PC_FILES_DIR=pcre-8.37

PC_URL=http://nchc.dl.sourceforge.net/project/pcre/pcre/8.37/

PC_PREFIX=/usr/local/pcre

H_FILES=httpd-2.4.17.tar.gz

H_FILES_DIR=httpd-2.4.17

H_URL=http://mirror.bit.edu.cn/apache//httpd/

H_PREFIX=/usr/local/apache2/


#Mysql define path variable

M_FILES=mysql-5.6.27.tar.gz

M_FILES_DIR=mysql-5.6.27

M_URL=http://mirrors.linuxeye.com/lnmp/src/

M_PREFIX=/usr/local/mysql/


#PHP define path variable

P_FILES=php-5.6.14.tar.gz

P_FILES_DIR=php-5.6.14

P_URL=http://cn2.php.net/distributions/

P_PREFIX=/usr/local/php5/


echo -e "\033[34m-------------------------\033[0m"

echo -e "\033[35mReady to set up LAMP source environment\n

Wait for 5 seconds, please select menu\033[0m"

sleep 5

if [ -z "$1" ];then

echo -e "\033[36mPlease Select install menu follow:\033[0m"

echo -e "\033[32m1)编译安装Apache服务器\033[1m"

echo "2)编译安装Mysql服务器"

echo "3)编译安装PHP服务器"

echo "4)配置index.php并启动LAMP服务"

echo -e "\033[31mUsage: { /bin/sh $0 1|2|3|4|help}\033[0m"

exit

fi


if [[ "$1" -eq "help" ]];then

        echo -e "\033[36mPlease Select Install Menu follow:\033[0m"

        echo -e "\033[32m1)编译安装Apache服务器\033[1m"

        echo "2)编译安装MySQL服务器"

        echo "3)编译安装PHP服务器"

        echo "4)配置index.php并启动LAMP服务"

        echo -e "\033[31mUsage: { /bin/sh $0 1|2|3|4|help}\033[0m"

        exit

fi


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

#Install httpd web server

if [[ "$1" -eq "1" ]];then

wget -c $A_URL/$A_FILES && tar -zxvf $A_FILES && cd $A_FILES_DIR && ./configure --prefix=$A_PREFIX

if [ $? -eq 0 ];then

  make && make install

echo -e "\033[32m The $A_FILES_DIR install OK!\033[0m"

else

echo -e "\033[32m the $A_FILES_DIR install error,please check...\033[0m"

exit 0

fi

wget -c $AU_URL/$AU_FILES && tar -zxvf $AU_FILES && cd $AU_FILES_DIR && ./configure --prefix=$AU_PREFIX -with-apr=$A_PREFIX

if [ $? -eq 0 ];then

  make && make install

echo -e "\033[32m The $AU_FILES_DIR install OK!\033[0m"

else

echo -e "\033[32m the $AU_FILES_DIR install error,please check...\033[0m"

exit 0

fi

wget -c $PC_URL/$PC_FILES && tar -zxvf $PC_FILES && cd $PC_FILES_DIR && ./configure --prefix=$PC_PREFIX

if [ $? -eq 0 ];then

  make && make install

echo -e "\033[32m The $PC_FILES_DIR install OK!\033[0m"

else

echo -e "\033[32m the $PC_FILES_DIR install error,please check...\033[0m"

exit 0

fi

wget -c $H_URL/$H_FILES && tar -zxvf $H_FILES && cd $H_FILES_DIR && ./configure --prefix=$H_PREFIX --with-apr=$A_PREFIX --with-apr-util=$AU_PREFIX  --with-pcre=$PC_PREFIX --enable-so --enable-rewrite

if [ $? -eq 0 ];then

make && make install

cp $H_PREFIX/bin/apachectl /etc/init.d/httpd

echo "# chkconfig: 2345 50 90" >> /etc/init.d/httpd && chkconfig --add httpd && chkconfig --level 35 httpd on && /etc/init.d/httpd restart

echo -e "\033[32m----------------------------------\033[0m"

echo -e "\033[32m The $H_FILES_DIR Server install Success ! \033[0m"

else

echo -e "\033[32m The $H_FILES_DIR Make or Make install ERROP,please check....\033[0m"

exit 0

fi

fi


#Install mysql DB server

if [[ "$1" -eq "2" ]];then

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

wget -c $M_URL/$M_FILES && tar -zxvf $M_FILES && cd $M_FILES_DIR &&  cmake ./ \

-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \

-DMYSQL_DATADIR=/usr/local/mysql/data \

-DSYSCONFDIR=/etc \

-DWITH_MYISAM_STORAGE_ENGINE=1 \

-DWITH_INNOBASE_STORAGE_ENGINE=1 \

-DWITH_MEMORY_STORAGE_ENGINE=1 \

-DMYSQL_UNIX_ADDR=/tmp/mysqld.sock \

-DMYSQL_TCP_PORT=3306 \

-DENABLED_LOCAL_INFILE=1 \

-DWITH_PARTITION_STORAGE_ENGINE=1 \

-DEXTRA_CHARSETS=all \

-DDEFAULT_CHARSET=utf8 \

-DDEFAULT_COLLATION=utf8_general_ci

if [ $? -eq 0 ];then

make -j8 && make install

groupadd mysql

useradd -g mysql -s /sbin/noghion mysql

cd $M_PREFIX 

chown -R mysql.mysql .   

scripts/mysql_install_db --basedir=$M_PREFIX --datadir=$PREFIX/data --user=mysql  

chown -R mysql.mysql ./data

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

bin/mysqld_safe --user=mysql

ln -s $M_PREFIX/bin/mysql /usr/bin/mysql

chmod +x /etc/init.d/mysqld

rm -rf /etc/my.cnf 

cat > /etc/my.cnf <<EOF

[mysqld]

datadir=/usr/local/mysql/data

socket=/tmp/mysqld.sock

user=mysql

# Disabling symbolic-links is recommended to prevent assorted security risks

symbolic-links=0

[mysqld_safe]

log-error=/var/log/mysqld.log

pid-file=/var/run/mysqld/mysqld.pid

EOF

/etc/init.d/mysqld restart

echo -e "\n\033[32m-----------------------------------------------\033[0m"

                echo -e "\033[32mThe $M_FILES_DIR Server Install Success !\033[0m"

        else

                echo -e "\033[32mThe $M_FILES_DIR Make or Make install ERROR,Please Check......"

                exit 0

        fi

fi


#Install PHP server

if [[ "$1" -eq "3" ]];then

yum install libxml2-devel -y

        wget -c $P_URL/$P_FILES &&  tar -zxvf $P_FILES && cd $P_FILES_DIR &&./configure --prefix=$P_PREFIX --with-config-file-path=/usr/local/php/etc   --with-apxs2=$H_PREFIX/bin/apxs --with-mysql=$M_PREFIX/

        if [ $? -eq 0 ];then

                make && make test && make install

                echo -e "\n\033[32m-----------------------------------------------\033[0m"

                echo -e "\033[32mThe $P_FILES_DIR Server Install Success !\033[0m"

        else

                echo -e "\033[32mThe $P_FILES_DIR Make or Make install ERROR,Please Check......"

                exit 0

        fi

fi

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

if [[ "$1" -eq "4" ]];then


sed -i '/DirectoryIndex/s/index.html/index.php index.html/g' $H_PREFIX/conf/httpd.conf 

$H_PREFIX/bin/apachectl restart

echo "AddType     application/x-httpd-php .php" >>$H_PREFIX/conf/httpd.conf

IP=`ifconfig eth0|grep "Bcast"|awk '{print $2}'|cut -d: -f2`

echo "You can access http://$IP/"


cat >$H_PREFIX/htdocs/index.php <<EOF

<?php

phpinfo();

?>

EOF

fi