这个脚本是使用shell编写,为了快速在生产环境上部署lnmp。

  环境为:CentOS7.2,php-7.0.20,nginx-1.10.3,mysql-5.7.18。

#!/bin/sh
#author: vim
#qq: 82996821
#OS: Centos7.2
#filename: auto_install_lnmp.sh

#init variable
PHP_VERSION='7.0.20'    #PHP版本号
NGINX_VERSION='1.10.3'    #NGINX版本号

TOOL_DIR=`pwd`
APP_INSTALL_DIR='/usr/local'    #服务安装路径
DATA_DIR='/data'    #数据目录

#source function library
. /etc/init.d/functions


installMySQL(){
    echo "----- install mysql -----"
    cd ${TOOL_DIR}
    [ ! -f mysql57-community-release-el7-8.noarch.rpm  ] && \
    wget https://repo.mysql.com/mysql57-community-release-el7-8.noarch.rpm
    rpm -ivh mysql57-community-release-el7-8.noarch.rpm
    yum -y install mysql-server
    [ $? -eq 0  ]&&action $"install mysql:" /bin/true||action $"install mysql:" /bin/false

cat > /etc/my.cnf <<EOF
[mysqld]
#开启binlog日志
server-id=1
log-bin=mysql-bin
#设置端口
port=$MYSQL_PORT
#数据文件目录
datadir=$DATA_DIR/mysql
socket=$DATA_DIR/mysql/mysql.sock

symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld.pid

#开启慢查询日志
slow-query-log=On
slow_query_log_file=/var/log/mysql_slow_query.log
long_query_time=2
log_queries_not_using_indexes=ON

#控制error log、genera log,等等记录日志的显示时间参数
log_timestamps=SYSTEM
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci
sql_mode='NO_ENGINE_SUBSTITUTION'

[client]
socket=$DATA_DIR/mysql/mysql.sock
EOF

    systemctl start mysqld
    [ $? -eq 0  ]&&action $"start mysql:" /bin/true||action $"start mysql:" /bin/false
    systemctl enable mysqld
}

installMakeMySQL(){
    cd ${TOOL_DIR}
    yum -y install gcc-c++ ncurses-devel cmake make perl gcc autoconf automake zlib libxml libgcrypt libtool bison
    useradd -r -s /sbin/nologin mysql
    [ ! -f mysql-boost-5.7.18.tar.gz  ] && \
    wget https://cdn.mysql.com/Downloads/MySQL-5.7/mysql-boost-5.7.18.tar.gz
    tar zxf mysql-boost-5.7.18.tar.gz
    cd mysql-5.7.18
    cmake -DCMAKE_INSTALL_PREFIX=${APP_INSTALL_DIR}/mysql-5.17.18 -DMYSQL_DATADIR=${DATA_DIR}/mysql -DMYSQL_UNIX_ADDR=${DATA_DIR}/mysql/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_BOOST=./boost

    make && make install
    [ $? -eq 0  ]&&action $"install make mysql:" /bin/true||action $"install make nginx:" /bin/false

    ln -s ${APP_INSTALL_DIR}/mysql-5.17.18/ /usr/local/mysql

cat > /etc/my.cnf <<EOF
[mysqld]
server-id=1
log-bin=mysql-bin
port=$MYSQL_PORT
datadir=$DATA_DIR/mysql
socket=$DATA_DIR/mysql/mysql.sock

symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

slow-query-log=On
slow_query_log_file=/var/log/mysql_slow_query.log
long_query_time=2
log_queries_not_using_indexes=ON

log_timestamps=SYSTEM
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci
sql_mode='NO_ENGINE_SUBSTITUTION'

[client]
socket=$DATA_DIR/mysql/mysql.sock
EOF
    mkdri ${DATA_DIR}/mysql -p
    chown mysql.mysql /data/mysql/
    ${APP_INSTALL_DIR}/mysql/bin/mysqld --defaults-file=/etc/my.cnf --initialize --user=mysql
    ${APP_INSTALL_DIR}/mysql/support-files/mysql.server start
    [ $? -eq 0  ]&&action $"start mysql:" /bin/true||action $"start mysql:" /bin/false
    echo "${APP_INSTALL_DIR}/mysql/support-files/mysql.server start" >> /etc/rc.local
    systemctl enable mysqld
}

installNginx(){
    echo "----- step : install nginx-----"
    yum -y install pcrepcre-devel openssl openssl-devel zlib zlib-devel
    #create nginx user
    useradd www -s /sbin/nologin -M

    cd ${TOOL_DIR}
    [ ! -f nginx-${NGINX_VERSION}.tar.gz  ] && \
    wget http://nginx.org/download/nginx-${NGINX_VERSION}.tar.gz
    tar xf nginx-${NGINX_VERSION}.tar.gz
    cd nginx-${NGINX_VERSION}

    ./configure --user=www --group=www --prefix=${APP_INSTALL_DIR}/nginx-${NGINX_VERSION} --with-http_stub_status_module --with-http_ssl_module
    make && make install
    [ $? -eq 0  ]&&action $"install nginx:" /bin/true||action $"install nginx:" /bin/false
    
    ln -s ${APP_INSTALL_DIR}/nginx-${NGINX_VERSION}/ ${APP_INSTALL_DIR}/nginx


    ${APP_INSTALL_DIR}/nginx/sbin/nginx
    [ $? -eq 0  ]&&action $"start nginx:" /bin/true||action $"start nginx:" /bin/false
    echo "${APP_INSTALL_DIR}/nginx/sbin/nginx"  >> /etc/rc.local
    
}

installPHP(){
    echo "----- step : install php -----"
    yum -y install libxml2-devel libxml2 libxml2-devel curl curl-devel  libjpeg-devel libpng-devel freetype-devel libxslt-devel
    cd ${TOOL_DIR}
    [ ! -f php-${PHP_VERSION}.tar.gz ] && \
    wget http://cn2.php.net/distributions/php-${PHP_VERSION}.tar.gz
    tar xf php-${PHP_VERSION}.tar.gz
    cd php-${PHP_VERSION}
    ./configure  \
    --prefix=${APP_INSTALL_DIR}/php-${PHP_VERSION} \
    --with-config-file-path=${APP_INSTALL_DIR}/php-${PHP_VERSION}/etc \
    --enable-mbstring \
    --enable-sockets \
    --enable-fpm \
    --with-mysqli \
    --enable-mysqlnd \
    --with-pdo-mysql=mysqlnd \
    --with-freetype-dir \
    --with-jpeg-dir \
    --with-png-dir \
    --with-zlib \
    --enable-xml \
    --disable-rpath \
    --enable-inline-optimization \
    --with-curl \
    --enable-mbregex \
    --with-gd \
    --enable-gd-native-ttf \
    --with-openssl \
    --enable-zip \
    --enable-short-tags \
    --enable-static \
    --with-fpm-user=www \
    --with-fpm-group=www

    make && make install
    [ $? -eq 0  ]&&action $"install php:" /bin/true||action $"install php:" /bin/false

    ln -s ${APP_INSTALL_DIR}/php-${PHP_VERSION} ${APP_INSTALL_DIR}/php

    echo "----- step : config php -----"
    cd ${TOOL_DIR}/php-${PHP_VERSION}
    cp php.ini-development ${APP_INSTALL_DIR}/php/etc/php.ini
    cd ${APP_INSTALL_DIR}/php/etc
    cp php-fpm.conf.default php-fpm.conf
    cd ${APP_INSTALL_DIR}/php/etc/php-fpm.d/
    cp www.conf.default www.conf

    #start php-fpm
    ${APP_INSTALL_DIR}/php/sbin/php-fpm -c ${APP_INSTALL_DIR}/php/etc/php.ini
    [ $? -eq 0  ]&&action $"start php:" /bin/true||action $"start php:" /bin/false
    echo "${APP_INSTALL_DIR}/php/sbin/php-fpm -c ${APP_INSTALL_DIR}/php/etc/php.ini"  >> /etc/rc.local
}


initAllEnv(){
    echo "----- step : add env -----"
    echo "export PATH=$PATH:$HOME/bin:${APP_INSTALL_DIR}/php/bin:${APP_INSTALL_DIR}/nginx/sbin/:/usr/local/mysql/bin/" >>/etc/profile
    source /etc/profile
    [ $? -eq 0  ]&&action $"initAllEnv:" /bin/true||action $"initAllEnv:" /bin/false
    echo "PATH: $PATH"
}

    AStr="Yum install MySQL"
    BStr="Make install MySQL"
    CStr="Make Install Nginx"
    DStr="Make install PHP"
    EStr="InitAll Environment"
    FStr="One key auto make install"

    
while true
do    
    echo "###############################################################"
    echo "<========================@system init@========================>"
    echo "A --${AStr}"
    echo "B --${BStr}"
    echo "C --${CStr}"
    echo "D --${DStr}"
    echo "E --${EStr}"
    echo "F --${FStr}"

    echo "------------------------------------------------"
    echo "Note: after 60s will exit"

    read -n1 -t60 -p "Choose one of A-B-C-D-E-:::" option
    option=${option:-0}

    flag0=$(echo $option|egrep "0"|wc -l)
    flag1=$(echo $option|egrep "q|Q"|wc -l)
    flag2=$(echo $option|egrep "[A-Fa-f]"|wc -l)


    if [ $flag0 -eq 1  ]; then
         exit 1
    elif [ $flag1 -eq 1 ]; then
         exit
    elif [ $flag2 -ne 1  ]; then
         echo "Pls input A--->F letter"
    fi

    echo -e "\nyour choose is: $option\n"
    echo "after 5s start install......"
    sleep 5
    case $option in
         A|a)
              installMySQL
         ;;
         B|b)
              installMakeMySQL
         ;;
         C|c)
              installNginx
         ;;
         D|d)
              installPHP
         ;;
         E|e)
              initAllEnv
         ;;
         f|F)
              installMakeMySQL
              installNginx
              configNginx
              installPHP
              initAllEnv
         ;;
         *)
         echo "Please input A-F,thank you!"
         #exit
         ;;
    esac
done