前序

本脚本应对一些机器所需环境的一次性部署, 部署前需要准备离线安装包放在命名为Packages的文件夹中。

shell

#/usr/bin/env bash
currentDIR=$(cd `dirname $0`; pwd)
installDIR=/usr/local

#JDK
function JDK(){
    j=`whereis java`
    java=$(echo ${j} | grep "jdk")
    if [[ "$java" != "" ]]
    then
        echo;
        echo "JDK已安装"
    else
        echo;
        echo "正在部署JDK"
        cd Packages
        tar -zxvf jdk-*.tar.gz -C ${installDIR} >/dev/null 2>&1
        echo;
        cd ${installDIR}/jdk* && jdkname=`pwd | awk -F '/' '{print $NF}'`
        echo;
        sed -i '$a\export JAVA_HOME='${installDIR}'/'${jdkname}'\nexport JRE_HOME=${JAVA_HOME}/jre\nexport CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib\nexport PATH=${JAVA_HOME}/bin:$PATH' /etc/profile
        echo "source /etc/profile"
    fi
}
#REDIS
function REDIS(){
    echo "正在部署REDIS"
    mkdir -p ${installDIR}/redis/{logs,nodes,conf,bin}
    cd ${currentDIR}/Packages
    tar -zxvf redis*.tar.gz >/dev/null 2>&1
    cd redis*/src && make >/dev/null 2>&1
    if [[ $? -ne 0 ]]; then
        echo "编译出错"
    else
        cp redis-cli redis-server ${installDIR}/redis/bin
        cp redis-trib.rb ${installDIR}/redis
        cp ../redis.conf ${installDIR}/redis/conf
        cd ${installDIR}/redis/conf
        #后台
        sed -i 's/daemonize no/daemonize yes/' redis.conf
        #日志
        sed -i 's/logfile ""/logfile "\/usr\/local\/redis\/logs\/redis.logs"/' redis.conf
        #启动
        cd ../
        echo "./bin/redis-server conf/redis.conf" > start.sh
        chmod +x start.sh
    fi
}
#MYSQL
function MYSQL(){
    echo "正在部署MYSQL"
    cd ${currentDIR}/Packages
    tar -zxvf mysql*.tar.gz -C ${installDIR} >/dev/null 2>&1
    cd ${installDIR} && mv mysql* mysql
    mkdir -p /data/mysql && mkdir ${installDIR}/mysql/logs
    touch ${installDIR}/mysql/logs/mysql.logs && touch ${installDIR}/mysql/logs/initialize.logs
    touch ${installDIR}/mysql/logs/start.logs
    useradd mysql -s /usr/sbin/nologin
    configfile="/etc/my.cnf"
    NdataDIR="/data/mysql"
    OdataDIR=`cat /etc/my.cnf | grep datadir | awk -F '=' '{print$2}'`
    Nsocket="/tmp/mysql.sock"
    Osocket=`cat /etc/my.cnf | grep socket | awk -F '=' '{print$2}'`
    Nlogerror="${installDIR}/mysql/logs/mysql.logs"
    Ologerror=`cat /etc/my.cnf | grep log-error | awk -F '=' '{print$2}'`
    Npidfile="${installDIR}/mysql/mysql.pid"
    Opidfile=`cat /etc/my.cnf | grep pid-file | awk -F '=' '{print$2}'`

    sed -i "s|${OdataDIR}|${NdataDIR}|g" ${configfile}
    sed -i "s|${Ologerror}|${Nlogerror}|g" ${configfile}
    sed -i "s|${Opidfile}|${Npidfile}|g" ${configfile}
    sed -i "s|${Osocket}|${Nsocket}|g" ${configfile}
    chown -R mysql:mysql /etc/my.cnf
    chown -R mysql:mysql ${installDIR}/mysql
    chown -R mysql:mysql /data/
    echo "初始化"
    ${installDIR}/mysql/bin/mysqld --initialize --user=mysql --basedir=${installDIR}/mysql --datadir=/data/mysql > ${installDIR}/mysql/logs/initialize.logs 2>&1
    echo "#!/bin/bash" > ${installDIR}/mysql/start.sh
    echo "./bin/mysqld_safe --user=mysql --basedir=${installDIR}/mysql --datadir=/data/mysql > logs/start.logs 2>&1 &" >> ${installDIR}/mysql/start.sh
    chmod +x ${installDIR}/mysql/start.sh
    echo "启动"
    cd ${installDIR}/mysql && ./start.sh
    echo "修改新的密码"
    ln -fs /usr/local/mysql/bin/mysql /usr/bin/mysql
    password1=`cat ${installDIR}/mysql/logs/initialize.logs | grep password | awk -F ' ' '{print$NF}'`
    #echo "旧的密码:${password1}"
    password2="123456"
    #炮灰牺牲品
    mysql --protocol=TCP -u root -p''${password1}'' --connect-expired-password -e "flush PRIVILEGES;" >/dev/null 2>&1
    /usr/bin/sleep 5
    #主力
    mysql --protocol=TCP -u root -p''${password1}'' --connect-expired-password -e "alter user 'root'@'localhost' identified by '${password2}';" >/dev/null 2>&1
    echo "新的密码:${password2}"
    echo "部署完成!"
}
function PHP(){
    echo "正在部署PHP"
    cd Packages
    wget https://mirrors.ustc.edu.cn/centos/7.5.1804/extras/x86_64/Packages/epel-release-7-11.noarch.rpm
    rpm -ivh epel-release*.rpm
    echo;
    echo "部署需要时间,请耐心稍等"
    mkdir -p ${installDIR}/php/logs && touch ${installDIR}/php/logs/{yum.logs,configure.logs,make.logs}
    tar -zxvf php*.tar.gz >/dev/null 2>&1
    cd php*
    #依赖
    yum install -y gcc gcc-c++ libmcrypt-devel mcrypt mhash gd-devel ncurses-devel libxml2-devel bzip2-devel libcurl-devel curl-devel libjpeg-devel libpng-devel freetype-devel net-snmp-devel openssl-deve python-devel zlib-devel freetype libxslt* bison autoconf re2c >${installDIR}/php/logs/yum.logs 2>&1
    #编译
    ./configure --prefix=/usr/local/php --exec-prefix=/usr/local/php --bindir=/usr/local/php/bin --sbindir=/usr/local/php/sbin --includedir=/usr/local/php/include --libdir=/usr/local/php/lib/php --mandir=/usr/local/php/php/man --with-config-file-path=/usr/local/php/etc --with-mysql-sock=/var/run/mysql/mysql.sock --with-mhash --with-openssl --with-mysqli=shared,mysqlnd --with-pdo-mysql=shared,mysqlnd --with-gd --with-iconv --with-zlib --enable-zip --enable-inline-optimization --disable-debug --disable-rpath --enable-shared --enable-xml --enable-bcmath --enable-shmop --enable-sysvsem --enable-mbregex --enable-mbstring --enable-ftp --enable-pcntl --enable-calendar --enable-exif --enable-sockets --with-xmlrpc --with-libxml-dir --enable-soap --without-pear --with-gettext --enable-session --with-curl --with-jpeg-dir --with-png-dir --with-freetype-dir --with-bz2 --enable-opcache --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --without-gdbm --enable-fast-install --disable-fileinfo > ${installDIR}/php/logs/configure.logs 2>&1
    #安装
    make && make install > ${installDIR}/php/logs/make.logs 2>&1
    #配置
    cp php.ini-production ${installDIR}/php/etc/php.ini
    echo;
    M=`cat ${installDIR}/php/etc/php.ini | grep date.timezone | tail -n 1`
    datatime="date.timezone = Asia/shanghai"
    sed -i "s|${M}|${datatime}|g" ${installDIR}/php/etc/php.ini
    echo "zend_extension=opcache.so" >> ${installDIR}/php/etc/php.ini
    cp ${installDIR}/php/etc/php-fpm.conf.default ${installDIR}/php/etc/php-fpm.conf
    cp ${installDIR}/php/etc/php-fpm.d/www.conf.default ${installDIR}/php/etc/php-fpm.d/www.conf
    cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
    chmod +x /etc/rc.d/init.d/php-fpm
    #慎用(仅限centos&redhat)
    chkconfig --add php-fpm
    useradd nginx -s /usr/sbin/nologin
    echo "部署完成"
    echo;
    echo "启动PHP"
    service php-fpm start
}
function OPENRESTY(){
    echo "正在部署OPENRESTY"
    cd Packages
    yum install -y gcc gcc-c++ zlib-devel pcre-devel openssl-devel readline-devel > yum.logs 2>&1
    useradd www -s /usr/sbin/nologin
    mkdir /usr/local/openresty
    tar -zxvf openresty*.tar.gz >/dev/null 2>&1
    cd openresty*
    #编译
    ./configure --prefix=/usr/local/openresty --user=www --group=www --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-http_realip_module > configure.logs 2>&1
    gmake && gmake install >gmake.logs 2>&1
    echo "部署完成"
}

function main(){
    JDK
    REDIS
    MYSQL
    PHP
    OPENRESTY
}
main
文档更新时间: 2019-03-08 09:46   作者:杭州-雪翊寒|深圳-夏天