#/bin/bash

##################安装apache###############

#日志文件
logfile=/usr/local/osasoft/install_soft.log

#软件安装目录
softpath=/usr/local/osasoft/

#记录日志
log_save(){

        if [ "$?" == "0" ]; then
                        echo "$1 安装成功." >> $logfile
        else
                        echo "$1 安装失败." >> $logfile
                        exit 1
        fi
}



#安装前的准备工作
inst_preparation(){

        ##安装依赖包
        yum -y install wget gcc make gcc-c++ libtermcap-devel autoconf imake automake libtool openssl-devel pam-devel python-devel libxml2-devel xmlto mysql-devel

        #创建临时目录
        mkdir -p /usr/local/osasoft/temp/
  
        cd /usr/local/osasoft/temp/

        #下载apache
        rm -fr {httpd-2.0.64.tar.bz2,zlib-1.2.7.tar.gz,my.cnf,libunwind-1.0.1.tar.gz,gperftools-2.0.tar.gz,mysql-5.1.57.tar.gz,sphinx-0.9.9.tar.gz,gperftools-2.0.tar.gz,libunwind-1.0.1.tar.gz,curl-7.25.0.tar.gz,libxml2-2.7.8.tar.gz,ncurses-5.9.tar.gz,fontconfig-2.9.0.tar.gz,jpegsrc.v8d.tar.gz,libxslt-1.1.26.tar.gz,zlib-1.2.7.tar.gz,freetype-2.4.9.tar.gz,libiconv-1.13.1.tar.gz,mcrypt-2.6.8.tar.gz,php-5.4.3.tar.bz2,gd-2.0.35.tar.gz,libmcrypt-2.5.8.tar.gz,mhash-0.9.9.9.tar.gz,giflib-4.2.0.tar.bz2,libpng-1.5.10.tar.gz}
        wget http://down.osapub.com/soft/mysqlsoft/{httpd-2.0.64.tar.bz2,zlib-1.2.7.tar.gz,my.cnf,libunwind-1.0.1.tar.gz,gperftools-2.0.tar.gz,mysql-5.1.57.tar.gz,sphinx-0.9.9.tar.gz,gperftools-2.0.tar.gz,libunwind-1.0.1.tar.gz,curl-7.25.0.tar.gz,libxml2-2.7.8.tar.gz,ncurses-5.9.tar.gz,fontconfig-2.9.0.tar.gz,jpegsrc.v8d.tar.gz,libxslt-1.1.26.tar.gz,zlib-1.2.7.tar.gz,freetype-2.4.9.tar.gz,libiconv-1.13.1.tar.gz,mcrypt-2.6.8.tar.gz,php-5.4.3.tar.bz2,gd-2.0.35.tar.gz,libmcrypt-2.5.8.tar.gz,mhash-0.9.9.9.tar.gz,giflib-4.2.0.tar.bz2,libpng-1.5.10.tar.gz}

}

###########################安装apache#####################################
inst_zlib() {

rm -fr zlib-1.2.7
mkdir /usr/local/zlib
tar zxvf zlib-1.2.7.tar.gz
cd zlib-1.2.7
./configure --prefix=/usr/local/zlib
make && make install

log_save 'zlib-1.2.7'

echo "/usr/local/zlib/lib" >> /etc/ld.so.conf
ldconfig

}


inst_apache(){

        cd ..
        rm -fr httpd-2.0.64
        tar xvf httpd-2.0.64.tar.bz2
        cd  httpd-2.0.64

        ./configure --prefix="$softpath"apache --enable-mods-shared=all --with-z=/usr/local/zlib --enable-cgi --enable-rewrite --enable-deflate --with-mpm=worker

        make && make install

        log_save 'httpd-2.0.64'

}
####################################安装mysql######################################
#增加数据库用户

mysql_adduser(){

        if [ -z $1 ];then
                groupadd -g 88 mysql
                adduser -u 88 -g mysql -s /sbin/nologin mysql
        else
                groupadd -g $1 mysql
                adduser -u $1 -g mysql -s /sbin/nologin mysql
        fi
}


#检测系统版本

if [ -d "/lib64" ];then

        systems='x86_64'
else
        systems='i686'

fi
inst_libunwind(){
        cd "$softpath"temp
        rm -fr libunwind-1.0.1
        tar vxf libunwind-1.0.1.tar.gz
        cd libunwind-1.0.1/
        CFLAGS=-fPIC ./configure

        make clean && make CFLAGS=-fPIC && make CFLAGS=-fPIC install
        log_save libunwind-1.0.1

}


inst_tcmalloc(){
##----------------------------------------------------------
## 使用Google的开源TCMalloc库,提高MySQL在高并发情况下的性能
cd ..
rm -fr gperftools-2.0
tar xvf gperftools-2.0.tar.gz
cd gperftools-2.0
CHOST="x86_64-pc-linux-gnu" CFLAGS="-march=nocona -O2 -pipe" CXXFLAGS="-march=nocona -O2 -pipe" \
./configure
make clean
make && make install
log_save gperftools-2.0
echo "/usr/local/lib" > /etc/ld.so.conf.d/usr_local_lib.conf
/sbin/ldconfig
}

inst_mysql_sphinx(){
#安装mysql-5.1.57支持sphinx-2.0.4
cd ..
rm -fr sphinx-0.9.9
rm -fr mysql-5.1.57
tar xvf sphinx-0.9.9.tar.gz


tar xvf mysql-5.1.57.tar.gz
cd mysql-5.1.57
mkdir storage/sphinx
cp -fr ../sphinx-0.9.9/mysqlse/* ./storage/sphinx/


chmod -R 777 ../mysql-5.1.57
sh BUILD/autorun.sh
CHOST=""$systems"-pc-linux-gnu" CFLAGS="-march=nocona -O2 -pipe" CXXFLAGS="-march=nocona -O2 -pipe" \
./configure --prefix="$softpath"mysql/ \
--enable-assembler \
--with-charset=utf8 \
--with-extra-charsets=gbk,gb2312,utf8 \
--with-extra-charsets=complex \
--with-mysqld-user=mysql \
--with-unix-socket-path=/tmp/mysql.sock \
--without-debug \
--with-pthread \
--without-ndb-debug \
--enable-thread-safe-client \
--with-big-tables \
--with-readline --with-ssl \
--with-embedded-server \
--enable-local-infile \
--with-plugins=partition,innobase,myisammrg,sphinx

make && make install

log_save mysql-5.1.57

cd ..

}

config_mysql(){
cp my.cnf "$softpath"mysql/
mkdir -p "$softpath"mysql/{temp,data,logs}

chown -R mysql:mysql "$softpath"mysql
chmod 777 /usr/local/osasoft/mysql/temp

echo ""$softpath"mysql/bin/mysqld_safe --defaults-file="$softpath"mysql/my.cnf &" > /root/mysql_start
echo ""$softpath"mysql/bin/mysqladmin --defaults-file="$softpath"mysql/my.cnf -uroot -posapub shutdown" >/root/mysql_stop
chmod 700 /root/mysql_*

"$softpath"mysql/bin/mysql_install_db --defaults-file="$softpath"mysql/my.cnf --user=mysql --datadir="$softpath"mysql/data
log_save 'mysql_install_db'

chmod 660 "$softpath"mysql/data/mysql/*
chmod 700 "$softpath"mysql/data/mysql
ln -s "$softpath"mysql/bin/mysql /usr/local/bin/mysql
"$softpath"mysql/bin/mysqld_safe --defaults-file="$softpath"mysql/my.cnf &
sleep 10
"$softpath"mysql/bin/mysqladmin --defaults-file="$softpath"mysql/my.cnf -uroot password osapub
chmod 700 "$softpath"mysql/data/test
chmod 770 "$softpath"mysql/data/ib*

}
###########################安装php###############################
inst_libxml() {

cd ..
rm -fr libxml2-2.7.8
tar zxvf libxml2-2.7.8.tar.gz
cd libxml2-2.7.8
./configure --prefix=/usr/local/libxml2  --with-python
make
make install

log_save 'libxml2'

LD_LIBRARY_PATH=/usr/local/lib
LD_LIBRARY_PATH=/usr/local/libxml2/lib
LD_LIBRARY_PATH=/usr/lib/python2.6/site-packages
echo "/usr/local/libxml2/lib" >> /etc/ld.so.conf
echo "/usr/lib/python2.6/site-packages" >> /etc/ld.so.conf
ldconfig

}

inst_libxslt() {

cd ..
rm -fr libxslt-1.1.26
mkdir /usr/local/libxslt
tar zxvf libxslt-1.1.26.tar.gz
cd libxslt-1.1.26
./configure --prefix=/usr/local/libxslt --with-libxml-prefix=/usr/local/libxml2
make
make install

#log_save() 'libxslt-1.1.26 '

echo "/usr/local/libxslt/lib" >> /etc/ld.so.conf
echo "/usr/local/libxslt/lib/python2.6/site-packages" >> /etc/ld.so.conf
ldconfig

}

inst_libiconv() {

cd ..
rm -fr  libiconv-1.13.1
mkdir /usr/local/libiconv
tar zxvf libiconv-1.13.1.tar.gz
cd libiconv-1.13.1
./configure --prefix=/usr/local/libiconv
make
make install

log_save 'libiconv-1.13.1'

echo "/usr/local/libiconv/lib" >> /etc/ld.so.conf
ldconfig

}

inst_mhash() {

cd ..
rm -fr mhash-0.9.9.9
tar zxvf mhash-0.9.9.9.tar.gz
cd mhash-0.9.9.9
./configure
make
make install

log_save 'mhash-0.9.9.9'

echo "/usr/local/lib" >> /etc/ld.so.conf
ln -s /usr/local/lib/libmhash.a        /usr/lib/libmhash.a
ln -s /usr/local/lib/libmhash.la       /usr/lib/libmhash.la
ln -s /usr/local/lib/libmhash.so       /usr/lib/libmhash.so
ln -s /usr/local/lib/libmhash.so.2     /usr/lib/libmhash.so.2
ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib/libmhash.so.2.0.1
ldconfig

}


inst_libmcrypt() {

cd ..
rm -fr libmcrypt-2.5.8
mkdir /usr/local/libmcrypt
tar zxvf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8/libltdl
./configure  --enable-ltdl-install
make
make install
echo "/usr/local/lib" >> /etc/ld.so.conf
ldconfig
cd "$softpath"temp
cd libmcrypt-2.5.8
./configure --prefix=/usr/local/libmcrypt
make && make install

log_save 'libmcrypt-2.5.8'

ln -s /usr/local/libmcrypt/lib/libmcrypt.la         /usr/lib/libmcrypt.la
ln -s /usr/local/libmcrypt/lib/libmcrypt.so         /usr/lib/libmcrypt.so
ln -s /usr/local/libmcrypt/lib/libmcrypt.so.4       /usr/lib/libmcrypt.so.4
ln -s /usr/local/libmcrypt/lib/libmcrypt.so.4.4.8   /usr/lib/libmcrypt.so.4.4.8
cp /usr/local/libmcrypt/bin/libmcrypt-config /usr/bin/libmcrypt-config
cp /usr/local/libmcrypt/lib/libmcrypt.* /usr/lib
echo "/usr/local/libmcrypt/lib" >>  /etc/ld.so.conf
ldconfig

}


inst_mcrypt() {

cd ..
rm -fr mcrypt-2.6.8
mkdir /usr/local/mcrypt
tar zxvf mcrypt-2.6.8.tar.gz
cd mcrypt-2.6.8
LD_LIBRARY_PATH=/usr/local/lib
./configure --prefix=/usr/local/mcrypt --with-libmcrypt-prefix=/usr/local/libmcrypt --with-libiconv-prefix=/usr/local/libiconv
make && make install

log_save 'mcrypt-2.6.8 '

}


inst_curl() {

cd ..
rm -fr curl-7.25.0
tar zxvf curl-7.25.0.tar.gz
cd curl-7.25.0
./configure
make && make install

log_save 'curl-7.25.0'
ldconfig

}


inst_freetype() {

cd ..
rm -fr freetype-2.4.9
mkdir /usr/local/freetype2
tar zxvf freetype-2.4.9.tar.gz
cd freetype-2.4.9
./configure --prefix=/usr/local/freetype2
make && make install

log_save 'freetype-2.4.9'

echo "/usr/local/freetype2/lib"  >> /etc/ld.so.conf
ldconfig

}


inst_fontconfig() {

mkdir /usr/local/fontconfig
tar zxvf fontconfig-2.9.0.tar.gz
cd fontconfig-2.9.0
export PKG_CONFIG_PATH=/usr/local/libxml2/lib/pkgconfig:$PKG_CONFIG_PATH
cp -r /usr/local/libxml2/include/libxml2/libxml/ /usr/include/
./configure --prefix=/usr/local/fontconfig --with-freetype-config=/usr/local/freetype2/bin/freetype-config
make && make install

log_save 'fontconfig-2.9.0'

echo "/usr/local/fontconfig/lib" >> /etc/ld.so.conf
ldconfig

}


inst_libpng() {

cd ..
rm -fr libpng-1.5.10
mkdir /usr/local/libpng2
tar zxvf libpng-1.5.10.tar.gz
cd libpng-1.5.10/
export LDFLAGS=-L/usr/local/zlib/lib
export CFLAGS=-I/usr/local/zlib/include
./configure --prefix=/usr/local/libpng2
make && make install

log_save 'libpng-1.5.10'

echo "/usr/local/libpng2/lib" >> /etc/ld.so.conf
ldconfig

}


inst_jpeg_8d() {

cd ..
rm -fr jpeg-8d
mkdir /usr/local/jpeg8
mkdir /usr/local/jpeg8/include
mkdir /usr/local/jpeg8/lib
mkdir /usr/local/jpeg8/bin
mkdir /usr/local/jpeg8/man/man1/ -p
tar zxvf jpegsrc.v8d.tar.gz
cd jpeg-8d
./configure --prefix=/usr/local/jpeg8 --enable-shared --enable-static
make && make install

log_save 'jpeg-8d'

echo "/usr/local/jpeg8/lib" >> /etc/ld.so.conf
ldconfig

}


inst_gd() {

cd ..
rm -fr gd
mkdir /usr/local/gd2
tar zxvf gd-2.0.35.tar.gz
cd gd/2.0.35
cp /usr/local/libpng2/include/pngconf.h "$softpath"temp/gd/2.0.35/
sed -i "s/#include \"png.h\"/#include \"\/usr\/local\/libpng2\/include\/png.h\"/g" gd_png.c
./configure --prefix=/usr/local/gd2  --with-jpeg=/usr/local/jpeg8 --with-freetype=/usr/local/freetype2 --with-png=/usr/local/libpng2 --with-fontconfig=/usr/local/fontconfig
make && make install
if [ "$?" == "0" ]; then
        echo "gd-2.0.35安装成功." >> "$softpath"install_soft.log
else
        echo "gd-2.0.35安装失败." >> "$softpath"install_soft.log
        exit 1
fi
echo "/usr/local/gd2/lib" >> /etc/ld.so.conf
ldconfig


}


inst_giflib() {

cd "$softpath"temp
rm -fr giflib-4.2.0
tar jxvf giflib-4.2.0.tar.bz2
cd giflib-4.2.0
./configure
make -i && make install

log_save 'giflib-4.2.0'

}


inst_php() {

cd ..
rm -fr php-5.4.3
tar jxf php-5.4.3.tar.bz2
cd php-5.4.3
#export LDFLAGS=-L/usr/lib64/mysql
if [ -d "/lib64" ];then

        systems='x86_64'
                export LDFLAGS=-L/usr/lib64/mysql
else
        systems='i686'

fi
./configure  --prefix="$softpath"php --with-config-file-path="$softpath"php/etc --with-apxs2="$softpath"apache/bin/apxs --with-gd=shared --with-jpeg-dir=/usr/local/jpeg8  --with-png-dir=/usr/local/libpng2 --enable-gd-native-ttf --with-zlib-dir=/usr/local/zlib --with-freetype-dir=/usr/local/freetype2 --enable-zip --with-libxml-dir=/usr/local/libxml2 --with-mysql="$softpath"mysql --with-mysqli="$softpath"mysql/bin/mysql_config --with-pdo-mysql="$softpath"mysql --with-curl --with-curlwrappers --with-mcrypt=/usr/local/libmcrypt --enable-mbstring=all --with-mhash --enable-soap --enable-sockets --with-pear --enable-gd-native-ttf --enable-ftp --disable-ipv6 --with-iconv --without-sqlite --without-pdo-sqlite --with-openssl --enable-zend-multibyte --enable-maintainer-zts --enable-static --enable-fastcgi --enable-force-cgi-redirect --enable-mbregex --enable-inline-optimization --enable-shmop --enable-sysvsem --enable-safe-mode --enable-bcmath --disable-debug --disable-rpath --enable-discard-path
make
make install

log_save 'php-5.4.3'
sed -i '880a AddType application/x-httpd-php .php .phtml' /usr/local/osasoft/apache/conf/httpd.conf
echo 'Php all soft install' >> "$softpath"install_soft.log

}

inst_main(){

        #安装前的准备工作
        inst_preparation
        #安装zlib
        inst_zlib
        #安装apache
        inst_apache
                #添加用户
        mysql_adduser
                inst_libunwind
        inst_tcmalloc
        #安装带sphinx插件的mysql
        inst_mysql_sphinx
        #配置数据库
        config_mysql
        #安装PHP常用扩展
        inst_libxml
        inst_libxslt
        inst_libiconv
        inst_mhash
        inst_libmcrypt
        inst_mcrypt
        inst_curl
        inst_freetype
        inst_fontconfig
        inst_libpng
        inst_jpeg_8d
        inst_gd
        inst_giflib

        #安装PHP
        inst_php

}


#安装程序
inst_main

此脚本需要自己配置php主页