菜單腳本(簡寫)

#!/bin/bashphp

######################################mysql

## system:CentOS 6.5 64bit          ##linux

## Author:coral                     ##nginx

## QQ:791880666                     ##c++

## E-mail:zyjqianfuyu@163.com       ##redis

## Date:          16:30 2014-11-11  ##sql

## Version: 1.1                     ##mongodb

## LNMP full install                ##apache

######################################bash


#loading functions

if [ -x /etc/init.d/functions ];then

   . /etc/init.d/functions 

else  

   chmod +x /etc/init.d/functions

   . /etc/init.d/functions

fi


###update time

hwclock -s && action "update time is ok" /bin/true || exit 1


#define variable

Tools_Dir="/usr/local/src/"

Nginx_Dir="/opt/nginx"       #nginx1.72

Mysql_Dir="/opt/mysql"       #mysql-5.5.28

Php_Dir="/opt/php"           #php-5.4.31

Apache_Dir="/opt/apache"     #apache-2.2.22

Redis_Dir="/opt/redis"       #redis-2.8.9

Mongodb_Dir="/opt/mongodb"   #mongodb-2.6.0

Ip="`ifconfig eth0 |awk -F'[ :]' '/inet addr/ {print $13}'`"

Log_File="/tmp/install.log"

Err_Log="/tmp/install_err.log"

Lan_Ip="`ifconfig eth0|grep 'inet addr'|cut -d ":" -f2|cut -d " " -f1`"


#install dir


#create tools dir

[ ! -d $Tools_Dir ] && mkdir $Tools_Dir


#install apache

install_apache(){

   printf "Install apache,please wait...\n"

   #judge

   [ -d ${Apache_Dir} ] || mkdir -p ${Apache_Dir}

   [ -n `rpm -qa | grep httpd` ] && /etc/init.d/httpd stop > /dev/null 2>& 1

   #install rpm packet

   yum -y install gcc gcc-c++ zlib-devel openssl-devel make pcre-devel apr-deve /dev/null 2>&1

   #create apache user and group

   [ -z "`grep apache /etc/passwd`" ] && {

   groupadd apache

   useradd -g apache -s /sbin/nologin -M apache

   }

   #install apache

   cd $Tools_Dir

#  wget http://nginx.org/download/nginx-1.3.4.tar.gz >>$Log_File 2>&1

   tar zxf ${Tools_Dir}httpd-2.2.22.tar.gz 

   chown -R apache:apache ./httpd-2.2.22 && cd httpd-2.2.22 || exit 1

    ./configure \

    --prefix=${Apache_Dir} \

    --user=apache --group=apache \

    --enable-deflate --enable-expires\

    --enable-headers --enable-modules=most \

    --enable-so --with-mpm=work --enable-rewrite >> $Log_File 2>&1

    [ $? -ne 0 ] && tail -30 $Log_File |tee -a $Err_Log && exit 1;

    make >>$Log_File 2>&1

    [ $? -ne 0 ] && tail -30 $Log_File |tee -a $Err_Log && exit 1;

    make install >>$Log_File 2>&1

    [ $? -ne 0 ] && tail -30 $Log_File |tee -a $Err_Log && exit 1;

    cd ../

    #start apache

    ${Apache_Dir}/sbin/apachectl start

    if [ -n "`netstat -tnl |grep 80`" ];then

        printf "Install Apache Success,Please Input http://$Ip Access.\n"

    fi


}


#install nginx

install_nginx(){

    printf "Install Nginx,Please wait...\n"

    #judge 

    [ -d $Nginx_Dir ] || mkdir $Nginx_Dir -p

    [ -n `rpm -qa |grep httpd` ] && /etc/init.d/httpd stop >/dev/null 2>&1


    #install rpm packet

    yum install gcc gcc-c++ openssl openssl-devel pcre-devel zlib-devel -y >>$Log_File 2>&1

    #create nginx user and group

    [ -z "`grep nginx /etc/passwd`" ] && {

    groupadd nginx

    useradd -g nginx -s /sbin/nologin -M nginx

    }

    #install nginx

    cd $Tools_Dir

#   wget http://nginx.org/download/nginx-1.3.4.tar.gz >>$Log_File 2>&1

    tar zxf ${Tools_Dir}nginx-1.7.2.tar.gz

    chown -R nginx:nginx ./nginx-1.7.2 && cd nginx-1.7.2 || exit 1

    ./configure \

    --prefix=$Nginx_Dir \

    --user=nginx --group=nginx \

    --with-http_stub_status_module \

    --with-http_ssl_module \

    --with-http_gzip_static_module >> $Log_File 2>&1

    [ $? -ne 0 ] && tail -30 $Log_File |tee -a $Err_Log && exit 1;

    make >>$Log_File 2>&1

    [ $? -ne 0 ] && tail -30 $Log_File |tee -a $Err_Log && exit 1;

    make install >>$Log_File 2>&1

    [ $? -ne 0 ] && tail -30 $Log_File |tee -a $Err_Log && exit 1;

    cd ../

    #start nginx

    $Nginx_Dir/sbin/nginx

    if [ -n "`netstat -tnl |grep 80`" ];then

        printf "Install Nginx Success,Please Input http://$Ip Access.\n"

    fi

}



#install mysql

install_mysql(){

    printf "Install MySQL,Please wait...\n"

    #judge 

    [ -d $Mysql_Dir ] || mkdir $Mysql_Dir -p

    [ "`cat /proc/cpuinfo |grep "model name" |wc -l`" -lt 4 ] && echo "提示:您的CPU達不到4核,建議修改"make -j 4"參數,以避免影響您的安裝速度,按Ctrl+C取消"

    sleep 10

    #install rpm packet

    yum install gcc gcc-c++ ncurses-devel bison -y >>$Log_File 2>&1

    #create mysql user and group

    [ -z "`grep mysql /etc/passwd`" ] && {

    groupadd mysql

    useradd -g mysql -s /sbin/nologin -M mysql

    }

    #install cmake

    cd $Tools_Dir

#   wget http://www.cmake.org/files/v2.8/cmake-2.8.8.tar.gz >>$Log_File 2>&1

    tar zxf ${Tools_Dir}cmake-2.8.8.tar.gz

    chown -R mysql:mysql ./cmake-2.8.8 && cd cmake-2.8.8 || exit 1

    ./configure >> $Log_File 2>&1

    [ $? -ne 0 ] && tail -30 $Log_File |tee -a $Err_Log && exit 1;

    make >>$Log_File 2>&1

    [ $? -ne 0 ] && tail -30 $Log_File |tee -a $Err_Log && exit 1;

    make install >>$Log_File 2>&1

    [ $? -ne 0 ] && tail -30 $Log_File |tee -a $Err_Log && exit 1;

    #install mysql

    cd $Tools_Dir

#   wget http://mysql.ntu.edu.tw/Downloads/MySQL-5.5/mysql-5.5.28.tar.gz >/dev/null 2>&1

    tar zxf ${Tools_Dir}mysql-5.5.28.tar.gz

    chown -R mysql:mysql ./mysql-5.5.28 && cd ${Tools_Dir}mysql-5.5.28 || exit 1

    cmake \

    -DCMAKE_INSTALL_PREFIX=$Mysql_Dir \

    -DMYSQL_UNIX_ADDR=/tmp/mysql.sock \

    -DDEFAULT_CHARSET=utf8 \

    -DDEFAULT_COLLATION=utf8_general_ci \

    -DWITH_EXTRA_CHARSETS=all \

    -DWITH_INNOBASE_STORAGE_ENGINE=1 \

    -DWITH_FEDERATED_STORAGE_ENGINE=1 \

    -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \

    -DWITH_EXAMPLE_STORAGE_ENGINE=1 \

    -DWITH_partition_STORAGE_ENGINE=1 \

    -DWITH_SSL=system \

    -DWITH_READLINE=1 \

    -DWITH_ZLIB=system \

    -DENABLED_LOCAL_INFILE=1 >> $Log_File 2>&1

    [ $? -ne 0 ] && tail -30 $Log_File |tee -a $Err_Log && exit 1;

    make -j 4 >>$Log_File 2>&1

    [ $? -ne 0 ] && tail -30 $Log_File |tee -a $Err_Log && exit 1;

    make install >>$Log_File 2>&1

    [ $? -ne 0 ] && tail -30 $Log_File |tee -a $Err_Log && exit 1;

    #PATH variable

    PATH=$PATH:$Mysql_Dir/bin

    echo "PATH=$PATH:$Mysql_Dir/bin" >>/etc/profile

    source /etc/profile

    #create directory

    mkdir $Mysql_Dir/data

    mkdir $Mysql_Dir/tmp

    #accredit mysql user

    chown -R mysql:mysql $Mysql_Dir

    #init database

    cd $Mysql_Dir       #must into mysql_dir init database

    $Mysql_Dir/scripts/mysql_install_db --datadir=$Mysql_Dir/data/ --user=mysql >>$Log_File 2>&1

    [ $? -ne 0 ] && tail -30 $Log_File |tee -a $Err_Log && exit 1;

    #create start and config file

    cd ${Tools_Dir}mysql-5.5.28 

    cp support-files/my-medium.cnf /etc/my.cnf

    cp support-files/mysql.server /etc/init.d/mysqld   #Start writing your own scripts.

    chmod 700 /etc/init.d/mysqld

    chkconfig --add mysqld

    chkconfig mysqld on

    #start mysqld

    $Mysql_Dir/bin/mysqld_safe --defaults-file=/etc/my.cnf & >>$Log_File 2>&1

    [ $? -ne 0 ] && tail -30 $Log_File |tee -a $Err_Log && exit 1;

    if [ -n "`netstat -tnl |grep 3306`" ];then

        printf "Install MySQL Success,Please Login Test.\n"

    fi

}



#install php

install_php(){

    printf "Install PHP,Please wait...\n"

    #judge 

    [ -n "`rpm -qa |grep php`" ] && echo "檢測到您機器上有rpm安裝的PHP環境,防止衝突請先卸載原先安裝的PHP環境,按Ctrl+C取消"

    sleep 10

    [ -d $Php_Dir ] || mkdir $Php_Dir -p

    #install rpm packet

    yum install zlib libxml libjpeg freetype libpng gd curl libconv zili-devl libxml2-devel libjpeg-devel freetype-devel libpng-devel gd-devel curl-devel libgcrypt libgcrypt-devel libxslt libxslt-devel bzip2 bzip2-devel libmcrypt  libmcrypt-devel -y >>$Log_File 2>&1

    #install libiconv

    cd $Tools_Dir

    tar zxf libmcrypt-2.5.7.tar.gz

    chown -R root:root libmcrypt-2.5.7 && cd ${Tools_Dir}libmcrypt-2.5.7 || exit 1

    ./configure --prefix=/usr/local/libmcrypt >>$Log_File 2>&1

    [ $? -ne 0 ] && tail -30 $Log_File |tee -a $Err_Log && exit 1;

    make >>$Log_File 2>&1

    [ $? -ne 0 ] && tail -30 $Log_File |tee -a $Err_Log && exit 1;

    make install >>$Log_File 2>&1

    [ $? -ne 0 ] && tail -30 $Log_File |tee -a $Err_Log && exit 1;

    cd ../ 

#   wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz >>$Log_File 2>&1

    tar zxf libiconv-1.14.tar.gz

    chown -R root:root libiconv-1.14 && cd ${Tools_Dir}libiconv-1.14 || exit 1

    ./configure --prefix=/usr/local/libiconv >>$Log_File 2>&1

    [ $? -ne 0 ] && tail -30 $Log_File |tee -a $Err_Log && exit 1;

    make >>$Log_File 2>&1

    [ $? -ne 0 ] && tail -30 $Log_File |tee -a $Err_Log && exit 1;

    make install >>$Log_File 2>&1

    [ $? -ne 0 ] && tail -30 $Log_File |tee -a $Err_Log && exit 1;

    cd ../

    #install php

    cd $Tools_Dir

#   wget http://cn.php.net/get/php-5.4.31.tar.gz/from/cn2.php.net/mirror >>$Log_File 2>&1

    tar zxf ${Tools_Dir}php-5.4.31.tar.gz 

    chown -R root:root php-5.4.31 && cd php-5.4.31 || exit 1

    ./configure \

    --prefix="$Php_Dir" \

    --with-mysql="$Mysql_Dir" \

    --with-config-file-path="$Php_Dir"/etc \

    --with-config-file-scan-dir="$Php_Dir"/etc/php.d \

    --enable-bcmath \

    --with-bz2 \

    --with-curl \

    --enable-ftp \

    --with-gd \

    --enable-mbstring \

    --with-mcrypt=/usr/local/libmcrypt \

    --enable-mysqlnd \

    --with-mysql=mysqlnd \

    --with-pdo-mysql=mysqlnd \

    --with-mysqli \

    --with-openssl \

    --enable-pcntl \

    --with-pcre-dir=/usr/bin/pcre-config \

    --enable-soap \

    --enable-sockets \

    --enable-sysvmsg \

    --enable-sysvsem \

    --enable-sysvshm \

    --with-jpeg-dir \

    --with-png-dir \

    --with-freetype-dir \

    --with-libxml-dir \

    --with-xmlrpc \

    --with-zlib-dir \

    --enable-zip \

    --enable-fpm >>$Log_File 2>&1

    [ $? -ne 0 ] && tail -30 $Log_File |tee -a $Err_Log && exit 1;

    make >>$Log_File 2>&1

    [ $? -ne 0 ] && tail -30 $Log_File |tee -a $Err_Log && exit 1;

    make install >>$Log_File 2>&1

    [ $? -ne 0 ] && tail -30 $Log_File |tee -a $Err_Log && exit 1;

    #create config file

    cd ${Tools_Dir}php-5.4.31/

    cp php.ini-production $Php_Dir/lib/php.ini

    cp $Php_Dir/etc/php-fpm.conf.default $Php_Dir/etc/php-fpm.conf

    #start php-fpm

    $Php_Dir/sbin/php-fpm >/dev/null 2>&1   #start php-fpm

    [ $? -ne 0 ] && tail -30 $Log_File |tee -a $Err_Log && exit 1;

    if [ -n "`netstat -tnl |grep 9000`" ];then

        printf "Install PHP Success,Please Look 9000 Port.\n"

    fi

}



#install php cache plugin

#PHP extensions directory path

Php_Extensions_Dir(){

    Extensions_Dir=`dirname $(find $Php_Dir -name \*.so|head -1)`

    echo "extension_dir = "$Extensions_Dir"" >> $Php_Dir/lib/php.ini

    }


#xcache參數要在eaccelerator參數前面

install_php_XCache(){

    printf "Install XCache,Please wait...\n"

    cd $Tools_Dir

#   wget http://xcache.lighttpd.net/pub/Releases/1.3.2/xcache-3.0.4.tar.gz >>$Log_File 2>&1

    tar zxf ${Tools_Dir}xcache-3.0.4.tar.gz

    chown -R root:root xcache-3.0.4 && cd xcache-3.0.4 || exit 1

    $Php_Dir/bin/phpize >>$Log_File 2>&1

    ./configure \

    --enable-xcache \

    --with-php-config=$Php_Dir/bin/php-config >>$Log_File 2>&1

    [ $? -ne 0 ] && tail -30 $Log_File |tee -a $Err_Log && exit 1;

    make >>$Log_File 2>&1

    [ $? -ne 0 ] && tail -30 $Log_File |tee -a $Err_Log && exit 1;

    make install >>$Log_File 2>&1

    [ $? -ne 0 ] && tail -30 $Log_File |tee -a $Err_Log && exit 1;

    cd ../

    if [ -n "`find $Php_Dir -name xcache.so`" ];then

        printf "Install XCache Success.\n"

    fi

    #config xcache

    cat >>$Php_Dir/lib/php.ini<< EOF

extension = xcache.so

[xcache]

xcache.shm_scheme =        "mmap"

xcache.size  =               60M

xcache.count =                 4

xcache.slots =                16K

xcache.ttl   =                 0

xcache.gc_interval =           0

xcache.var_namespace_mode =    0

xcache.var_namespace =        ""

xcache.readonly_protection = Off

xcache.mmap_path =    "/dev/zero"

xcache.coredump_directory =   ""

xcache.coredump_type =         0

xcache.disable_on_crash =    Off

xcache.experimental =        Off

xcache.cacher =               On

xcache.stat   =               On

xcache.optimizer =           Off

[xcache.coverager]

xcache.coverager =           Off

xcache.coverager_autostart =  On

xcache.coveragedump_directory = ""

EOF

}


install_php_mysqli(){

    printf "Install mysqli,Please wait...\n"

    cd $Tools_Dir/php-5.4.31/ext/mysqli

    $Php_Dir/bin/phpize >>$Log_File 2>&1

    yum install mysql-devel -y >>$Log_File 2>&1

    ./configure \

    --with-php-config=$Php_Dir/bin/php-config \

    --with-mysqi=/usr/local/mysql/bin/mysql_config >>$Log_File 2>&1

    [ $? -ne 0 ] && tail -30 $Log_File |tee -a $Err_Log && exit 1;

    make >>$Log_File 2>&1

    [ $? -ne 0 ] && tail -30 $Log_File |tee -a $Err_Log && exit 1;

    make install >>$Log_File 2>&1

    [ $? -ne 0 ] && tail -30 $Log_File |tee -a $Err_Log && exit 1;

    cd ../

    if [ -n "`find $Php_Dir -name mysqli.so`" ];then

        printf "Install mysqli Success.\n"

    fi

    #configure mysqli

    cp $Php_Dir/lib/php.ini $Php_Dir/lib/php.ini.$(date +%F%T)

    echo "extension = mysqli.so" >>$Php_Dir/lib/php.ini

}



#install redis

install_redis(){

   printf "Install redis,please wait...\n"

   #judge

   [ -d ${Redis_Dir} ] || mkdir -p ${Redis_Dir}

   #install rpm packet

   yum -y install gcc gcc-c++ tcl zlib-devel openssl-devel make pcre-devel apr-devel > /dev/null 2>&1

   

   #install Redis

   cd $Tools_Dir

#  wget http://nginx.org/download/nginx-1.3.4.tar.gz >>$Log_File 2>&1

   tar zxf ${Tools_Dir}redis-2.8.9.tar.gz && chown -R root:root ./redis-2.8.9 || exit 1

   [ $? -eq 0 ] && cd ./redis-2.8.9 || exit 1

   make >> $Log_File 2>&1

   [ $? -ne 0 ] && tail -30 $Log_File |tee -a $Err_Log && exit 1;

   make test >> $Log_File 2>&1

   [ $? -ne 0 ] && tail -30 $Log_File |tee -a $Err_Log && exit 1;

   make PREFIX=${Redis_Dir} install >>$Log_File 2>&1

   [ $? -ne 0 ] && tail -30 $Log_File |tee -a $Err_Log && exit 1;

   cp redis.conf /opt/redis/ && cd ../ || exit 1

   

   #start redis

   sed -i "s#daemonize no#daemonize yes#g" ${Redis_Dir}/redis.conf

   [ "`grep -i "daemonize yes" ${Redis_Dir}/redis.conf | wc -l`" -eq 1 ] && echo "redis is ok" || exit 1 

   ${Redis_Dir}/bin/redis-server ${Redis_Dir}/redis.conf

   if [ -n "`netstat -tnl |grep 6379`" ];then

       printf "Install Redis Success.\n"

   else

       exit 1

   fi

}



###install mongodb

install_mongodb(){

   printf "Install mongodb,please wait...\n"

   #judge

   [ -d ${Mongodb_Dir} ] || mkdir -p ${Mongodb_Dir}

   #install rpm packet

   yum -y install gcc gcc-c++ zlib-devel openssl-devel make pcre-devel > /dev/null 2>&1


   #install Mongodb

   cd $Tools_Dir

#  wget http://nginx.org/download/nginx-1.3.4.tar.gz >>$Log_File 2>&1

   tar zxf ${Tools_Dir}mongodb-linux-x86_64-2.6.0.tgz && chown -R root:root ./mongodb-linux-x86_64-2.6.0 || exit 1

   [ $? -eq 0 ] && mv ${Tools_Dir}mongodb-linux-x86_64-2.6.0/* ${Mongodb_Dir}/ && cd ${Mongodb_Dir} 

   mkdir -p /data/mongodb && chmod 755 -R /data/mongodb

   ${Mongodb_Dir}/bin/mongod --dbpath=/data/mongodb --logpath=/data/mongodb/mongodb.log --port 27017 --fork >> $Log_File 2>&1

   if [ $? -eq 0 -a -n "`netstat -tnl |grep 27017`" ];then

       printf "Install Mongodb Success.\n"

   else

       exit 1

   fi

}




install_help(){

cat <<EOF

+------------------------------------------------------------------------------------------------------------------------+

| This is install LNMP scrip.                                                                                            |  

|   {$0 lnmp (install nginx mysql php)}                                                                                  |

|   {$0 php_plugin (install php_xcache php_PDO_MYSQL php_mysqli)}                                                        |

|   {$0 nginx (install nginx)}                                                                                           |

|   {$0 mysql (install mysql)}                                                                                       |

|   {$0 php (install php)}                                                                                               |

+------------------------------------------------------------------------------------------------------------------------+

EOF

}

#########menu

cat << EOF

----------------------------------------

|****Please Enter Your Choice:[0-11]****|

----------------------------------------

(0)  Install Apache 

(1)  Install Nginx 

(2)  Install Mysql

(3)  Install Php

(4)  Install Php_plugin

(5)  Install Redis

(6)  Install Mongodb

(7)  Install Mysql-slave

(8)  Install Redis-slave

(9)  Install Mongodb-slave

(10) Install lnmp

(11) Install lnmp+php_plugin

EOF

read -p "Please Enter Your Choice: " num

case "$num" in

0)

    install_apache

;;

1)

    install_nginx

;;

2)

    install_mysql

;;

3)

    install_php

;;

4)

    install_php_XCache

    install_php_mysqli

    Php_Extensions_Dir

;;

5)

    install_redis

;;

6)

    install_mongodb

;;

7)

    /bin/bash /usr/local/src/mysql-slave.sh > /dev/null 2 >& 1

    [ $? -ne 0 ] && exit 1 || action "mysql-slave is ok" /bin/true

;;

8)

    /bin/bash /usr/local/src/redis-slave.sh > /dev/null 2 >& 1

    [ $? -ne 0 ] && exit 1 || action "redis-slave is ok" /bin/true

;;

9)

    /bin/bash /usr/local/src/mongodb-slave.sh > /dev/null 2 >& 1

    [ $? -ne 0 ] && exit 1 || action "mongodb-slave is ok" /bin/true

;;

10)

    install_nginx

    install_mysql

    install_php

;;

11)

    install_nginx

    install_mysql

    install_php

    install_php_XCache

    install_php_mysqli

    Php_Extensions_Dir

;;

*)

    install_help

;;

esac

相關文章
相關標籤/搜索