#!/bin/bash

#version:2012.11.29

#create by roc

#dis: this is used to auto install lamp.这是一个Lamp的自动化安装脚本

#rocbg设定变量
lamp_down=/usr/local/src/tarbag
lamp_tar=/usr/local/src/software
lamp_log=/tmp/lamp_install.log
 

#显示data到/tmp/lamp_install.log的日志
echo "####################`date` #################" >> $lamp_log
#提取IP

ip=`ifconfig eth0 | grep Bcast | awk -F ":" '{print $2}' | cut -d " " -f 1`
#显示IP到lamp的日志

echo ip:$ip >> $lamp_log
#如果没有tarbag的文件夹,then建立

if [ ! -d /usr/local/src/tarbag ];then
    mkdir /usr/local/src/tarbag
fi
 

#下载包的地址
#download lamp packet
wget http://labs.renren.com/apache-mirror//httpd/httpd-2.2.21.tar.gz -P $lamp_down
wget http://download.savannah.gnu.org/releases/freetype/freetype-2.4.6.tar.gz -P $lamp_down
wget http://www.boutell.com/gd/http/gd-2.0.33.tar.gz  -P $lamp_down
wget http://ijg.org/files/jpegsrc.v8c.tar.gz -P $lamp_down
wget http://sourceforge.net/projects/libpng/files/libpng15/1.5.4/libpng-1.5.4.tar.gz/download -P $lamp_down
wget http://blog.s135.com/soft/linux/nginx_php/mysql/mysql-5.5.3-m3.tar.gz -P $lamp_down
wget http://cn2.php.net/distributions/php-5.3.8.tar.bz2 -P $lamp_down
 

#没有software,then建立此文件夹
if [ ! -d /usr/local/src/software ];then
    mkdir /usr/local/src/software
fi
 

#解压缩
#uncompress file
cd   $lamp_down
for i in $lamp_down/*.tar.gz 
do
    tar -xzvf $i -C $lamp_tar
done
 
for i in $lamp_down/*.tar.bz2
do
    tar -xjvf $i -C $lamp_tar
done

#源码包安装APACHE
#Apache install
echo "Apache install start!" >>$lamp_log
 
cd $lamp_tar/httpd-2.2.21/
./configure --prefix=/usr/local/apache2 --enable-so  \
--enable-rewrite --enable-vhost-alias --enable-http \
--enable-static-htpasswd 
sleep 10 
make && make install 
sleep 8
 

#做Apache的软连接
ln  -s /usr/local/apache2/bin/apachectl /bin/apachectl
 #modity服务器名称的DirectoryIndex选项
#modity ServerName DirectoryIndex options
sed -i '292c\ServerName $ip:80' /usr/local/apache2/conf/httpd.conf
sed -i '394c\DirectoryIndex index.php index.html index.html.var' /usr/local/apache2/conf/httpd.conf
 
#test apache configure
/bin/apachectl -t >> $lamp_log

echo "`date` Apache is Installed"  >> $lamp_log
#sed -i '6c\SELINUX=disabled' /etc/selinux/config
 
/bin/apachectl start && echo "`date` Apache is started Good!" >> $lamp_log

echo "`date` Please modify httpd.conf ServerName Option" >> $lamp_log

#安装Freetype
#Freetype Install
echo "`date` Freetype install start" >> $lamp_log
cd $lamp_tar/freetype-2.4.6/
./configure --prefix=/usr/local/freetype 
make && make install
sleep 5
echo "`date` Freetype is  installed" >> $lamp_log
sleep 10 
 

#安装JPEG install
#JPEG install
echo "`date` jpeg install start" >> $lamp_log
cd $lamp_tar/jpeg-8
./configure --prefix=/usr/local/jpeg7 
make && make install 
echo "`date` jpeg is installed" >> $lamp_log
sleep 8 
 

#安装libpng
#libpng Install
echo "`date` libpng  install start" >> $lamp_log
cd $lamp_tar/libpng-1.5.4
./configure --prefix=/usr/local/libpng
make && make install 
echo "`date`libpng is installed" >> $lamp_log
sleep 10 
 

#安装gd
#gd install
echo "`date`gd start install"
cd $lamp_tar/gd-2.0.33
./configure --prefix=/usr/local/gd \
--with-png=/usr/local/libpng --with-freetype=/usr/local/freetype \
--with-jpeg=/usr/local/jpeg7 
#modify gd_png.c
sed -i '/png.h/d' gd_png.c 
sed -i '15c\#include "/usr/local/libpng/include/png.h" ' gd_png.c 
 
make && make install 
echo "`date` gd is installed" >> $lamp_log
 

#安装Mysql

#Mysql Install 
 
useradd mysql  && echo "`date` user mysql added" >> $lamp_log
 
cd $lamp_tar/mysql-5.5.3-m3
./configure  --prefix=/usr/local/mysql  --datadir=/mydata --with-charset=utf8 --with-extra-charsets=complex --enable-thread-safe-client --with-big-tables --with-ssl --with-embedded-server --enable-local-infile --enable-assembler --with-plugins=innobase  --with-plugins=partition
 
make && make install 
 
echo "`date` mysql is installed " >> $lamp_log
#about configure file
cp $lamp_tar/mysql-5.5.3-m3
cp support-files/my-large.cnf /etc/my.cnf
chown -R mysql.mysql /usr/local/mysql
 
#pre databases
/usr/local/mysql/bin/mysql_install_db --user=mysql
#service file
cp $lamp_tar/mysql-5.5.3-m3/support-files/mysql.server /etc/init.d/mysql
chmod 755 /etc/init.d/mysql
#start mysql
/etc/init.d/mysql start  && echo "`date` mysql service is ready" >> $lamp_log
 
 
#PHP install 
echo "`date` php-5.3.8 start install " >> $lamp_log
cd $lamp_tar/php-5.3.8
./configure --prefix=/usr/local/php5 --with-apxs2=/usr/local/apache2/bin/apxs --with-jpeg-dir=/usr/local/jpeg7/ --with-gd=/usr/local/gd/ --with-png-dir=/usr/local/libpng/ --with-freetype-dir=/usr/local/freetype/ --enable-mbregex --with-mysql=/usr/local/mysql/ --with-pdo-mysql=/usr/local/mysql/ 
make && make install 
 
#copy file and write to log
cp $lamp_tar/php-5.3.8/php.ini-recommended /usr/local/php5/lib/php.ini && \
echo "`date` php.ini file is copyed to /usr/local/php5/lib/" >> $lamp_log
echo "application/x-httpd-php  php" >> /usr/local/apache2/conf/mime.types
 
#edit test.php
touch $htdocs/test.php
cat >> $htdocs/test.php << CHENG
phpinfo();
?>
echo "Go to http://$ip/test.php" >> $lamp_log
echo "Mysql port 3306 and socket_file is  /tmp/mysql.socket" >> $lamp_log
echo " user=root  paswd=null" >> $lamp_log
echo "##########end################" >> $lamp_log 
cat /usr/local/apache2/build/config.nice >> $lamp_log
/usr/local/php/bin/php -i |grep configure >> $lamp_log
cat /usr/local/mysql/bin/mysqlbug|grep configure >> $lamp_log