lnmp环境部署脚本-y

#!/bin/bash
#date:2018-01-01 
#author: yangjw
#新版的MySQL安装需要高版本2.8以上cmake                ===>  wget  https://cmake.org/files/v3.10/cmake-3.10.2.tar.gz
#新版的MySQL安装有可能需要安装   ncurses-6.0.tar.gz   ===>  wget ftp://ftp.gnu.org/gnu/ncurses/ncurses-6.0.tar.gz
#新版的MySQL安装有可能需要安装   bison-3.0.4.tar.gz   ===>  wget http://ftp.gnu.org/gnu/bison/bison-3.0.4.tar.gz
#
#注:1. 安装5.7版本数据库的时候,需要注意一下配置,内存4G以上; 
#    2. cmake 版本必须是2.8以上; 
#    3. 卸载掉自带的 boost*
#########################  function   配置区域  ################################
##########################  环境统一安装包  ####################################
depend_pkg (){
    yum install -y gcc gcc-c++ make cmake autoconf automake ncurses-devel    \
        libxml2-devel  perl-devel libcurl-devel libgcrypt  libgcrypt-devel   \
        libxslt  libxslt-devel pcre-devel openssl-devel  openssl  wget       \
        curl-devel db4-devel  libXpm-devel gmp-*  libc-client-devel          \
        openldap-devel freetype freetype-devel gd libjpeg-*  libpng-*        \
        zlib zlib-devel bzip2-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 "eth0" | 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          
    Nginx_version=1.10.3            # Nginx 版本号,如果没有限制,可以默认使用 1.10.3 版本
    Mysql_version=5.6.17            # Mysql 版本号 ( For  MySQL 5.[5-7] ,如没有要求,推荐默认安装5.6.17版本。)
    Mysql_port=3306                 # Mysql 监听的端口号 
    Php_version=7.0.22              # Php 版本号 ,如果没有限制,可以默认使用 5.6.19 版本,支持安装[5.X | 7.X]
    Mhash_version=0.9.9.9           # Mhash 版本号,默认可以选择0.9.9.9,也可以选择去其他版本
    Libiconv_version=1.13           # Libiconv 版本号,默认可以选择1.13,也可以选择其他版本
    Libmcrypt_version=2.5.8         # Libmcrypt 版本,默认可以选择2.5.8,也可以选择其他版本  
    soft_sign="PXJY_WebSer"         # 隐藏nginx名字和版本后,显示的标志名字
}

All_path_2(){
    Work_dir=/app/src                                      # 所有软件包的存放目录   
    App_dir=/app                                           # 网站顶层目录,可随意修改名字

    Web_install_dir=${App_dir}/nginx${Nginx_version}       # Nginx 安装目录的路径
    Web_config_dir=${Web_install_dir}/conf                 # Nginx 主配置文件的路径
    Web_root_dir=${App_dir}/webroot/project                # 配置网站项目根目录的路径
    Web_log_dir=${App_dir}/applog                          # 配置网站存放日志的路径

    Mysql_boost_dir=${App_dir}/mysql${Mysql_version}       # 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 server id 的值(此处不能有空格 -->  “=1” )

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

#########################     Nginx 安装相关函数   #############################

#隐藏nginx版本函数
Hidden_version(){
#修改文件-1 nginx.h
cat >  ${Work_dir}/nginx-${Nginx_version}/src/core/nginx.h  << EOF
/*
 * Copyright (C) Igor Sysoev
 * Copyright (C) Nginx, Inc.
 */

#ifndef _NGINX_H_INCLUDED_
#define _NGINX_H_INCLUDED_

#define nginx_version      1000001
#define NGINX_VERSION      "${Nginx_version}"
#define NGINX_VER          "${soft_sign}"

#ifdef NGX_BUILD
#define NGINX_VER_BUILD    NGINX_VER " (" NGX_BUILD ")"
#else
#define NGINX_VER_BUILD    NGINX_VER
#endif

#define NGINX_VAR          "${soft_sign}"
#define NGX_OLDPID_EXT     ".oldbin"

#endif /* _NGINX_H_INCLUDED_ */
EOF

sed -i "/static u_char ngx_http_server_string/s/nginx/${soft_sign}/"  ${Work_dir}/nginx-${Nginx_version}/src/http/ngx_http_header_filter_module.c
sed -i "/<hr><center>nginx<\/center>/s/nginx/${soft_sign}/"           ${Work_dir}/nginx-${Nginx_version}/src/http/ngx_http_special_response.c

}

Nginx_install(){
    ####################  创建网站和日志目录,配置文件目录  #############
    mkdir -p  ${Web_root_dir}
    mkdir -p  ${Web_log_dir}
    mkdir -p  ${Web_install_dir}/conf/conf.d/
    sleep 1
    cd ${Work_dir}
    [ -f "nginx-${Nginx_version}.tar.gz" ]    ||  wget  http://nginx.org/download/nginx-${Nginx_version}.tar.gz
    tar  zxf    nginx-${Nginx_version}.tar.gz 
    if [ $? -eq 0 ];then
        cd nginx-${Nginx_version}
        echo -e  "\n\e[1;33m-- 开始编译 Nginx 服务. . . . .  --\e[0m\n"
        echo -e  "\n\e[1;33m-- 剩余3s,也可以Ctrl+c,停止安装 --\e[0m\n"
        echo 
        Hidden_version;
        sleep 3
        useradd   -s  /sbin/nologin    nginx
        ./configure  --prefix=${Web_install_dir} --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre 
        make  -j ${CPUS}  && make install
        [ $? -eq 0 ]  &&  echo -e "\033[34m Nginx install  successed \033[0m" ||  echo -e "\033[34m Nginx install failed \033[0m" 
        sleep  3
    else
        echo  -e "\e[1;33m -- Nginx  Install  failed. -- \e[0m"
        exit 1
    fi
    ######################## start 配置nginx.conf主配置文件和 虚拟主机##########
    if [ $? -eq 0 ];then
    cp   ${Web_config_dir}/nginx.conf  ${Web_config_dir}/nginx.conf.BAK
    cat << EOF  >  ${Web_config_dir}/nginx.conf
user  nginx;
worker_processes 2;
worker_cpu_affinity  01  10;
error_log  ${Web_install_dir}/logs/error.log error;
pid        ${Web_install_dir}/logs/nginx.pid;
worker_rlimit_nofile  65535;
events {
    use epoll;
    worker_connections  2048;
}

http {
    include       ${Web_install_dir}/conf/mime.types;
    default_type  application/octet-stream;
    access_log    ${Web_install_dir}/logs/access.log  ;
    log_format  main  '\$remote_addr - \$remote_user [\$time_local] "\$request" '
                      '\$status \$body_bytes_sent "\$http_referer" '
                      '"\$http_user_agent" "\$http_x_forwarded_for"';
    log_format  combinedio  '\$remote_addr - \$remote_user [\$time_local] '
                            '"\$request" \$status $body_bytes_sent '
                            '"\$http_referer" "\$http_user_agent" \$request_length \$request_time \$upstream_response_time \$server_addr';
    sendfile        on;
    tcp_nopush      on;
    tcp_nodelay     on;
    server_tokens   off;
    keepalive_timeout  65;
    #gzip  on;
    gzip             on;
    gzip_min_length  1000;
    gzip_proxied     expired no-cache no-store private auth;
    gzip_buffers     4 8k;
    gzip_types       text/plain application/x-javascript text/css application/xml text/javascript;
    gzip_disable     "MSIE [1-6]\.";
    
    #proxy_buffer_size 64k;
    #proxy_buffers 4 256k;
    #proxy_busy_buffers_size 256k;
    #client_header_buffer_size 64k;
    #large_client_header_buffers 4 64k;
 
	fastcgi_read_timeout 600;
    proxy_connect_timeout     600;
    proxy_read_timeout        600;
    proxy_send_timeout        600;
    client_body_buffer_size   20M;
    client_max_body_size      20M;      #设置允许客户端请求的最大的单个文件字节数
    client_header_buffer_size 20M;      #指定来自客户端请求头的headebuffer大小
    include  conf.d/*.conf;
}
EOF
    ######################## 配置虚拟主机头 ###########
    cat  << EOF > ${Web_install_dir}/conf/conf.d/project.conf
server {
	listen       80;
	server_name  ${Server_ip};
	root         ${Web_root_dir};
	index index.php index.html;
	access_log   ${Web_log_dir}/project.log  main;
	error_log    ${Web_log_dir}/project.error.log  error;
	location / {
	    index  index.php;
	    if (!-e \$request_filename){
		    rewrite ^/(.*)\$ /index.php?s=\$1;
		}
	}
	error_page  404              /404.html;
	location = /40x.html {
	}
	error_page   500 502 503 504  /50x.html;
	location = /50x.html {
	}
	#限制 该目录下执行解析php文件
	#    location ~* ^/public/static/.*\.(php|php5)$ {
	#        deny all;
	#    }
	
	location ~ .*\.php {
	    if ($request_filename ~* (.*)\.php) {
            set $php_url $1;
        }
        if (!-e $php_url.php) {
            return 404;
        }
		fastcgi_pass   127.0.0.1:9000;
		fastcgi_index  index.php;
		fastcgi_split_path_info  ^(.+\.php)(.*)\$;
        fastcgi_param  PATH_INFO               \$fastcgi_path_info;
        fastcgi_param  PATH_TRANSLATED \$document_root\$fastcgi_path_info;
		fastcgi_param  SCRIPT_FILENAME  \$document_root\$fastcgi_script_name;
		fastcgi_param  PHP_VALUE  "open_basedir=${Web_root_dir}:/tmp/";
		#include        fastcgi_params;
		include         fastcgi.conf;
	}
}
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
    cat <<  EOF > ${Web_root_dir}/index.html
<center><h1>Test  ${Server_ip}  Nginx  ${Nginx_version}</h1></center>
EOF
    echo -e "\033[34m 正在启动Nginx Service \033[0m"
    sleep 1
    ${Web_install_dir}/sbin/nginx  
    [ $? -eq 0 ] &&  echo -e "\033[34m  Nginx Service  OK \033[0m"  || echo -e "\033[34m Nginx Service  down  \033[0m"
    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
innodb_data_file_path = ibdata1:2G:autoextend
innodb_log_file_size = 512M

# 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 =~  ^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-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/

        [ -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}/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 安装相关函数     ############################# 
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  --prefix=/usr/local
    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 --prefix=/usr/local
    make  -j $CPUS  &&  make install
}

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 --prefix=/usr/local
    make  -j $CPUS  &&  make   install
    cd libltdl
    ./configure --with-gmetad --enable-gexec --enable-ltdl-install
    make  -j $CPUS  &&  make install
    ln -sv /usr/local/lib/libmcrypt* /usr/lib/
    ln -sv /usr/local/lib/libmhash.* /usr/lib/
    ln -s /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config
    ldconfig
}

Php_install(){
    cd    $Work_dir
    [ -f "php-${Php_version}.tar.gz" ]         || wget http://am1.php.net/distributions/php-${Php_version}.tar.gz
    tar zxvf php-${Php_version}.tar.gz 
    cd php-${Php_version}
    echo -e "\033[34m 开始安装编译  php . . . . . . \033[0m"
    sleep 3
    echo -e "\033[34m 开始安装编译 configure  php . . . . . . \033[0m"
    make clean 
    ./configure --prefix=${Php_install_dir}  --with-mysql=mysqlnd  --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd  --enable-sysvsem --enable-inline-optimization    \
                --with-openssl  --with-config-file-path=${Php_config_dir} --with-config-file-scan-dir=${Php_config_dir}/php.d  --enable-bcmath  --without-pear  \
                --with-gd   --with-jpeg-dir   --with-freetype-dir --with-zlib   --enable-xml --with-libxml-dir  --with-mhash  --enable-fpm --enable-mbregex         \
                --with-mcrypt  --with-curl   --with-png-dir  --enable-shmop --enable-mbstring  --enable-zip --enable-json   --enable-phar  --with-iconv-dir         \
                --enable-gd-native-ttf --enable-pcntl --enable-sockets 
        grep  "/usr/local/lib"  /etc/ld.so.conf  > /dev/null 2>&1
    if  [ $? -ne 0 ] ;then
        echo "/usr/local/lib" >>  /etc/ld.so.conf
        ldconfig
    fi
    sleep 2
    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'
    if [ $? -ne 0 ];then
        make  clean 
        make -j ${CPUS}  ZEND_EXTRA_LIBS='-liconv'
    fi 
    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"
        sleep 2
        echo -e "\033[34m  开始配置 php-fpm.conf  \033[0m"
        sleep 2 
        cat > ${Php_config_dir}/php-fpm.conf  << EOF
[global]
pid = ${Php_install_dir}/php-fpm.pid
log_level = error
error_log = ${Php_install_dir}/php-fpm.log
daemonize = yes
[www]
user = nginx
group = nginx
;listen.owner = nginx      ##此用户需与nginx运行用户对应,启用unix socket设置选项时会用到,如果使用tcp方式访问,这里可以注释
;listen.group = nginx      ##此用户需与nginx运行用户对应,启用unix socket设置选项时会用到,如果使用tcp方式访问,这里可以注释
listen.mode = 0660
listen = 127.0.0.1:9000;  ##启动php-fpm.sock,对应的nginx,也要修改配置,/var/run/php-fpm/php-fpm.sock;,sock连接稍快些
pm = dynamic
pm.max_children = 200
pm.start_servers = 20
pm.min_spare_servers = 20
pm.max_spare_servers = 50
php_admin_value[memory_limit] = 64M
listen.backlog = 2048
pm.max_requests = 1024
php_admin_flag[expose_php] = off        
EOF
 
        mkdir -p         ${Web_root_dir}
        echo "<?php phpinfo();?>" > ${Web_root_dir}/index.php
        #grep    "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
        echo   -e  "\033[34m  开始启动 php service   \033[0m "
        sleep 2
        ${Php_install_dir}/sbin/php-fpm  -y  ${Php_config_dir}/php-fpm.conf
        [ $? -eq 0 ]  &&  echo  -e "\033[34m  Php  Service  Start  Successed  \033[0m"  || echo  -e "\033[34m  Php  Service  Start  Failed  \033[0m"
        ss  -ntl | grep   9000 > /dev/null 2>&1
        [ $? -eq 0 ] && echo  -e  "\033[34m  PHP 服务已经正常  \033[0m "
    else
        echo -e "\033[34m  Php  Installed  Failed  \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 Nginx:    ${Nginx_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  安装配置 Nginx 环境 ............... \033[0m"
        sleep 2
        depend_pkg;
        All_path_1;
        All_path_2;
        mkdir  -p $Work_dir;
        Nginx_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;
        Mhash_install;
        Libiconv_install;
        Libmcrypt_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 "\n\n\t\t\t 任意键继续"
    read -n 1 line
done
posted @ 2018-03-01 12:06  淡淡定定  阅读(237)  评论(0编辑  收藏  举报