#!/bin/bash

##################安装mysql################

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

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

#增加数据库用户

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_preparation(){

        #安装依赖包
                yum install libtermcap-devel autoconf gcc make gcc-c++ imake automake libtool wget openssl-devel pam-devel python-devel libxml2-devel xmlto mysql-devel pcre-devel -y
        #创建临时目录
        mkdir -p /usr/local/osasoft/temp/

        cd /usr/local/osasoft/temp/

        #下载需要安装的软件
        rm -fr {libunwind-1.0.1.tar.gz,gperftools-2.0.tar.gz,pcre-8.30.tar.gz,nginx-1.2.0.tar.gz,spawn-fcgi,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,my.cnf,mysql-5.1.57.tar.gz,sphinx-0.9.9.tar.gz}
        wget http://down.osapub.com/soft/mysqlsoft/{libunwind-1.0.1.tar.gz,gperftools-2.0.tar.gz,pcre-8.30.tar.gz,nginx-1.2.0.tar.gz,spawn-fcgi,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,my.cnf,mysql-5.1.57.tar.gz,sphinx-0.9.9.tar.gz}
        
}


log_save(){

        if [ "$?" == "0" ]; then
                        echo "$1 安装成功." >> $logfile
        else
                        echo "$1 安装失败." >> $logfile
                        exit 1
        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_for_nginx################
inst_zlib() {
cd "$softpath"temp
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_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 "$softpath"temp
rm -fr gd/2.0.35
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 /usr/local/osasoft/temp/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 
./configure  --prefix="$softpath"php --with-config-file-path="$softpath"php/etc  --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' 

echo 'Php all soft install' >> "$softpath"install_soft.log

}
###################安装nginx##################
#增加nginx用户
adduser_nginx() {

        groupadd -g 80 www
        adduser -u 80 -g www -s /sbin/nologin www

}
#安装pcre
inst_pcre() {

#安装Nginx所需的pcre库
cd ..
#重复安装pcre的时候会提示此文件已存在错误
rm -fr /usr/local/share/man/man3/pcre16_*
rm -fr pcre-8.30
tar zxvf pcre-8.30.tar.gz
cd  pcre-8.30
CHOST=""$systems"-pc-linux-gnu" CFLAGS="-march=nocona -O2 -pipe" CXXFLAGS="-march=nocona -O2 -pipe" \
./configure

make && make install

log_save 'pcre-8.30'

}


#安装nginx
inst_nginx() {

## 为优化性能,可以安装 google 的 tcmalloc,这个之前在装mysql时,已经安装过了
## 所以我们编译 Nginx 时,加上参数 --with-google_perftools_module
## 然后在启动nginx前需要设置环境变量 export LD_PRELOAD=/usr/local/lib/libtcmalloc.so
## 加上 -O2 参数也能优化一些性能
##
## 默认的Nginx编译选项里居然是用 debug模式的(-g参数),在 auto/cc/gcc 文件最底下,去掉那个 -g 参数
## 就是将  CFLAGS="$CFLAGS -g"  修改为   CFLAGS="$CFLAGS"   或者直接删除这一行

cd ..
rm -fr nginx-1.2.0
tar xvf nginx-1.2.0.tar.gz
cd nginx-1.2.0

sed -i 's#CFLAGS="$CFLAGS -g"#CFLAGS="$CFLAGS "#' auto/cc/gcc

make clean

CHOST=""$systems"-pc-linux-gnu" CFLAGS="-march=nocona -O2 -pipe" CXXFLAGS="-march=nocona -O2 -pipe" \
./configure --user=www --group=www \
--prefix="$softpath"nginx \
--with-http_stub_status_module \
--with-google_perftools_module \
--with-pcre=../pcre-8.30 \
--with-zlib=../zlib-1.2.7

make && make install

log_save 'nginx-1.2.0'

}

config_nginx() {
#创建Nginx日志目录
mkdir -p /data/logs
chmod +w /data/logs
chown -R www:www /data/logs

echo 'export LD_PRELOAD=/usr/local/lib/libtcmalloc.so' > /root/nginx_start
echo 'ulimit -SHn 51200' >> /root/nginx_start
echo "$softpath"nginx/sbin/nginx -c "$softpath"nginx/conf/nginx.conf >> /root/nginx_start
echo "$softpath"nginx/sbin/nginx -t > /root/nginx_reload
#echo kill -HUP `cat "$softpath"nginx/nginx.pid` >> /root/nginx_reload

chmod 700 /root/nginx_*

}

config_fastcgi_nginx() {

# 复制 lighttpd中的spawn-fcgi 程序到PHP目录下使用
cd ..
/bin/cp spawn-fcgi  "$softpath"php/bin/
chmod +x "$softpath"php/bin/spawn-fcgi

echo 'ulimit -SHn 51200' > /root/fastcgi_start
echo "$softpath"php/bin/spawn-fcgi -a 127.0.0.1 -p 8881 -C 256 -u www -f "$softpath"php/bin/php-cgi >> /root/fastcgi_start

chmod 700 /root/fastcgi_start

echo 'pkill -9 php-cgi' > /root/fastcgi_restart
echo 'sleep 2' >> /root/fastcgi_restart
echo 'pkill -9 php-cgi' >> /root/fastcgi_restart
echo 'sleep 2' >> /root/fastcgi_restart
echo '/root/fastcgi_start' >> /root/fastcgi_restart

chmod 700 /root/fastcgi_restart

}


inst_main(){

        #添加用户
        mysql_adduser
        #安装前的准备工作
        inst_preparation
        inst_libunwind
        inst_tcmalloc

        #安装带sphinx插件的mysql
        inst_mysql_sphinx

        #配置数据库
        config_mysql
        
        #安装PHP常用扩展
        inst_zlib
        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
        #增加nginx用户
        adduser_nginx
        #安装pcre
        inst_pcre
        #安装nginx
        inst_nginx
        #配置nginx
        config_nginx
        #安装配置fastcgi
        config_fastcgi_nginx
        
}

#安装程序
inst_main

 

##################################################
脚本是在最小化安装模式的系统上测试完全通过(公司内网服务器都采用的最小化安装的)服务都能正常运行
脚本的编写,测试,安装,生产环境都是在最小化安装的系统上 所以不可能兼容所有环境
如果是server模式安装的系统 那么就需要把zlib函数去掉 如果忘记去掉了 
在SEkinux没关闭的情况下会出现不能重启服务 提示
libz.so.1 模块没权限访问
没关系还有补救的方法 
1.vim /etc/ld.so.conf
2.删除 /usr/local/zlib/lib 这一行
3.回到命令行 输入 ldconfig -v 
完成这3步就行了