#!/bin/bash

#---------------------------- WARNING ------------------------------
# This script is suitable for debian 6.0.1a_64bit.
# This script is install LAMP automacally.
#------------------------------------------------------------------
# Version: 20110511
#==================================================================


### define software version
http_ver="httpd-2.2.17"
mysql_ver="mysql-5.1.56"
php_ver="php-5.2.16"
gd_ver="GD-2.45"
curl_ver="curl-7.21.4"
libxml_ver="libxml2-2.7.8"

### judge the runner is root or not
if [ $(id -u) != "0" ]; then
   printf "Error:please run this script by root!\n"
   exit 1
fi

 

echo "softwares are installing,please wait ........."
sleep 3
### install necessary packages from debian source
apt-get -y install gcc automake make openssl libssl-dev libncurses5-dev g++ libgd2-xpm libgd2-xpm-dev libmcrypt4 libmcrypt-dev autoconf


###  ------------------------------- begin install apache --------------------

### add apache user
groupadd www
useradd -g www www

cd lamp_packages/
tar xzvf ${http_ver}.tar.gz
cd ${http_ver}
./configure --prefix=/usr/local/apache2 --enable-so --enable-mods-shared=most --enable-rewrite --enable-ssl
make
make install
ln -s /usr/local/apache2/bin/apachectl /etc/init.d/apachectl
cp /usr/local/apache2/conf/httpd.conf /usr/local/apache2/conf/httpd.conf.ini

/usr/local/apache2/bin/apachectl -k start

sed -i 's/exit 0//g' /etc/rc.local
echo "/usr/local/apache2/bin/apachectl -k start" >> /etc/rc.local

cd ../..

### ------------------------------ begin install mysql ---------------------

###  create mysql user
groupadd mysql
useradd -g mysql mysql

cd lamp_packages/
tar xzvf ${mysql_ver}.tar.gz
cd ${mysql_ver}
./configure --prefix=/usr/local/mysql --with-charset=utf8 --with-plugins=myisam,innodb_plugin,innobase --with-pthread --enable-assembler --enable-thread-safe-client --with-client-ldflags=-all-static --with-mysqld-ldflags=-all-static
make
make install

cd ../..

chown -R mysql:mysql /usr/local/mysql/
/usr/local/mysql/bin/mysql_install_db --user=mysql
chown -R root /usr/local/mysql/
chown -R mysql /usr/local/mysql/var
cp /usr/local/mysql/share/mysql/my-huge.cnf  /etc/my.cnf
/usr/local/mysql/bin/mysqld_safe --user=mysql &


ln -s /usr/local/mysql/share/mysql/mysql.server /etc/init.d/mysql.server
echo "/usr/local/mysql/share/mysql/mysql.server start" >> /etc/rc.local


### ------------------------ install packages by php relay ---------------

### install GD
cd lamp_packages/
tar xzvf ${gd_ver}.tar.gz
cd ${gd_ver}
perl Makefile.PL PREFIX=/usr/local/GD2
make
make install
cd ../..

### install libxml2
cd lamp_packages/
tar xzvf ${libxml_ver}.tar.gz
cd ${libxml_ver}
./configure --prefix=/usr/local/libxml2
make
make install
cd ../..

### install curl
cd lamp_packages/
tar xzvf ${curl_ver}.tar.gz
cd ${curl_ver}
./configure --prefix=/usr/local/curl
make
make install
cp /usr/include/gd.h /usr/local/GD2/
cp /usr/lib/libgd.so /usr/local/GD2/
cd ../..

### ----------------------------------begin install php ----------------------
cd lamp_packages/
tar xzvf ${php_ver}.tar.gz
cd ${php_ver}
./configure --prefix=/usr/local/php5 --with-apxs2=/usr/local/apache2/bin/apxs  --with-mysql=/usr/local/mysql/ --with-libxml-dir=/usr/local/libxml2   --with-gd=/usr/local/GD2 --with-jpeg-dir  --with-png-dir --enable-mbstring  --enable-soap --with-curl=/usr/local/curl --with-pdo-mysql=/usr/local/mysql  --enable-sockets --with-xmlrpc --with-mcrypt --with-freetype-dir
make
make install
cp php.ini-dist  /usr/local/php5/lib/php.ini
echo "AddType application/x-httpd-php .php .phtml" >> /usr/local/apache2/conf/httpd.conf
sed -i 's/DirectoryIndex index.html/DirectoryIndex index.php index.html/g' /usr/local/apache2/conf/httpd.conf
cd ../..

### restart services
/usr/local/apache2/bin/apachectl -k restart > /dev/null 2>&1
/usr/local/mysql/share/mysql/mysql.server restart > /dev/null 2>&1


for i in $(seq 5 -1 0) ; do
echo -en "\aInstall will be OK!!! please wait ---- $i ----\r"
sleep 2
done
echo

echo "now checking services ......"
sleep 2

### check apache
if [ "$(netstat -antl | grep 80 | awk -F ":" '{print $4}')"="80" ];then
  echo "apache has been started up!!"
else
  echo "do not forget start your apache server!!"
fi


### check mysql
if [ "$(netstat -antl | grep 3306 | awk -F ":" '{print $4}')"="3306" ];then
  echo "mysql has been started up!!"
else
  echo "do not forget start your mysql server!!"
fi


printf "Install finished , please restart your server to confirm!!!\n"