lamp环境部署脚本-y

#!/bin/bash
#date:2018-01-01   
#author:by yangjw
#
############################################################       function   配置区域       ##############################################################################
#############################  环境统一安装包  ################################
depend_pkg (){
    yum install -y gcc gcc-c++ make cmake automake  autoconf       \
	ncurses-devel libxml2-devel perl-devel libcurl-devel libgcrypt \
	libgcrypt-devel libxslt libxslt-devel pcre-devel openssl-devel \
	wget   bzip2-devel curl-devel db4-devel libjpeg-*  libpng-*    \
	freetype-devel libXpm-devel gmp-devel  libc-client-devel       \
	openldap-devel gd freetype zlib zlib-devel libtool expat-devel \
	bison  libtermcap-devel  
	CPU_NUM=`cat /proc/cpuinfo  | grep processor | wc  -l`
    let  CPUS=${CPU_NUM}-1
    network_ip=`ip a | grep "em1" | awk '{print $2}' | cut -d / -f1 | egrep "((1[0-9][0-9]|2[0-4][0-9]|25[0-5]|[1-9][0-9]|[0-9]).){3}(1[0-9][0-9]|2[0-4][0-9]|25[0-5]|[1-9][0-9]|[0-9])"`
}
#########################     变量修改函数区域     ############################
All_path_1(){
    Server_ip=${network_ip}              #  本主机IP	 
    Apache_version=2.4.39                #  Apache 服务的版本号,如果没有限制,推荐默认安装的版本为 2.4.25  和 apr 1.5.2 apr-util1.5.4 是兼容的
    Mysql_version=5.6.17                 #  Mysql 版本号,如果没有限制,默认安装的版本为 5.6.17
    Libmcrypt_version=2.5.8              #  安装的版本号 默认推荐2.5.8 
    Mhash_version=0.9.9.9                #  默认推荐的版本0.9.9.9
    Libiconv_version=1.13                #  默认推荐的版本1.13
    Php_version=5.6.32                   #  Php的版本号,如果没有限制,默认安装的版本为 5.6.26
    Apr_version=1.7.0                    #  Apr 的版本号,如果没有兼容性要求,默认安装的版本为 1.5.2
    Apr_util_version=1.6.0               #  Apr_util 的版本号,如果没有兼容性要求,默认安装的版本为 1.5.4 
    Mysql_port=3306                      #  MySQL 的监听端口号
    soft_sign="PXJY_WebSer"              #  隐藏apache名字和版本后,显示的标志名字
}

All_path_2(){
    Work_dir=/app/src                                       #  源码包的存放路径
    App_dir=/app                                            #  如果想把MySQL安装目录和数据目录分开,就启用这个变量参数,针对mysql的数据目录存储的修改

    Web_root_dir=${App_dir}/webroot/project                 #  web 测试网站的程序目录位置
    Web_logs_dir=${App_dir}/applog                          #  web 日志路径位置 
    Web_install_dir=${App_dir}/httpd${Apache_version}       #  apache 的安装路径
    Web_config_dir=${Web_install_dir}/conf                  #  apache 的主配置文件目录 

    Mysql_boost_dir=${App_dir}/mysql${Mysql_version}        #  针对MySQL boost文件配置的存放路径
    Mysql_install_dir=${App_dir}/mysql${Mysql_version}      #  mysql 的安装路径
    Mysql_conf_dir=${App_dir}/mysql${Mysql_version}/etc     #  mysql 配置文件的路径   
    Mysql_data_dir=${App_dir}/mysql${Mysql_version}/data    #  mysql 数据存放目录
    Mysql_log_dir=${App_dir}/mysql${Mysql_version}/data     #  mysql 日志存放目录
    Mysql_socket_dir=${App_dir}/mysql${Mysql_version}/data  #  mysql  socket 存放路径位置 
    Mysql_server_id=${Mysql_port}                           #  mysql  唯一的ID值

    Php_install_dir=${App_dir}/php${Php_version}            #  php 安装路径
    Php_config_dir=${Php_install_dir}/etc                   #  php 配置文件路径
}

#########################     Apache 安装相关函数   ###########################
Apr_install(){
    cd $Work_dir
    [ -f "apr-${Apr_version}.tar.gz" ]             || wget  http://archive.apache.org/dist/apr/apr-${Apr_version}.tar.gz
    tar   xvf   apr-${Apr_version}.tar.gz
    cd apr-${Apr_version}
    echo  -e "\033[34m 开始安装apache依赖软件包apr. . . . . .\033[0m"
    sleep  2
    ./configure --prefix=/usr/local/apr
    make  -j ${CPUS} && make install
}
Apr_util_install(){
    if  [ $? -eq 0 ];then
        echo  -e "\033[34m  Apr  Install  Successed ! \033[0m"
    	sleep  2
    	echo  -e "\033[34m  Beging  Starting   Install  Apr-util  \033[0m"
    	sleep  2
    	cd $Work_dir
        [ -f "apr-util-${Apr_util_version}.tar.gz" ]   || wget  http://archive.apache.org/dist/apr/apr-util-${Apr_util_version}.tar.gz
        tar   xvf   apr-util-${Apr_util_version}.tar.gz
        cd    apr-util-${Apr_util_version}
        sleep  2 
        ./configure  --with-apr=/usr/local/apr
        #./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
        make  -j ${CPUS} &&   make install
    	if  [ $? -eq 0 ];then
    	    echo  -e "\033[34m Apr-util  Install  Successed ! \033[0m" ; sleep 2 
        else
    	    echo  -e "\033[34m Apr-util  Install  Failed ! \033[0m"
    	fi
    else
    	echo  -e "\033[34m  Apr  Install  Failed ! \033[0m"
        exit 1
    fi
    sleep 2
}

#隐藏apache版本号和更改软件名字标志的函数
Hidden_version(){
    sed  -i  "/#define AP_SERVER_BASEPRODUCT/s/Apache/PXJY WebSer/"  ${Work_dir}/httpd-${Apache_version}/include/ap_release.h
}

Apache_install(){
    cd $Work_dir
    [ -f "httpd-${Apache_version}.tar.gz" ]        || wget  http://archive.apache.org/dist/httpd/httpd-${Apache_version}.tar.gz
    tar   zxf   httpd-${Apache_version}.tar.gz
    cd    httpd-${Apache_version}

    #优化apache的请求数操作
    perl -pi -e "s/DEFAULT_SERVER_LIMIT 256/DEFAULT_SERVER_LIMIT 15000/g"  server/mpm/prefork/prefork.c
    Hidden_version;

    echo  -e "\033[34m 开始编译 apache 服务. . . . . .\033[0m"
    sleep 2
    #./configure --prefix=$Web_install_dir   --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util
    ./configure --prefix=$Web_install_dir   --enable-modules=all  --with-apr=/usr/local/apr           \
                --enable-expires=shared --enable-usertrack=shared  --enable-vhost-alias=shared          \
                --enable-rewrite=shared --enable-speling=shared  --enable-proxy=shared --enable-so      \
                --with-mpm=prefork --with-expat=builtin   --enable-ssl 
    make  -j  ${CPUS}  &&   make install
    if  [ $? -eq 0 ] ;then 
        echo  -e  "\033[34m  Apache  Service  Install  Successed ! \033[0m"
        sleep 2
        useradd  -s /sbin/nologin  apache 
        sed -i "s/#ServerName www.example.com:80/ServerName ${Server_ip}:80/g"     ${Web_config_dir}/httpd.conf
        sed -i "$ a Include conf/extra/httpd-vhosts.conf"                          ${Web_config_dir}/httpd.conf
        sed -i "s/User daemon/User apache/g"            ${Web_config_dir}/httpd.conf
        sed -i "s/Group daemon/Group apache/g"          ${Web_config_dir}/httpd.conf
        sed -i 's/DirectoryIndex index.html/DirectoryIndex index.php index.html/g' ${Web_config_dir}/httpd.conf
        sed -i '$ a ServerTokens Prod'                  ${Web_config_dir}/httpd.conf
        sed -i '$ a ServerSignature Off'                ${Web_config_dir}/httpd.conf
        sed -i "s/#LoadModule/LoadModule/g"             ${Web_config_dir}/httpd.conf
##############     Include conf/extra/httpd-vhosts.conf      ###################
        mkdir  -p  ${Web_config_dir}/extra
        mkdir  -p  ${Web_root_dir}
        mkdir  -p  ${Web_logs_dir}
        \cp   ${Web_config_dir}/extra/httpd-vhosts.conf  ${Web_config_dir}/extra/httpd-vhosts.conf.BAK
        echo ${Apache_version}  | grep  "2.4.*"  > /dev/null  2>&1
        if [ $? -eq 0 ] ;then
            #apache 2.4.X 版本的配置,版本2.2 和2.4 版本的配置文件有点区别
            cat  << EOF  >  ${Web_config_dir}/extra/httpd-vhosts.conf
<VirtualHost *:83>
    ServerAdmin    webmaster@dummy-host2.example.com
    DocumentRoot   "${Web_root_dir}"
    ServerName     ${Server_ip}
    ErrorLog       "${Web_logs_dir}/project_error.log"
    CustomLog      "${Web_logs_dir}/project_access.log" combined
    DirectoryIndex index.php index.Html index.html
    #php_admin_value open_basedir  "${Web_root_dir}:/tmp:."
    <Directory "${Web_root_dir}">
        Options  FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>
EOF
        else
            ##apache 2.2.X 版本的配置		
            cat  << EOF  >  ${Web_config_dir}/extra/httpd-vhosts.conf
NameVirtualHost *:80
<VirtualHost *:80>
    DocumentRoot   "${Web_root_dir}"
    ServerName     ${Server_ip}
    DirectoryIndex index.php index.Html index.html
    CustomLog      "${Web_logs_dir}/project_access.log" common
    ErrorLog       "${Web_logs_dir}/project_error.log"
    #限制跨站,安全访问,避免木马文件破坏其他目录下的文件
    php_admin_value open_basedir  "${Web_root_dir}/:/tmp:."
    <Directory "${Web_root_dir}">
        Options -Indexes MultiViews FollowSymLinks
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>
EOF
        fi
        cat  << EOF >> ${Web_config_dir}/httpd.conf
<IfModule prefork.c>
StartServers         10
MinSpareServers      10
MaxSpareServers      30
MaxClients           5000
MaxRequestsPerChild  0
</IfModule>
EOF
########## 配置网站默认页以及连接数据库的状态信息 ##############
        cat << EOF >  ${Web_root_dir}/index.html
<center><h1>Test ${Server_ip} : Apache-${Apache_version}</h1></center>
EOF
        cat << EOF >  ${Web_root_dir}/index.php
<?php
\$link = mysqli_connect("127.0.0.1:${Mysql_port}",'root','');   
if (\$link)
    echo "Success...";
else
    echo "Failure...";
mysql_close(); 
phpinfo();
?>
EOF
#####################   正在启动apahce 服务  ###################
        echo -e "\033[34m 正在启动Apache Service \033[0m"
        sleep 2
        ${Web_install_dir}/bin/apachectl   -k   start
        if [ $? -eq 0 ]; then
            echo  -e  "\033[34m  Apache  Service  Started  Successed ! \033[0m"
        else
            echo  -e  "\033[34m  Apache  Service  Started   Failed ! \033[0m"
        fi
    else
        echo  -e  "\033[34m  Apache  make  install  abnormal ! \033[0m"
        exit 1
    fi
}
#########################     MySQL 安装相关函数   ############################
Config_MySQL_conf(){
cat  <<  EOF  >  ${Mysql_conf_dir}/my.cnf 
[mysqld]
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
innodb_buffer_pool_size = 1024M
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
log_bin = mysql-bin
default-storage-engine = InnoDB
character-set-server = utf8
# These are commonly set, remove the # and set as required.
basedir = ${Mysql_install_dir}
datadir = ${Mysql_data_dir}
port = ${Mysql_port}
server_id = ${Mysql_server_id}
socket = ${Mysql_socket_dir}/mysql.sock
#
#开启安全更新模式
#SQL_SAFE_UPDATES = 1
# per table  for ibdatafile
innodb-file-per-table=1
pid-file=${Mysql_data_dir}/mysql.pid

#binlog do db
#binlog-do-db    = atrip_db_blog
#binlog-do-db    = atrip_db_website
#binlog-ignore-db = mysql
#binlog-ignore-db = test

# add connecton  config by yangjw
max_heap_table_size = 64M
tmp_table_size = 64M
max_connections = 10000
max_connect_errors = 10000

skip_name_resolve = 1
event_scheduler = 1

#slow query log
slow_query_log=on
long_query_time=5

# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M 
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
EOF
}

MySQL_5_6_X(){
    if [[ $Mysql_version =~  ^5.6 ]];then
        id  mysql > /dev/null  2>&1  ||  /usr/sbin/useradd  -s /sbin/nologin  mysql
        mkdir -p ${Mysql_data_dir} ${Mysql_conf_dir}
        cd ${Work_dir}
        [ -f "mysql-${Mysql_version}.tar.gz" ] || wget https://downloads.mysql.com/archives/get/file/mysql-${Mysql_version}.tar.gz    
        tar zxvf mysql-${Mysql_version}.tar.gz -C   $Work_dir/
        cd mysql-${Mysql_version}

        echo -e "\n\033[34m 开始安装编译 mysql . . . . . . \033[0m\n"
        sleep 2
        /usr/bin/cmake -DCMAKE_INSTALL_PREFIX=${Mysql_install_dir}         \
                       -DMYSQL_UNIX_ADDR=${Mysql_socket_dir}/mysql.sock    \
                       -DSYSCONFDIR=${Mysql_conf_dir}                      \
                       -DDEFAULT_CHARSET=utf8                              \
                       -DDEFAULT_COLLATION=utf8_general_ci                 \
                       -DWITH_EXTRA_CHARSETS:STRING=all                    \
                       -DWITH_MYISAM_STORAGE_ENGINE=1                      \
                       -DWITH_INNOBASE_STORAGE_ENGINE=1                    \
                       -DWITH_MEMORY_STORAGE_ENGINE=1                      \
                       -DWITH_READLINE=1                                   \
                       -DENABLED_LOCAL_INFILE=1                            \
                       -DWITH_SSL=bundled                                  \
                       -DWITH_ZLIB=system                                  \
                       -Dwith-mysqld-ldflags=-all-static                   \
                       -Dwith-client-ldflags=-all-static                   \
                       -DMYSQL_DATADIR=${Mysql_data_dir}                   \
                       -DMYSQL_USER=mysql                                  \
                       -DMYSQL_TCP_PORT=${Mysql_port}
        if [ $? -eq 0 ];then
            if  [ $CPUS  -eq 0 ];then
                echo -e  "\e[1;33m  +++++++++++++++++++++    \e[0m"
                echo -e  "\e[1;33m   \  make  开始编译   \   \e[0m"
                echo -e  "\e[1;33m    +++++++++++++++++++++  \e[0m"    
                sleep 3
                make
                echo -e  "\e[1;33m  +++++++++++++++++++++++++    \e[0m"
                echo -e  "\e[1;33m   \make  install  开始编译\   \e[0m"
                echo -e  "\e[1;33m    ++++++++++++++++++++++++   \e[0m"
                sleep 3
                make  install
            else
                echo -e  "\e[1;33m  +++++++++++++++++++++    \e[0m"
                echo -e  "\e[1;33m   \  make  开始编译   \   \e[0m"
                echo -e  "\e[1;33m    +++++++++++++++++++++  \e[0m"    
                sleep 3
                make  -j  ${CPUS}
                echo -e  "\e[1;33m  +++++++++++++++++++++++++    \e[0m"
                echo -e  "\e[1;33m   \make  install  开始编译\   \e[0m"
                echo -e  "\e[1;33m    ++++++++++++++++++++++++   \e[0m"
                sleep 3
                make  install
            fi
            [ $? -ne 0 ] &&  exit 1
            chown -R   mysql.mysql ${Mysql_data_dir}
            cd    ${Mysql_install_dir}
        else
            echo -e  "\n\e[1;33m  编译有错误,请重新检查,编译 !   \e[0m\n"
            exit 1
        fi
    else
        echo -e  "\n\e[1;33m 选择错误,请重新选择! \e[0m\n"
        exit 1
    fi
    chmod  +x   scripts/mysql_install_db

    echo  -e "\e[1;33m 开始初始化数据库实例\e[0m"
    sleep 2
    scripts/mysql_install_db   --user=mysql    --datadir=${Mysql_data_dir}
    [ $? -eq 0 ]  &&  echo  -e  "\n\e[1;33m== 初始化 successed ==\e[0m\n"  ||  echo  -e  "\n\e[1;33m== 初始化 failed ==\e[0m\n"

    Config_MySQL_conf;

    echo  -e "\e[1;33m 开始启动Mysql服务\e[0m"
    sleep 2
    ${Mysql_install_dir}/bin/mysqld_safe --defaults-file=${Mysql_conf_dir}/my.cnf --user=mysql &
    echo  -e "\n\n"

    sleep 3
    ss -ntl | grep  $Mysql_port  > /dev/null  2>&1
    if  [ $?  -eq   0  ] ;then
        echo    
        echo -e "\e[1;33m       +------------------------------------------+ \e[0m"
        echo -e "\e[1;33m      /|                                         /| \e[0m"
        echo -e "\e[1;33m     +------------------------------------------+ | \e[0m"
        echo -e "\e[1;33m     | |                                        | | \e[0m"
        echo -e "\e[1;33m     | +-- Wecome to use MySQL${Mysql_version}------------|-+ \e[0m"
        echo -e "\e[1;33m     |/                                         |/  \e[0m"
        echo -e "\e[1;33m     +___________Successful_____________________+   \e[0m"
        echo -e "\e[1;33m    /                                          /    \e[0m"
        echo -e "\e[1;33m   /            Perfect                       /     \e[0m"   
        echo -e "\e[1;33m  /__________________________________________/      \e[0m"
    else
        echo -e "\e[1;35m    +---------------------------------+        \e[0m"
        echo -e "\e[1;35m    |\          Failed                |\       \e[0m"
        echo -e "\e[1;35m    | +-------------------------------|-+      \e[0m"                           
        echo -e "\e[1;35m    | |                               | |      \e[0m"
        echo -e "\e[1;35m    +-|---I'm Sorry,Please Checking---+-|      \e[0m"
        echo -e "\e[1;35m     \|                                \|      \e[0m"
        echo -e "\e[1;35m      +---------------------------------+      \e[0m"
        echo -e "\e[1;35m       \                                 \     \e[0m"       
        echo -e "\e[1;35m        \       请重新执行脚本            \    \e[0m"
        echo -e "\e[1;35m         \                                 \   \e[0m"
        echo -e "\e[1;35m          +---------------------------------+  \e[0m"   
        exit 1
    fi
}

MySQL_5_7_X(){
    if [[ $Mysql_version =~  ^5.7 ]];then
        id  mysql > /dev/null  2>&1 ||  /usr/sbin/useradd  -s /sbin/nologin  mysql
        mkdir -p ${Mysql_data_dir} ${Mysql_conf_dir}
        cd   ${Work_dir}            
        [ -f "${Work_dir}/boost_1_59_0.tar.gz" ]  || wget  http://liquidtelecom.dl.sourceforge.net/project/boost/boost/1.59.0/boost_1_59_0.tar.gz		
        tar zxvf boost_1_59_0.tar.gz  -C  ${Mysql_boost_dir}

        [ -f "mysql-${Mysql_version}.tar.gz" ]    || wget  https://downloads.mysql.com/archives/get/file/mysql-${Mysql_version}.tar.gz
        tar  zxvf mysql-${Mysql_version}.tar.gz -C   $Work_dir/
        cd   mysql-${Mysql_version}

        echo -e "\n\033[34m 开始安装编译 mysql . . . . . . \033[0m\n"
        sleep 2        
        /usr/bin/cmake -DCMAKE_INSTALL_PREFIX=${Mysql_install_dir}         \
                       -DMYSQL_UNIX_ADDR=${Mysql_socket_dir}/mysql.sock    \
                       -DSYSCONFDIR=${Mysql_conf_dir}                      \
                       -DDEFAULT_CHARSET=utf8                              \
                       -DDEFAULT_COLLATION=utf8_general_ci                 \
                       -DWITH_EXTRA_CHARSETS:STRING=all                    \
                       -DWITH_MYISAM_STORAGE_ENGINE=1                      \
                       -DWITH_INNOBASE_STORAGE_ENGINE=1                    \
                       -DWITH_MEMORY_STORAGE_ENGINE=1                      \
                       -DWITH_READLINE=1                                   \
                       -DENABLED_LOCAL_INFILE=1                            \
                       -DWITH_SSL=bundled                                  \
                       -DWITH_ZLIB=system                                  \
                       -Dwith-mysqld-ldflags=-all-static                   \
                       -Dwith-client-ldflags=-all-static                   \
                       -DMYSQL_DATADIR=${Mysql_data_dir}                   \
                       -DMYSQL_USER=mysql                                  \
                       -DMYSQL_TCP_PORT=${Mysql_port}                      \
                       -DWITH_BOOST=${Mysql_boost_dir}/boost_1_59_0
        if [ $? -eq 0 ];then
            if  [ $CPUS  -eq 0 ];then
                echo -e  "\e[1;33m  +++++++++++++++++++++    \e[0m"
                echo -e  "\e[1;33m   \  make  开始编译   \   \e[0m"
                echo -e  "\e[1;33m    +++++++++++++++++++++  \e[0m"    
                sleep 3
                make
                echo -e  "\e[1;33m  +++++++++++++++++++++++++    \e[0m"
                echo -e  "\e[1;33m   \make  install  开始编译\   \e[0m"
                echo -e  "\e[1;33m    ++++++++++++++++++++++++   \e[0m"
                sleep 3
                make  install
            else
                echo -e  "\e[1;33m  +++++++++++++++++++++    \e[0m"
                echo -e  "\e[1;33m   \  make  开始编译   \   \e[0m"
                echo -e  "\e[1;33m    +++++++++++++++++++++  \e[0m"    
                sleep 3
                make  -j  ${CPUS}
                echo -e  "\e[1;33m  +++++++++++++++++++++++++    \e[0m"
                echo -e  "\e[1;33m   \make  install  开始编译\   \e[0m"
                echo -e  "\e[1;33m    ++++++++++++++++++++++++   \e[0m"
                sleep 3
                make  install
            fi
            [ $? -ne 0 ] &&  exit 1
            chown -R   mysql.mysql ${Mysql_data_dir}
            cd ${Mysql_install_dir}
        else 
            echo -e  "\n\e[1;33m  编译有错误,请重新检查,编译 !   \e[0m\n"
            exit 1
        fi
    else
        echo -e  "\n\e[1;33m 选择错误,请重新选择! \e[0m\n"
        exit 1
    fi

    echo  -e "\n\e[1;33m 开始进行初始化数据库实例\e[0m\n"
    sleep 2
    ${Mysql_install_dir}/bin/mysqld  --initialize-insecure  --user=mysql  --basedir=${Mysql_install_dir}  --datadir=${Mysql_data_dir}
    [ $? -eq 0 ]  &&  echo  -e  "\n\e[1;33m== 初始化 successed ==\e[0m\n"  ||  echo  -e  "\n\e[1;33m== 初始化 failed ==\e[0m\n"    

    Config_MySQL_conf;

    echo -e "\e[1;33m 开始启动Mysql服务\e[0m"
    ${Mysql_install_dir}/bin/mysqld_safe --defaults-file=${Mysql_conf_dir}/my.cnf --user=mysql  &
    echo  -e "\n\n" 

    sleep 3 
    ss -ntl | grep  $Mysql_port  > /dev/null  2>&1
    if  [ $?  -eq   0  ] ;then
        echo    
        echo -e "\e[1;33m       +------------------------------------------+ \e[0m"
        echo -e "\e[1;33m      /|                                         /| \e[0m"
        echo -e "\e[1;33m     +------------------------------------------+ | \e[0m"
        echo -e "\e[1;33m     | |                                        | | \e[0m"
        echo -e "\e[1;33m     | +-- Wecome to use MySQL${Mysql_version}------------|-+ \e[0m"
        echo -e "\e[1;33m     |/                                         |/  \e[0m"
        echo -e "\e[1;33m     +___________Successful_____________________+   \e[0m"
        echo -e "\e[1;33m    /                                          /    \e[0m"
        echo -e "\e[1;33m   /            Perfect                       /     \e[0m"   
        echo -e "\e[1;33m  /__________________________________________/      \e[0m"
    else
        echo -e "\e[1;35m    +---------------------------------+        \e[0m"
        echo -e "\e[1;35m    |\          Failed                |\       \e[0m"
        echo -e "\e[1;35m    | +-------------------------------|-+      \e[0m"                    
        echo -e "\e[1;35m    | |                               | |      \e[0m"
        echo -e "\e[1;35m    +-|---I'm Sorry,Please Checking---+-|      \e[0m"
        echo -e "\e[1;35m     \|                                \|      \e[0m"
        echo -e "\e[1;35m      +---------------------------------+      \e[0m"
        echo -e "\e[1;35m       \                                 \     \e[0m"       
        echo -e "\e[1;35m        \       请重新执行脚本            \    \e[0m"
        echo -e "\e[1;35m         \                                 \   \e[0m"
        echo -e "\e[1;35m          +---------------------------------+  \e[0m"   
        exit 1
    fi
}

MySQL_8_X_X(){
    if [[ $Mysql_version =~  ^8 ]];then
        id    mysql > /dev/null  2>&1 ||  /usr/sbin/useradd  -s /sbin/nologin  mysql
        mkdir -p ${Mysql_data_dir} ${Mysql_conf_dir}
        cd   ${Work_dir}
        [ -f "mysql-boost-${Mysql_version}.tar.gz" ] ||  wget https://downloads.mysql.com/archives/get/file/mysql-boost-${Mysql_version}.tar.gz
        tar  zxvf mysql-boost-${Mysql_version}.tar.gz -C   $Work_dir/
        cd   mysql-${Mysql_version}

        echo -e "\n\033[34m 开始安装编译 mysql . . . . . . \033[0m\n"
        sleep 2
        /usr/bin/cmake -DCMAKE_INSTALL_PREFIX=${Mysql_install_dir}         \
                       -DMYSQL_UNIX_ADDR=${Mysql_socket_dir}/mysql.sock    \
                       -DSYSCONFDIR=${Mysql_conf_dir}                      \
                       -DDEFAULT_CHARSET=utf8                              \
                       -DDEFAULT_COLLATION=utf8_general_ci                 \
                       -DWITH_EXTRA_CHARSETS:STRING=all                    \
                       -DWITH_MYISAM_STORAGE_ENGINE=1                      \
                       -DWITH_INNOBASE_STORAGE_ENGINE=1                    \
                       -DWITH_MEMORY_STORAGE_ENGINE=1                      \
                       -DWITH_READLINE=1                                   \
                       -DENABLED_LOCAL_INFILE=1                            \
                       -DWITH_SSL=bundled                                  \
                       -DWITH_ZLIB=system                                  \
                       -Dwith-mysqld-ldflags=-all-static                   \
                       -Dwith-client-ldflags=-all-static                   \
                       -DMYSQL_DATADIR=${Mysql_data_dir}                   \
                       -DMYSQL_USER=mysql                                  \
                       -DMYSQL_TCP_PORT=${Mysql_port}                      \
                       -DWITH_BOOST=${Mysql_boost_dir}/src/mysql-${Mysql_version}/boost/
        if [ $? -eq 0 ];then
            if  [ $CPUS  -eq 0 ];then
                echo -e  "\e[1;33m  +++++++++++++++++++++    \e[0m"
                echo -e  "\e[1;33m   \  make  开始编译   \   \e[0m"
                echo -e  "\e[1;33m    +++++++++++++++++++++  \e[0m"    
                sleep 3
                make
                echo -e  "\e[1;33m  +++++++++++++++++++++++++    \e[0m"
                echo -e  "\e[1;33m   \make  install  开始编译\   \e[0m"
                echo -e  "\e[1;33m    ++++++++++++++++++++++++   \e[0m"
                sleep 3
                make  install
            else
                echo -e  "\e[1;33m  +++++++++++++++++++++    \e[0m"
                echo -e  "\e[1;33m   \  make  开始编译   \   \e[0m"
                echo -e  "\e[1;33m    +++++++++++++++++++++  \e[0m"    
                sleep 3
                make  -j  ${CPUS}
                echo -e  "\e[1;33m  +++++++++++++++++++++++++    \e[0m"
                echo -e  "\e[1;33m   \make  install  开始编译\   \e[0m"
                echo -e  "\e[1;33m    ++++++++++++++++++++++++   \e[0m"
                sleep 3
                make  install
            fi
            [ $? -ne 0 ] &&  exit 1
            chown -R   mysql.mysql ${Mysql_data_dir}
            cd ${Mysql_install_dir}
        else
            echo -e  "\n\e[1;33m  编译有错误,请重新检查,编译 !   \e[0m\n"
            exit 1
        fi
    else
        echo -e  "\n\e[1;33m 选择错误,请重新选择! \e[0m\n"
        exit 1 
    fi

    echo  -e "\n\e[1;33m 开始进行初始化数据库实例\e[0m\n"
    sleep 2
    ${Mysql_install_dir}/bin/mysqld  --initialize-insecure  --user=mysql  --basedir=${Mysql_install_dir}  --datadir=${Mysql_data_dir}
    [ $? -eq 0 ]  &&  echo  -e  "\n\e[1;33m== 初始化 successed ==\e[0m\n"  ||  echo  -e  "\n\e[1;33m== 初始化 failed ==\e[0m\n"    

    Config_MySQL_conf;

    echo -e "\e[1;33m 开始启动Mysql服务\e[0m"
    ${Mysql_install_dir}/bin/mysqld_safe --defaults-file=${Mysql_conf_dir}/my.cnf --user=mysql  &
    echo -e "\n"

    sleep 3
    ss -ntl | grep  $Mysql_port  > /dev/null  2>&1
    if  [ $?  -eq   0  ] ;then
        echo    
        echo -e "\e[1;33m       +------------------------------------------+ \e[0m"
        echo -e "\e[1;33m      /|                                         /| \e[0m"
        echo -e "\e[1;33m     +------------------------------------------+ | \e[0m"
        echo -e "\e[1;33m     | |                                        | | \e[0m"
        echo -e "\e[1;33m     | +-- Wecome to use MySQL${Mysql_version}------------|-+ \e[0m"
        echo -e "\e[1;33m     |/                                         |/  \e[0m"
        echo -e "\e[1;33m     +___________Successful_____________________+   \e[0m"
        echo -e "\e[1;33m    /                                          /    \e[0m"
        echo -e "\e[1;33m   /            Perfect                       /     \e[0m"   
        echo -e "\e[1;33m  /__________________________________________/      \e[0m"
    else
        echo -e "\e[1;35m    +---------------------------------+        \e[0m"
        echo -e "\e[1;35m    |\          Failed                |\       \e[0m"
        echo -e "\e[1;35m    | +-------------------------------|-+      \e[0m"                    
        echo -e "\e[1;35m    | |                               | |      \e[0m"
        echo -e "\e[1;35m    +-|---I'm Sorry,Please Checking---+-|      \e[0m"
        echo -e "\e[1;35m     \|                                \|      \e[0m"
        echo -e "\e[1;35m      +---------------------------------+      \e[0m"
        echo -e "\e[1;35m       \                                 \     \e[0m"       
        echo -e "\e[1;35m        \       请重新执行脚本            \    \e[0m"
        echo -e "\e[1;35m         \                                 \   \e[0m"
        echo -e "\e[1;35m          +---------------------------------+  \e[0m"   
        exit 1
    fi
}

Mysql_install(){
    echo -e "\n选择匹配数据库的安装版本   \n"
    echo -e "\t  1. Install 5.6.X:    \n"
    echo -e "\t  2. Install 5.7.X:    \n"
    echo -e "\t  3. Install 8.X.X:    \n"
    echo -e "\t  0. 退出本次循环:     \n"
    read -n 1 -p "Please Input  Numbers : "  DB_Version
    case $DB_Version in
        1)
            depend_pkg;
            All_path_1;
            All_path_2;
            mkdir -p $Work_dir;
            MySQL_5_6_X;
        ;;
        2)
            depend_pkg;
            All_path_1;
            All_path_2;
            mkdir -p $Work_dir;
            MySQL_5_7_X;
        ;;
        3)
            depend_pkg;
            All_path_1;
            All_path_2;
            mkdir -p $Work_dir;
            MySQL_8_X_X;
        ;;
        0)
            continue
        ;;
        *)
            clear
            echo  -e  "\nI'm   Sorry ! please input  numbers [1,2,3,0]\n"
        ;;
    esac
    echo -en "\t\t\t 任意键继续\n"
    read -n 1  line
}

#########################     Php 安装相关函数     ############################
Qdbm_install(){
    cd  ${Work_dir}
    [ -f  qdbm-1.8.78.tar.gz ]  ||  wget http://fallabs.com/qdbm/qdbm-1.8.78.tar.gz
    tar  xf   qdbm-1.8.78.tar.gz
    cd   qdbm-1.8.78
    ./configure 
    make  -j  ${CPUS}   &&   make   install 
}

#此libmcrypt安装主要是考虑到后面会用到mcrypt模块,提前先安装上,如果需要安装mcrypt模块的话直接在php源码目录里装即可
Libmcrypt_install(){
    cd  ${Work_dir}
    [ -f "libmcrypt-${Libmcrypt_version}.tar.gz" ] || wget https://jaist.dl.sourceforge.net/project/mcrypt/Libmcrypt/${Libmcrypt_version}/libmcrypt-${Libmcrypt_version}.tar.gz
    tar -zxvf libmcrypt-${Libmcrypt_version}.tar.gz
    cd  libmcrypt-${Libmcrypt_version}
    ./configure   
    make  -j $CPUS  &&  make   install
    cd libltdl
    ./configure --with-gmetad --enable-gexec --enable-ltdl-install
    make  -j $CPUS  &&  make install
}

Mhash_install(){
    cd    ${Work_dir}
    [ -f "mhash-${Mhash_version}.tar.gz" ]         || wget https://jaist.dl.sourceforge.net/project/mhash/mhash/${Mhash_version}/mhash-${Mhash_version}.tar.gz
    tar -zxvf mhash-${Mhash_version}.tar.gz
    cd mhash-${Mhash_version}
    ./configure
    make -j $CPUS  && make install
}

Libiconv_install(){
    cd  $Work_dir
    [ -f "libiconv-${Libiconv_version}.tar.gz" ]   || wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-${Libiconv_version}.tar.gz
    tar -zxvf libiconv-${Libiconv_version}.tar.gz
    cd libiconv-${Libiconv_version}
    ./configure
    make  -j $CPUS  &&  make install
}

Php_install(){
    cd ${Work_dir}
    [ -f "php-${Php_version}.tar.gz" ] || wget http://am1.php.net/distributions/php-${Php_version}.tar.gz
    tar  xvf php-${Php_version}.tar.gz 
    cd php-${Php_version}
    echo -e "\033[34m 开始安装编译  php . . . . . . \033[0m"
    sleep 3
    ./configure --prefix=${Php_install_dir} --with-mysql=mysqlnd  --with-mysqli=mysqlnd  --with-pdo-mysql=mysqlnd                   \
	--with-apxs2=${Web_install_dir}/bin/apxs   --with-config-file-path=${Php_install_dir}/etc --enable-pic  --disable-static       \
	--enable-mbstring --enable-inline-optimization   --with-regex=system --with-gettext --with-gd --with-png --with-zlib            \
	--enable-debugger --disable-debug  --enable-magic-quotes   --enable-safe-mode --enable-sysvmsg --enable-sysvsem --enable-sysvshm\
	--enable-track-vars --enable-yp --enable-ftp --with-xml  --with-freetype-dir=/usr --with-png-dir=/usr --enable-gd-native-ttf    \
	--with-ttf  --with-ncurses --with-gmp --with-iconv  --with-jpeg-dir=/usr  --with-expat-dir=/usr --enable-bcmath --enable-exif   \
	--enable-sockets --enable-discard-path  --enable-wddx  --enable-trans-sid --enable-wddx --enable-versioning --enable-calendar   \
	--enable-dbx  --enable-dio --enable-mcal --enable-shmop  --with-curl --with-openssl  --with-qdbm   --enable-fpm --enable-fastcgi\
	--enable-zip  --with-mcrypt --with-mhash --with-qdbm
    if [ $? -eq 0 ];then
        echo -e  "\e[1;33m  +++++++++++++++++++++    \e[0m"
        echo -e  "\e[1;33m   \  make  开始编译   \   \e[0m"
        echo -e  "\e[1;33m    +++++++++++++++++++++   \e[0m"    
        sleep 3 
        make -j  ${CPUS} ZEND_EXTRA_LIBS='-liconv'
        echo -e  "\e[1;33m  +++++++++++++++++++++++++    \e[0m"
        echo -e  "\e[1;33m   \make  install  开始编译\   \e[0m"
        echo -e  "\e[1;33m    ++++++++++++++++++++++++   \e[0m"
        sleep 3
        make install
        if  [ $? -eq 0 ];then
            echo  -e  "\033[34m PHP  Install  Successed ! \033[0m"
    	    cp    php.ini-production                 ${Php_config_dir}/php.ini
            sed   -i '/upload_max_filesize/s/2/50/'  ${Php_config_dir}/php.ini
            sed   -i '/post_max_size/s/8/50/'        ${Php_config_dir}/php.ini
            sed   -i "/expose_php/s/On/OFF/"         ${Php_config_dir}/php.ini
            sed   -i '$a ;date.timezone=PRC\ndate.timezone = Asia/Shanghai'                   ${Php_config_dir}/php.ini
            sed   -i '$a ;关闭高危函数应用\ndisable_functions = passthru,exec,system,chroot'  ${Php_config_dir}/php.ini
			
            echo  -e "\033[34m php.ini   config  complete !!!  \033[0m"
    	    mkdir -p  ${Web_root_dir}  ${Web_logs_dir}
            #lamp环境编译php时,主要进行的配置
            echo "AddType application/x-httpd-php .php"              >> ${Web_config_dir}/httpd.conf
            echo "AddType application/x-httpd-php-source .phps"      >> ${Web_config_dir}/httpd.conf
            echo -e "\033[34m php modules append to httpd.conf  complete!!! \033[0m"
			
            echo -e "\033[34m 重启动 apache 服务\033[0m"  
            ${Web_install_dir}/bin/apachectl     restart
            sleep 3
            #egrep  -i  "export PHPRC=${Php_config_dir}/php.ini " /etc/profile > /dev/null  2>&1
            #if  [ $? -ne 0 ] ;then
            #    echo   "export PHPRC=${Php_config_dir}/php.ini " >> /etc/profile
            #fi
            #source  /etc/profile
        else
    	    echo -e "\033[34m  PHP  Install  Failed ! \033[0m"
    	    exit 1
        fi
    else
        echo  -e "\033[34m  Congfigure  Install  Php   Abnormal \033[0m"
        exit 1	
    fi
}
###########################################################################################################################################################################
############################# 脚本主程序内容 ##################################
while [ 1 ]
do
    echo
    All_path_1
    echo -e "\t\t\tSys Admin Menu\n"
    echo -e "\t  1. Install Apache:   ${Apache_version}\n"
    echo -e "\t  2. Install Mysql:    Continue...... \n"
    echo -e "\t  3. Install Php:      ${Php_version}   \n"
    echo -e "\t  0. 退出脚本程序     \n"
    
    read -n 1 -p "Please Input  Numbers : " NUM  
    case $NUM in
        1)
        echo  -e "\033[34m  安装配置 Apache 环境 ............... \033[0m"
        sleep 2
        depend_pkg;
        All_path_1;
        All_path_2;
        mkdir -p $Work_dir;
        Apr_install;
        Apr_util_install;
        Apache_install;
        ;;
        2)
        echo  -e "\033[34m  安装配置 MySQL 环境 ............... \033[0m"
        sleep 2
        Mysql_install;
        ;;
        3)
        echo  -e "\033[34m  安装配置 Php 环境 ............... \033[0m"
        sleep 2
        depend_pkg;
        All_path_1;
        All_path_2;
        mkdir -p $Work_dir;
        Qdbm_install;
        Libmcrypt_install;
        Mhash_install;
        Libiconv_install;
        Php_install;
        ;;
        0)
        echo -e "\n"
        exit 1
        ;;
        *)
        clear
        echo  -e "\033[34m  Please  Input  [ 1 | 2 | 3 | 0 ]  \033[0m"
    esac
        echo -en "\t\t\t 任意键继续"
        read -n 1 line
done

  

posted @ 2018-03-01 10:00  淡淡定定  阅读(254)  评论(0编辑  收藏  举报