31.第25章 企业级高性能WEB服务Nginx

一.系统I/O模型:

1.同步/异步:

关注的是事件处理的消息通信机制,即在等待⼀件事情的处理结果时,被调⽤者是否提供完成通知。
同步:synchronous,调⽤者等待被调⽤者返回消息后才能继续执⾏,如果被调⽤者不提供消息返回则为同步,同步需要调⽤者主动询问事情是否处理完成。
异步:asynchronous,被调⽤者通过事件状态、通知或回调机制主动通知调⽤者,即异步会主动返回被调⽤者的状态给调⽤者。

同步:进程发出请求调⽤后,内核不提供通知机制,即⽂件IO处理完成后不通知进程,需要进程⾃⼰去问内
核是否处理完成。
异步:进程发出请求调⽤后,内核会在调⽤处理完成后返回调⽤结果给进程,Nginx是异步的。

2.阻塞/⾮阻塞:

关注调⽤者在等待结果返回之前所处的状态

阻塞:blocking,指IO操作需要彻底完成后才返回到⽤户空间,调⽤结果返回之前,调⽤者被挂起,⼲不
了别的事情。
⾮阻塞:nonblocking,指IO操作被调⽤后⽴即返回给⽤户⼀个状态值,⽆需等到IO操作彻底完成,最终
的调⽤结果返回之前,调⽤者不会被挂起,可以去做别的事情。

3.系统IO模型组合:

以我去吃饭为例:我点了10个包⼦
同步与异步:
我点包⼦之后厨师是否告诉我:
同步:厨师做好包⼦后会放到指定位置,但是做好包⼦之前需要⾃⼰⼀次次去看包⼦做好没有,厨师不
会在包⼦做好之后通知我。
异步:厨师做好包⼦后告诉我包⼦做好放哪了。
阻塞与⾮阻塞::
我点包⼦后的状态:
阻塞:在厨师做包⼦期间⼀直在包⼦盘⼦前⾯等着,不能⼲别的事情。
⾮阻塞:点完包⼦就可以去⼲别的事情,⽐如去逛逛街或者买买买。
IO模型组合:
同步阻塞:我点完包⼦后不能去做别的事情,⽽且不知道包⼦有没有做好,需要⾃⼰⼀直等着并⼀次次的
问厨师做好没有。
同步⾮阻塞:点完包⼦后可以去做别的事情,但是不能⻓时间做别的事情,因为我还是不知道包⼦有没有
做好,也要⾃⼰⼀直等着并⼀次次的问厨师做好没有,只能抽空做点别的。
异步阻塞:我点完包⼦后不能去⾛做别的事情,但是厨师在做好包⼦后会告诉我,也就是我不⽤再⼀次次
为厨师包⼦有没有做好了。
异步⾮阻塞:我点完包⼦后可以做别的事情,⽽且可以⼀直在做别的去事情,因为厨师在做好包⼦后会告
诉我。

二.编译安装过程:

configure:检查当前编译环境,生成MakeFile

make:根据MakeFile生成相关模块

make install:将模块copy到指定目录

三.nginx一键编译安装脚本

[root@centos7 ~]# cat install_nginx_v2.sh
#!/bin/bash
#
#******************************************************************************
#Author:        zhanghui
#QQ:            19661891
#Date:          2021-03-17
#FileName:      install_nginx_v2.sh
#URL:           www.cnblogs.com/neteagles
#Description:   install_nginx for centos 7/8 & ubuntu 18.04/20.04
#Copyright (C): 2021 All rights reserved
#******************************************************************************
SRC_DIR=/usr/local/src
COLOR="echo -e \\033[01;31m"
END='\033[0m'

NGINX_URL=http://nginx.org/download/
NGINX_FILE=nginx-1.18.0.tar.gz
ECHO_NGINX=echo-nginx-module-0.62.tar.gz
OPENSSL=openssl-1.1.1j.tar.gz
NGINX_INSTALL_DIR=/apps/nginx
CPUS=`lscpu |awk '/^CPU\(s\)/{print $2}'`

os(){
    if grep -Eqi "CentOS" /etc/issue || grep -Eq "CentOS" /etc/*-release;then
        rpm -q redhat-lsb-core &> /dev/null || { ${COLOR}"安装lsb_release工具"${END};yum -y install redhat-lsb-core &> /dev/null; }
    fi
    OS_ID=`lsb_release -is`
    OS_RELEASE_VERSION=`lsb_release -rs |awk -F'.' '{print $1}'`
}

check_file (){
    cd  ${SRC_DIR}
    if [ ${OS_ID} == "CentOS" ] &> /dev/null;then
        rpm -q wget &> /dev/null || yum -y install wget &> /dev/null
    fi
    if [ ! -e ${NGINX_FILE} ];then
        ${COLOR}"缺少${NGINX_FILE}文件"${END}
        ${COLOR}'开始下载NGINX源码包'${END}
        wget ${NGINX_URL}${NGINX_FILE} || { ${COLOR}"NGINX源码包下载失败"${END}; exit; }
    else
        ${COLOR}"${NGINX_FILE}文件已准备好"${END}       
    fi
    if [ ! -e ${ECHO_NGINX} ];then
        ${COLOR}"缺少${ECHO_NGINX}文件"${END}
        exit    
    elif [ ! -e ${OPENSSL} ];then
        ${COLOR}"缺少${OPENSSL}文件"${END}
        exit
    else
        ${COLOR}"相关文件已准备好"${END}
    fi
} 

install_nginx(){
    [ -d ${NGINX_INSTALL_DIR} ] && { ${COLOR}"Nginx已存在,安装失败"${END};exit; }
    ${COLOR}"开始安装NGINX"${END}
    id nginx  &> /dev/null || { useradd -s /sbin/nologin -r  nginx; $COLOR"创建nginx用户"$END; }
    ${COLOR}"开始安装NGINX依赖包"${END}
    if [[ ${OS_RELEASE_VERSION} == 8 ]] &> /dev/null;then
        yum -y install make gcc-c++ libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel perl-ExtUtils-Embed &> /dev/null
    elif [[ ${OS_RELEASE_VERSION} == 7 ]] &> /dev/null;then
        yum -y install make gcc pcre-devel openssl-devel zlib-devel perl-ExtUtils-Embed &> /dev/null
    else
        apt update &> /dev/null;apt -y install make gcc libpcre3 libpcre3-dev openssl libssl-dev zlib1g-dev &> /dev/null
    fi
    cd $SRC_DIR
    tar xf ${NGINX_FILE} && tar xf ${ECHO_NGINX} && tar xf ${OPENSSL}
    NGINX_DIR=`echo ${NGINX_FILE}| sed -nr 's/^(.*[0-9]).*/\1/p'`
    ECHO_NGINX_DIR=`echo ${ECHO_NGINX}| sed -nr 's/^(.*[0-9]).*/\1/p'`
    OPENSSL_DIR=`echo ${OPENSSL}| sed -nr 's/^(.*[0-9][a-z]).*/\1/p'`
    cd ${NGINX_DIR}
    ./configure --prefix=${NGINX_INSTALL_DIR} --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module  --with-file-aio --add-module=${SRC_DIR}/${ECHO_NGINX_DIR} --with-openssl=${SRC_DIR}/${OPENSSL_DIR} 
    make -j $CPUS && make install 
    [ $? -eq 0 ] && $COLOR"NGINX编译安装成功"$END ||  { $COLOR"NGINX编译安装失败,退出!"$END;exit; }
    echo "PATH=${NGINX_INSTALL_DIR}/sbin:${PATH}" > /etc/profile.d/nginx.sh
    cat > /lib/systemd/system/nginx.service <<EOF
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=${NGINX_INSTALL_DIR}/logs/nginx.pid
ExecStartPre=/bin/rm -f ${NGINX_INSTALL_DIR}/logs/nginx.pid
ExecStartPre=${NGINX_INSTALL_DIR}/sbin/nginx -t
ExecStart=${NGINX_INSTALL_DIR}/sbin/nginx
ExecReload=/bin/kill -s HUP \$MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF
    systemctl daemon-reload
    systemctl enable --now nginx &> /dev/null 
    systemctl is-active nginx &> /dev/null ||  { ${COLOR}"NGINX 启动失败,退出!"${END} ; exit; }
    ${COLOR}"NGINX安装完成"${END}
}

main(){
    os
    check_file
    install_nginx
}

main

四.一键搭博客系统

[root@centos7 ~]#  cat install_mysql_nginx_php_wordpress.sh
#!/bin/bash
#
#*********************************************************************************************
#Author:        zhanghui
#QQ:            19661891
#Date:          2021-03-24
#FileName:      install_mysql_nginx_php_wordpress.sh
#URL:           www.cnblogs.com/neteagles
#Description:   install_mysql_nginx_php_wordpress for centos 7/8 & ubuntu 18.04/20.04
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************
SRC_DIR=/usr/local/src
COLOR="echo -e \\033[01;31m"
END='\033[0m'

MYSQL_URL=https://mirrors.cloud.tencent.com/mysql/downloads/MySQL-
MYSQL_VERSION='8.0/'
MYSQL_FILE='mysql-8.0.25-linux-glibc2.12-x86_64.tar.xz'
MYSQL_ROOT_PASSWORD=123456

NGINX_URL=http://nginx.org/download/
NGINX_FILE=nginx-1.18.0.tar.gz
ECHO_NGINX=echo-nginx-module-0.62.tar.gz
OPENSSL=openssl-1.1.1j.tar.gz
NGINX_INSTALL_DIR=/apps/nginx
CPUS=`lscpu |awk '/^CPU\(s\)/{print $2}'`

PHP='php-7.4.15.tar.xz'
PHP_INSTALL_DIR=/apps/php74

SERVERNAME=blog.neteagles.vip

APP='wordpress-5.7.2-zh_CN.tar.gz'
WEB_DIR=/data/www
MYSQL_WORDPRESS_PASSWORD=123456

${COLOR}'开始安装基于LNMP的wordpress'${END}
sleep 3

os(){
    if grep -Eqi "CentOS" /etc/issue || grep -Eq "CentOS" /etc/*-release;then
        rpm -q redhat-lsb-core &> /dev/null || { ${COLOR}"安装lsb_release工具"${END};yum -y install redhat-lsb-core &> /dev/null; }
    fi
    OS_ID=`lsb_release -is`
    OS_RELEASE_VERSION=`lsb_release -rs |awk -F'.' '{print $1}'`
}

check_file (){
    cd  ${SRC_DIR}
    ${COLOR}"请将\"${ECHO_NGINX}${TAR}、${OPENSSL}${TAR}、${PHP}和${APP}\"软件放在\"${SRC_DIR}\"目录下,如果没有放好软件包,请按ctrl+c退出"${END}
    sleep 10
    rpm -q wget &> /dev/null || yum -y install wget &> /dev/null
    if [ ${OS_ID} == "CentOS" ] &> /dev/null;then
        rpm -q wget &> /dev/null || yum -y install wget &> /dev/null
    fi
    if [ ! -e ${MYSQL_FILE} ];then
        ${COLOR}"缺少${MYSQL_FILE}文件"${END}
        ${COLOR}'开始下载MYSQL二进制安装包'${END}
        wget ${MYSQL_URL}${MYSQL_VERSION}${MYSQL_FILE} || { ${COLOR}"MYSQL二进制安装包下载失败"${END}; exit; }
    else
        ${COLOR}"${MYSQL_FILE}文件已准备好"${END}
    fi
    if [ ! -e ${NGINX_FILE} ];then
        ${COLOR}"缺少${NGINX_FILE}文件"${END}
        ${COLOR}'开始下载NGINX源码包'${END}
        wget ${NGINX_URL}${NGINX_FILE} || { ${COLOR}"NGINX源码包下载失败"${END}; exit; }
    else
        ${COLOR}"${NGINX_FILE}文件已准备好"${END}       
    fi
    if [ ! -e ${ECHO_NGINX} ];then
        ${COLOR}"缺少${ECHO_NGINX}文件"${END}
        exit    
    elif [ ! -e ${OPENSSL} ];then
        ${COLOR}"缺少${OPENSSL}文件"${END}
        exit
    elif [ ! -e ${PHP} ];then
        ${COLOR}"缺少${PHP}文件"${END}
        exit
    elif [ ! -e ${APP} ];then
        ${COLOR}"缺少${APP}文件"${END}
        exit
    else
        ${COLOR}"相关文件已准备好"${END}
    fi
}

install_mysql(){
    [ -d /usr/local/mysql ] && { ${COLOR}"MySQL数据库已存在,安装失败"${END};exit; }
    ${COLOR}"开始安装MySQL数据库..."${END}
    ${COLOR}'开始安装MYSQL依赖包'${END}
    if [[ ${OS_RELEASE_VERSION} == 8 ]] &> /dev/null;then
        yum -y install libaio perl-Data-Dumper ncurses-compat-libs &> /dev/null
    elif [[ ${OS_RELEASE_VERSION} == 7 ]] &> /dev/null;then
        yum -y install libaio perl-Data-Dumper &> /dev/null
    else
        apt update &> /dev/null;apt -y install numactl libaio-dev libtinfo5 &> /dev/null
    fi
    cd  ${SRC_DIR}
    tar xf ${MYSQL_FILE} -C /usr/local/
    MYSQL_DIR=`echo ${MYSQL_FILE}| sed -nr 's/^(.*[0-9]).*/\1/p'`
    ln -s  /usr/local/${MYSQL_DIR} /usr/local/mysql
    id mysql &> /dev/null || { useradd -s /sbin/nologin -r  mysql ; ${COLOR}"创建mysql用户"${END}; }
    chown -R  mysql.mysql /usr/local/mysql/
    echo 'PATH=/usr/local/mysql/bin/:$PATH' > /etc/profile.d/mysql.sh
    .  /etc/profile.d/mysql.sh
    cat > /etc/my.cnf <<-EOF
[mysqld]
server-id=1
log-bin
datadir=/data/mysql
socket=/data/mysql/mysql.sock
log-error=/data/mysql/mysql.log
pid-file=/data/mysql/mysql.pid
[client]
socket=/data/mysql/mysql.sock
EOF
    [ -d /data/mysql ] || mkdir -p /data/mysql &> /dev/null
    chown -R  mysql.mysql /data/mysql
    mysqld --initialize --user=mysql --datadir=/data/mysql 
    cp /usr/local/mysql/support-files/mysql.server  /etc/init.d/mysqld
    if [ ${OS_ID} == "CentOS" ] &> /dev/null;then
        chkconfig --add mysqld
    else
        update-rc.d -f mysqld defaults
    fi
    cat > /lib/systemd/system/mysqld.service <<-EOF
[Unit]
Description=mysql database server
After=network.target

[Service]
Type=notify
PrivateNetwork=false
Type=forking
Restart=no
TimeoutSec=5min
IgnoreSIGPIPE=no
KillMode=process
GuessMainPID=no
RemainAfterExit=yes
SuccessExitStatus=5 6
ExecStart=/etc/init.d/mysqld start
ExecStop=/etc/init.d//mysqld stop
ExecReload=/etc/init.d/mysqld reload

[Install]
WantedBy=multi-user.target
Alias=mysqld.service
EOF
    systemctl daemon-reload
    systemctl enable --now mysqld &> /dev/null
    [ $? -ne 0 ] && { ${COLOR}"数据库启动失败,退出!"${END};exit; }
    MYSQL_OLDPASSWORD=`awk '/A temporary password/{print $NF}' /data/mysql/mysql.log`
    mysqladmin  -uroot -p${MYSQL_OLDPASSWORD} password ${MYSQL_ROOT_PASSWORD} &>/dev/null
    ${COLOR}"MySQL数据库安装完成"${END}
}

install_nginx(){
    [ -d ${NGINX_INSTALL_DIR} ] && { ${COLOR}"Nginx已存在,安装失败"${END};exit; }
    ${COLOR}"开始安装NGINX"${END}
    id nginx  &> /dev/null || { useradd -s /sbin/nologin -r  nginx; $COLOR"创建nginx用户"$END; }
    ${COLOR}"开始安装NGINX依赖包"${END}
    if [[ ${OS_RELEASE_VERSION} == 8 ]] &> /dev/null;then
        yum -y install make gcc-c++ libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel perl-ExtUtils-Embed &> /dev/null
    elif [[ ${OS_RELEASE_VERSION} == 7 ]] &> /dev/null;then
        yum -y install make gcc pcre-devel openssl-devel zlib-devel perl-ExtUtils-Embed &> /dev/null
    else
        apt update &> /dev/null;apt -y install make gcc libpcre3 libpcre3-dev openssl libssl-dev zlib1g-dev &> /dev/null
    fi
    cd $SRC_DIR
    tar xf ${NGINX_FILE} && tar xf ${ECHO_NGINX} && tar xf ${OPENSSL}
    NGINX_DIR=`echo ${NGINX_FILE}| sed -nr 's/^(.*[0-9]).*/\1/p'`
    ECHO_NGINX_DIR=`echo ${ECHO_NGINX}| sed -nr 's/^(.*[0-9]).*/\1/p'`
    OPENSSL_DIR=`echo ${OPENSSL}| sed -nr 's/^(.*[0-9][a-z]).*/\1/p'`
    cd ${NGINX_DIR}
    ./configure --prefix=${NGINX_INSTALL_DIR} --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module  --with-file-aio --add-module=${SRC_DIR}/${ECHO_NGINX_DIR} --with-openssl=${SRC_DIR}/${OPENSSL_DIR} 
    make -j $CPUS && make install 
    [ $? -eq 0 ] && $COLOR"NGINX编译安装成功"$END ||  { $COLOR"NGINX编译安装失败,退出!"$END;exit; }
    echo "PATH=${NGINX_INSTALL_DIR}/sbin:${PATH}" > /etc/profile.d/nginx.sh
    cat > /lib/systemd/system/nginx.service <<EOF
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=${NGINX_INSTALL_DIR}/logs/nginx.pid
ExecStartPre=/bin/rm -f ${NGINX_INSTALL_DIR}/logs/nginx.pid
ExecStartPre=${NGINX_INSTALL_DIR}/sbin/nginx -t
ExecStart=${NGINX_INSTALL_DIR}/sbin/nginx
ExecReload=/bin/kill -s HUP \$MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF
    ${COLOR}"开始设置Nginx配置文件"${END}
    [ -d /data/www ] || mkdir -p /data/www/ &> /dev/null 
    cat > /apps/nginx/conf/nginx.conf <<-EOF
worker_processes  auto;
events {
    worker_connections  10240;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    server_tokens off;
    log_format  main  '\$remote_addr - \$remote_user [\$time_local] "\$request" '
    sendfile        on;
    client_max_body_size 100m;
    keepalive_timeout  65;
    server {
        listen       80 default_server;
        server_name  ${SERVERNAME}; 
        root /data/www ;
        access_log  logs/nginx.access.log  main;
        location / {
            root   /data/www/;
            index  index.php index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        location ~ \.php$ {
            root           /data/www;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  \$document_root\$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
}
EOF
    systemctl daemon-reload
    systemctl enable --now nginx &> /dev/null 
    systemctl is-active nginx &> /dev/null ||  { ${COLOR}"NGINX 启动失败,退出!"${END} ; exit; }
    ${COLOR}"NGINX安装完成"${END}
}

install_php (){
    ${COLOR}"开始安装PHP"${END}
    ${COLOR}"开始安装PHP依赖包"${END}
    if [[ ${OS_RELEASE_VERSION} == 8 ]] &> /dev/null;then
    cat > /etc/yum.repos.d/PowerTools.repo <<-EOF
[PowerTools]
name=PowerTools
baseurl=https://mirrors.aliyun.com/centos/8/PowerTools/x86_64/os/
        https://mirrors.huaweicloud.com/centos/8/PowerTools/x86_64/os/
        https://mirrors.cloud.tencent.com/centos/8/PowerTools/x86_64/os/
        https://mirrors.tuna.tsinghua.edu.cn/centos/8/PowerTools/x86_64/os/
        http://mirrors.163.com/centos/8/PowerTools/x86_64/os/
        http://mirrors.sohu.com/centos/8/PowerTools/x86_64/os/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
EOF
    fi
    if [ ${OS_ID} == "CentOS" ] &> /dev/null;then
        yum -y install gcc make libxml2-devel bzip2-devel libmcrypt-devel sqlite-devel oniguruma-devel &> /dev/null
    else
        apt update &> /dev/null;apt -y install gcc make openssl pkg-config libssl-dev libsqlite3-dev libonig-dev curl libbz2-dev libxml2-dev libjpeg-dev libpng-dev libfreetype6-dev libzip-dev &> /dev/null 
    fi
    cd ${SRC_DIR}
    tar xf ${PHP}
    PHP_DIR=`echo ${PHP}| sed -nr 's/^(.*[0-9]).*/\1/p'`
    cd ${PHP_DIR}
    ./configure --prefix=${PHP_INSTALL_DIR} --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-openssl --with-zlib --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --enable-mbstring --enable-xml --enable-sockets --enable-fpm --enable-maintainer-zts --disable-fileinfo
    make -j ${CPUS} && make install
    [ $? -eq 0 ] && ${COLOR}"PHP编译安装成功"${END} ||  { ${COLOR}"PHP编译安装失败,退出!"${END};exit; }
    echo 'PATH=$PHP_INSTALL_DIR/bin:$PATH' >/etc/profile.d/lamp.sh
    . /etc/profile.d/lamp.sh
    cp php.ini-production  /etc/php.ini
    cp sapi/fpm/php-fpm.service /lib/systemd/system/
    cd ${PHP_INSTALL_DIR}/etc
    cp php-fpm.conf.default  php-fpm.conf
    cd php-fpm.d/
    cp www.conf.default www.conf
    sed -i.bak -e 's/^user.*/user = nginx/' -e 's/^group.*/group = nginx/' -e 's/#pm.status_path =.*/pm.status_path = \/fpm_status/' -e 's/#ping.path =.*/ping.path = \/ping/' ${PHP_INSTALL_DIR}/etc/php-fpm.d/www.conf
    mkdir /etc/php.d/
    cat > /etc/php.d/opcache.ini <<-EOF
[opcache]
zend_extension=opcache.so
opcache.enable=1
EOF
    systemctl daemon-reload
    systemctl enable --now php-fpm &> /dev/null 
    systemctl is-active  php-fpm &> /dev/null ||  { ${COLOR}"PHP-FPM 启动失败,退出!"${END} ; exit; }
    $COLOR"PHP安装完成"${END}
}

install_wordpress(){
    ${COLOR}"开始安装WORDPRESS"${END}
    cd ${SRC_DIR}
    tar xf ${APP}
    [ -d ${WEB_DIR} ] || mkdir -p ${WEB_DIR}
    mv wordpress/* ${WEB_DIR}/
    chown -R nginx.nginx ${WEB_DIR}/wp-content/
    cd ${WEB_DIR}
    mv wp-config-sample.php wp-config.php
    mysql -uroot -p"${MYSQL_ROOT_PASSWORD}" -e "create database wordpress;create user wordpress@'127.0.0.1' identified by '${MYSQL_WORDPRESS_PASSWORD}';grant all on wordpress.* to wordpress@'127.0.0.1';" &>/dev/null
    sed -i.bak -e 's/database_name_here/wordpress/' -e 's/username_here/wordpress/' -e 's/password_here/'''${MYSQL_WORDPRESS_PASSWORD}'''/' -e 's/localhost/127.0.0.1/'  wp-config.php
    ${COLOR}"WORDPRESS安装完成"${END}
}

main(){
    os
    check_file
    install_mysql
    install_nginx
    install_php
    install_wordpress
}

main

五.一键搭论坛系统

[root@centos7 ~]# cat install_mysql_nginx_php_discuz.sh
#!/bin/bash
#
#****************************************************************************************
#Author:        zhanghui
#QQ:            19661891
#Date:          2021-03-25
#FileName:      install_mysql_nginx_php_discuz.sh
#URL:           www.neteagles.cn
#Description:   install_mysql_nginx_php_discuz for centos 7/8 & ubuntu 18.04/20.04
#Copyright (C): 2021 All rights reserved
#****************************************************************************************
SRC_DIR=/usr/local/src
COLOR="echo -e \\033[01;31m"
END='\033[0m'

MYSQL_URL=https://mirrors.cloud.tencent.com/mysql/downloads/MySQL-
MYSQL_VERSION='8.0/'
MYSQL_FILE='mysql-8.0.25-linux-glibc2.12-x86_64.tar.xz'
MYSQL_ROOT_PASSWORD=123456

NGINX_URL=http://nginx.org/download/
NGINX_FILE=nginx-1.18.0.tar.gz
ECHO_NGINX=echo-nginx-module-0.62.tar.gz
OPENSSL=openssl-1.1.1j.tar.gz
NGINX_INSTALL_DIR=/apps/nginx
CPUS=`lscpu |awk '/^CPU\(s\)/{print $2}'`

PHP='php-7.4.15.tar.xz'
PHP_INSTALL_DIR=/apps/php74

SERVERNAME=forum.neteagles.vip

APP='Discuz_X3.4_SC_UTF8_20210320.zip'
WEB_DIR=/data/www
MYSQL_DISCUZ_PASSWORD=123456

${COLOR}'开始安装基于LNMP的DISCUZ'${END}
sleep 3

os(){
    if grep -Eqi "CentOS" /etc/issue || grep -Eq "CentOS" /etc/*-release;then
        rpm -q redhat-lsb-core &> /dev/null || { ${COLOR}"安装lsb_release工具"${END};yum -y install  redhat-lsb-core &> /dev/null; }
    fi
    OS_ID=`lsb_release -is`
    OS_RELEASE_VERSION=`lsb_release -rs |awk -F'.' '{print $1}'`
}

check_file (){
    cd  ${SRC_DIR}
    ${COLOR}"请将\"${ECHO_NGINX}${TAR}、${OPENSSL}${TAR}、${PHP}和${APP}\"软件放在\"${SRC_DIR}\"目录下,如果没有放好软件包,请按ctrl+c退出"${END}
    sleep 10
    if [ ${OS_ID} == "CentOS" ] &> /dev/null;then
        rpm -q wget &> /dev/null || yum -y install wget &> /dev/null
    fi
    if [ ! -e ${MYSQL_FILE} ];then
        ${COLOR}"缺少${MYSQL_FILE}文件"${END}
        ${COLOR}'开始下载MYSQL二进制安装包'${END}
        wget ${MYSQL_URL}${MYSQL_VERSION}${MYSQL_FILE} || { ${COLOR}"MYSQL二进制安装包下载失败"${END}; exit; }
    else
        ${COLOR}"${MYSQL_FILE}文件已准备好"${END}
    fi
    if [ ! -e ${NGINX_FILE} ];then
        ${COLOR}"缺少${NGINX_FILE}文件"${END}
        ${COLOR}'开始下载NGINX源码包'${END}
        wget ${NGINX_URL}${NGINX_FILE} || { ${COLOR}"NGINX源码包下载失败"${END}; exit; }
    else
        ${COLOR}"${NGINX_FILE}文件已准备好"${END}       
    fi
    if [ ! -e ${ECHO_NGINX} ];then
        ${COLOR}"缺少${ECHO_NGINX}文件"${END}
        exit    
    elif [ ! -e ${OPENSSL} ];then
        ${COLOR}"缺少${OPENSSL}文件"${END}
        exit
    elif [ ! -e ${PHP} ];then
        ${COLOR}"缺少${PHP}文件"${END}
        exit
    elif [ ! -e ${APP} ];then
        ${COLOR}"缺少${APP}文件"${END}
        exit
    else
        ${COLOR}"相关文件已准备好"${END}
    fi
}

install_mysql(){
    [ -d /usr/local/mysql ] && { ${COLOR}"MySQL数据库已存在,安装失败"${END};exit; }
    ${COLOR}"开始安装MySQL数据库..."${END}
    ${COLOR}'开始安装MYSQL依赖包'${END}
    if [[ ${OS_RELEASE_VERSION} == 8 ]] &> /dev/null;then
        yum -y install libaio perl-Data-Dumper ncurses-compat-libs &> /dev/null
    elif [[ ${OS_RELEASE_VERSION} == 7 ]] &> /dev/null;then
        yum -y install libaio perl-Data-Dumper &> /dev/null
    else
        apt update &> /dev/null;apt -y install numactl libaio-dev libtinfo5 &> /dev/null
    fi
    cd  ${SRC_DIR}
    tar xf ${MYSQL_FILE} -C /usr/local/
    MYSQL_DIR=`echo ${MYSQL_FILE}| sed -nr 's/^(.*[0-9]).*/\1/p'`
    ln -s  /usr/local/${MYSQL_DIR} /usr/local/mysql
    id mysql &> /dev/null || { useradd -s /sbin/nologin -r  mysql ; ${COLOR}"创建mysql用户"${END}; }
    chown -R  mysql.mysql /usr/local/mysql/
    echo 'PATH=/usr/local/mysql/bin/:$PATH' > /etc/profile.d/mysql.sh
    .  /etc/profile.d/mysql.sh
    cat > /etc/my.cnf <<-EOF
[mysqld]
server-id=1
log-bin
datadir=/data/mysql
socket=/data/mysql/mysql.sock
log-error=/data/mysql/mysql.log
pid-file=/data/mysql/mysql.pid
[client]
socket=/data/mysql/mysql.sock
EOF
    [ -d /data/mysql ] || mkdir -p /data/mysql &> /dev/null
    chown -R  mysql.mysql /data/mysql
    mysqld --initialize --user=mysql --datadir=/data/mysql 
    cp /usr/local/mysql/support-files/mysql.server  /etc/init.d/mysqld
    if [ ${OS_ID} == "CentOS" ] &> /dev/null;then
        chkconfig --add mysqld
    else
        update-rc.d -f mysqld defaults
    fi
    cat > /lib/systemd/system/mysqld.service <<-EOF
[Unit]
Description=mysql database server
After=network.target

[Service]
Type=notify
PrivateNetwork=false
Type=forking
Restart=no
TimeoutSec=5min
IgnoreSIGPIPE=no
KillMode=process
GuessMainPID=no
RemainAfterExit=yes
SuccessExitStatus=5 6
ExecStart=/etc/init.d/mysqld start
ExecStop=/etc/init.d//mysqld stop
ExecReload=/etc/init.d/mysqld reload

[Install]
WantedBy=multi-user.target
Alias=mysqld.service
EOF
    systemctl daemon-reload
    systemctl enable --now mysqld &> /dev/null
    [ $? -ne 0 ] && { ${COLOR}"数据库启动失败,退出!"${END};exit; }
    MYSQL_OLDPASSWORD=`awk '/A temporary password/{print $NF}' /data/mysql/mysql.log`
    mysqladmin  -uroot -p${MYSQL_OLDPASSWORD} password ${MYSQL_ROOT_PASSWORD} &>/dev/null
    ${COLOR}"MySQL数据库安装完成"${END}
}

install_nginx(){
    [ -d ${NGINX_INSTALL_DIR} ] && { ${COLOR}"Nginx已存在,安装失败"${END};exit; }
    ${COLOR}"开始安装NGINX"${END}
    id nginx  &> /dev/null || { useradd -s /sbin/nologin -r  nginx; $COLOR"创建nginx用户"$END; }
    ${COLOR}"开始安装NGINX依赖包"${END}
    if [[ ${OS_RELEASE_VERSION} == 8 ]] &> /dev/null;then
        yum -y install make gcc-c++ libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel perl-ExtUtils-Embed &> /dev/null
    elif [[ ${OS_RELEASE_VERSION} == 7 ]] &> /dev/null;then
        yum -y install make gcc pcre-devel openssl-devel zlib-devel perl-ExtUtils-Embed &> /dev/null
    else
        apt update &> /dev/null;apt -y install make gcc libpcre3 libpcre3-dev openssl libssl-dev zlib1g-dev &> /dev/null
    fi
    cd $SRC_DIR
    tar xf ${NGINX_FILE} && tar xf ${ECHO_NGINX} && tar xf ${OPENSSL}
    NGINX_DIR=`echo ${NGINX_FILE}| sed -nr 's/^(.*[0-9]).*/\1/p'`
    ECHO_NGINX_DIR=`echo ${ECHO_NGINX}| sed -nr 's/^(.*[0-9]).*/\1/p'`
    OPENSSL_DIR=`echo ${OPENSSL}| sed -nr 's/^(.*[0-9][a-z]).*/\1/p'`
    cd ${NGINX_DIR}
    ./configure --prefix=${NGINX_INSTALL_DIR} --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module  --with-file-aio --add-module=${SRC_DIR}/${ECHO_NGINX_DIR} --with-openssl=${SRC_DIR}/${OPENSSL_DIR} 
    make -j $CPUS && make install 
    [ $? -eq 0 ] && $COLOR"NGINX编译安装成功"$END ||  { $COLOR"NGINX编译安装失败,退出!"$END;exit; }
    echo "PATH=${NGINX_INSTALL_DIR}/sbin:${PATH}" > /etc/profile.d/nginx.sh
    cat > /lib/systemd/system/nginx.service <<EOF
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=${NGINX_INSTALL_DIR}/logs/nginx.pid
ExecStartPre=/bin/rm -f ${NGINX_INSTALL_DIR}/logs/nginx.pid
ExecStartPre=${NGINX_INSTALL_DIR}/sbin/nginx -t
ExecStart=${NGINX_INSTALL_DIR}/sbin/nginx
ExecReload=/bin/kill -s HUP \$MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF
    ${COLOR}"开始设置Nginx配置文件"${END}
    [ -d /data/www ] || mkdir -pv /data/www/ &> /dev/null 
    cat > /apps/nginx/conf/nginx.conf <<EOF
worker_processes  auto;
events {
    worker_connections  10240;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    server_tokens off;
    log_format  main  '\$remote_addr - \$remote_user [\$time_local] "\$request" '
    sendfile        on;
    client_max_body_size 100m;
    keepalive_timeout  65;
    server {
        listen       80 default_server;
        server_name  ${SERVERNAME}; 
        root /data/www ;
        access_log  logs/nginx.access.log  main;
        location / {
            root   /data/www/;
            index  index.php index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        location ~ \.php$ {
            root           /data/www;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  \$document_root\$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
}
EOF
    systemctl daemon-reload
    systemctl enable --now nginx &> /dev/null 
    systemctl is-active nginx &> /dev/null ||  { ${COLOR}"NGINX 启动失败,退出!"${END} ; exit; }
    ${COLOR}"NGINX安装完成"${END}
}

install_php (){
    ${COLOR}"开始安装PHP"${END}
    ${COLOR}"开始安装PHP依赖包"${END}
    if [[ ${OS_RELEASE_VERSION} == 8 ]] &> /dev/null;then
    cat > /etc/yum.repos.d/PowerTools.repo <<-EOF
[PowerTools]
name=PowerTools
baseurl=https://mirrors.aliyun.com/centos/8/PowerTools/x86_64/os/
        https://mirrors.huaweicloud.com/centos/8/PowerTools/x86_64/os/
        https://mirrors.cloud.tencent.com/centos/8/PowerTools/x86_64/os/
        https://mirrors.tuna.tsinghua.edu.cn/centos/8/PowerTools/x86_64/os/
        http://mirrors.163.com/centos/8/PowerTools/x86_64/os/
        http://mirrors.sohu.com/centos/8/PowerTools/x86_64/os/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
EOF
    fi
    if [ ${OS_ID} == "CentOS" ] &> /dev/null;then
        yum -y install gcc make libxml2-devel bzip2-devel libmcrypt-devel sqlite-devel oniguruma-devel &> /dev/null
    else
        apt update &> /dev/null;apt -y install gcc make openssl pkg-config libssl-dev libsqlite3-dev libonig-dev curl libbz2-dev libxml2-dev libjpeg-dev libpng-dev libfreetype6-dev libzip-dev &> /dev/null 
    fi
    cd ${SRC_DIR}
    tar xf ${PHP}
    PHP_DIR=`echo ${PHP}| sed -nr 's/^(.*[0-9]).*/\1/p'`
    cd ${PHP_DIR}
    ./configure --prefix=${PHP_INSTALL_DIR} --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-openssl --with-zlib --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --enable-mbstring --enable-xml --enable-sockets --enable-fpm --enable-maintainer-zts --disable-fileinfo
    make -j ${CPUS} && make install
    [ $? -eq 0 ] && ${COLOR}"PHP编译安装成功"${END} ||  { ${COLOR}"PHP编译安装失败,退出!"${END};exit; }
    echo 'PATH=$PHP_INSTALL_DIR/bin:$PATH' >/etc/profile.d/lamp.sh
    . /etc/profile.d/lamp.sh
    cp php.ini-production  /etc/php.ini
    cp sapi/fpm/php-fpm.service /lib/systemd/system/
    cd ${PHP_INSTALL_DIR}/etc
    cp php-fpm.conf.default  php-fpm.conf
    cd php-fpm.d/
    cp www.conf.default www.conf
    sed -i.bak -e 's/^user.*/user = nginx/' -e 's/^group.*/group = nginx/' -e 's/#pm.status_path =.*/pm.status_path = \/fpm_status/' -e 's/#ping.path =.*/ping.path = \/ping/' ${PHP_INSTALL_DIR}/etc/php-fpm.d/www.conf
    mkdir /etc/php.d/
    cat > /etc/php.d/opcache.ini <<-EOF
[opcache]
zend_extension=opcache.so
opcache.enable=1
EOF
    systemctl daemon-reload
    systemctl enable --now php-fpm &> /dev/null 
    systemctl is-active  php-fpm &> /dev/null ||  { ${COLOR}"PHP-FPM 启动失败,退出!"${END} ; exit; }
    $COLOR"PHP安装完成"${END}
}

install_discuz(){
    ${COLOR}"开始安装DISCUZ"${END}
    cd ${SRC_DIR}
    rpm -q unzip &> /dev/null || yum -y install unzip &> /dev/null
    unzip ${APP} &> /dev/null  
    [ -d ${WEB_DIR} ] || mkdir -p ${WEB_DIR}
    mv upload/* ${WEB_DIR}/
    setfacl -R -m u:nginx:rwx ${WEB_DIR}
    mysql -uroot -p"${MYSQL_ROOT_PASSWORD}" -e "create database ultrax;create user ultrax@'127.0.0.1' identified by '${MYSQL_DISCUZ_PASSWORD}';grant all on ultrax.* to ultrax@'127.0.0.1';" &>/dev/null
    ${COLOR}"DISCUZ安装完成"${END}
}

main(){
    os
    check_file
    install_mysql
    install_nginx
    install_php
    install_discuz
}

main

六.一键搭mysql web端图形管理工具

[root@centos7 ~]# cat install_mysql_nginx_php_phpmyadmin.sh
#!/bin/bash
#
#*********************************************************************************************
#Author:        zhanghui
#QQ:            19661891
#Date:          2021-03-25
#FileName:      install_mysql_nginx_php_phpmyadmin.sh
#URL:           www.neteagles.cn
#Description:   install_mysql_nginx_php_phpmyadmin for centos 7/8 & ubuntu 18.04/20.04
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************
SRC_DIR=/usr/local/src
COLOR="echo -e \\033[01;31m"
END='\033[0m'

MYSQL_URL=https://mirrors.cloud.tencent.com/mysql/downloads/MySQL-
MYSQL_VERSION='8.0/'
MYSQL_FILE='mysql-8.0.25-linux-glibc2.12-x86_64.tar.xz'
MYSQL_ROOT_PASSWORD=123456

NGINX_URL=http://nginx.org/download/
NGINX_FILE=nginx-1.18.0.tar.gz
ECHO_NGINX=echo-nginx-module-0.62.tar.gz
OPENSSL=openssl-1.1.1j.tar.gz
NGINX_INSTALL_DIR=/apps/nginx
CPUS=`lscpu |awk '/^CPU\(s\)/{print $2}'`

PHP='php-7.4.15.tar.xz'
PHP_INSTALL_DIR=/apps/php74

SERVERNAME=phpmyadmin.neteagles.vip

APP='phpMyAdmin-5.1.0-all-languages.zip'
WEB_DIR=/data/www

${COLOR}'开始安装基于LNMP的PHPMYADMIN'${END}
sleep 3

os(){
    if grep -Eqi "CentOS" /etc/issue || grep -Eq "CentOS" /etc/*-release;then
        rpm -q redhat-lsb-core &> /dev/null || { ${COLOR}"安装lsb_release工具"${END};yum -y install  redhat-lsb-core &> /dev/null; }
    fi
    OS_ID=`lsb_release -is`
    OS_RELEASE_VERSION=`lsb_release -rs |awk -F'.' '{print $1}'`
}

check_file (){
    cd  ${SRC_DIR}
    ${COLOR}"请将\"${ECHO_NGINX}${TAR}、${OPENSSL}${TAR}、${PHP}和${APP}\"软件放在\"${SRC_DIR}\"目录下,如果没有放好软件包,请按ctrl+c退出"${END}
    sleep 10
    if [ ${OS_ID} == "CentOS" ] &> /dev/null;then
        rpm -q wget &> /dev/null || yum -y install wget &> /dev/null
    fi
    if [ ! -e ${MYSQL_FILE} ];then
        ${COLOR}"缺少${MYSQL_FILE}文件"${END}
        ${COLOR}'开始下载MYSQL二进制安装包'${END}
        wget ${MYSQL_URL}${MYSQL_VERSION}${MYSQL_FILE} || { ${COLOR}"MYSQL二进制安装包下载失败"${END}; exit; }
    else
        ${COLOR}"${MYSQL_FILE}文件已准备好"${END}
    fi
    if [ ! -e ${NGINX_FILE} ];then
        ${COLOR}"缺少${NGINX_FILE}文件"${END}
        ${COLOR}'开始下载NGINX源码包'${END}
        wget ${NGINX_URL}${NGINX_FILE} || { ${COLOR}"NGINX源码包下载失败"${END}; exit; }
    else
        ${COLOR}"${NGINX_FILE}文件已准备好"${END}       
    fi
    if [ ! -e ${ECHO_NGINX} ];then
        ${COLOR}"缺少${ECHO_NGINX}文件"${END}
        exit    
    elif [ ! -e ${OPENSSL} ];then
        ${COLOR}"缺少${OPENSSL}文件"${END}
        exit
    elif [ ! -e ${PHP} ];then
        ${COLOR}"缺少${PHP}文件"${END}
        exit
    elif [ ! -e ${APP} ];then
        ${COLOR}"缺少${APP}文件"${END}
        exit
    else
        ${COLOR}"相关文件已准备好"${END}
    fi
}

install_mysql(){
    [ -d /usr/local/mysql ] && { ${COLOR}"MySQL数据库已存在,安装失败"${END};exit; }
    ${COLOR}"开始安装MySQL数据库..."${END}
    ${COLOR}'开始安装MYSQL依赖包'${END}
    if [[ ${OS_RELEASE_VERSION} == 8 ]] &> /dev/null;then
        yum -y install libaio perl-Data-Dumper ncurses-compat-libs &> /dev/null
    elif [[ ${OS_RELEASE_VERSION} == 7 ]] &> /dev/null;then
        yum -y install libaio perl-Data-Dumper &> /dev/null
    else
        apt update &> /dev/null;apt -y install numactl libaio-dev libtinfo5 &> /dev/null
    fi
    cd  ${SRC_DIR}
    tar xf ${MYSQL_FILE} -C /usr/local/
    MYSQL_DIR=`echo ${MYSQL_FILE}| sed -nr 's/^(.*[0-9]).*/\1/p'`
    ln -s  /usr/local/${MYSQL_DIR} /usr/local/mysql
    id mysql &> /dev/null || { useradd -s /sbin/nologin -r  mysql ; ${COLOR}"创建mysql用户"${END}; }
    chown -R  mysql.mysql /usr/local/mysql/
    echo 'PATH=/usr/local/mysql/bin/:$PATH' > /etc/profile.d/mysql.sh
    .  /etc/profile.d/mysql.sh
    cat > /etc/my.cnf <<-EOF
[mysqld]
server-id=1
log-bin
datadir=/data/mysql
socket=/data/mysql/mysql.sock
log-error=/data/mysql/mysql.log
pid-file=/data/mysql/mysql.pid
[client]
socket=/data/mysql/mysql.sock
EOF
    [ -d /data/mysql ] || mkdir -p /data/mysql &> /dev/null
    chown -R  mysql.mysql /data/mysql
    mysqld --initialize --user=mysql --datadir=/data/mysql 
    cp /usr/local/mysql/support-files/mysql.server  /etc/init.d/mysqld
    if [ ${OS_ID} == "CentOS" ] &> /dev/null;then
        chkconfig --add mysqld
    else
        update-rc.d -f mysqld defaults
    fi
    cat > /lib/systemd/system/mysqld.service <<-EOF
[Unit]
Description=mysql database server
After=network.target

[Service]
Type=notify
PrivateNetwork=false
Type=forking
Restart=no
TimeoutSec=5min
IgnoreSIGPIPE=no
KillMode=process
GuessMainPID=no
RemainAfterExit=yes
SuccessExitStatus=5 6
ExecStart=/etc/init.d/mysqld start
ExecStop=/etc/init.d//mysqld stop
ExecReload=/etc/init.d/mysqld reload

[Install]
WantedBy=multi-user.target
Alias=mysqld.service
EOF
    systemctl daemon-reload
    systemctl enable --now mysqld &> /dev/null
    [ $? -ne 0 ] && { ${COLOR}"数据库启动失败,退出!"${END};exit; }
    MYSQL_OLDPASSWORD=`awk '/A temporary password/{print $NF}' /data/mysql/mysql.log`
    mysqladmin  -uroot -p${MYSQL_OLDPASSWORD} password ${MYSQL_ROOT_PASSWORD} &>/dev/null
    ${COLOR}"MySQL数据库安装完成"${END}
}

install_nginx(){
    [ -d ${NGINX_INSTALL_DIR} ] && { ${COLOR}"Nginx已存在,安装失败"${END};exit; }
    ${COLOR}"开始安装NGINX"${END}
    id nginx  &> /dev/null || { useradd -s /sbin/nologin -r  nginx; $COLOR"创建nginx用户"$END; }
    ${COLOR}"开始安装NGINX依赖包"${END}
    if [[ ${OS_RELEASE_VERSION} == 8 ]] &> /dev/null;then
        yum -y install make gcc-c++ libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel perl-ExtUtils-Embed &> /dev/null
    elif [[ ${OS_RELEASE_VERSION} == 7 ]] &> /dev/null;then
        yum -y install make gcc pcre-devel openssl-devel zlib-devel perl-ExtUtils-Embed &> /dev/null
    else
        apt update &> /dev/null;apt -y install make gcc libpcre3 libpcre3-dev openssl libssl-dev zlib1g-dev &> /dev/null
    fi
    cd $SRC_DIR
    tar xf ${NGINX_FILE} && tar xf ${ECHO_NGINX} && tar xf ${OPENSSL}
    NGINX_DIR=`echo ${NGINX_FILE}| sed -nr 's/^(.*[0-9]).*/\1/p'`
    ECHO_NGINX_DIR=`echo ${ECHO_NGINX}| sed -nr 's/^(.*[0-9]).*/\1/p'`
    OPENSSL_DIR=`echo ${OPENSSL}| sed -nr 's/^(.*[0-9][a-z]).*/\1/p'`
    cd ${NGINX_DIR}
    ./configure --prefix=${NGINX_INSTALL_DIR} --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module  --with-file-aio --add-module=${SRC_DIR}/${ECHO_NGINX_DIR} --with-openssl=${SRC_DIR}/${OPENSSL_DIR} 
    make -j $CPUS && make install 
    [ $? -eq 0 ] && $COLOR"NGINX编译安装成功"$END ||  { $COLOR"NGINX编译安装失败,退出!"$END;exit; }
    echo "PATH=${NGINX_INSTALL_DIR}/sbin:${PATH}" > /etc/profile.d/nginx.sh
    cat > /lib/systemd/system/nginx.service <<EOF
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=${NGINX_INSTALL_DIR}/logs/nginx.pid
ExecStartPre=/bin/rm -f ${NGINX_INSTALL_DIR}/logs/nginx.pid
ExecStartPre=${NGINX_INSTALL_DIR}/sbin/nginx -t
ExecStart=${NGINX_INSTALL_DIR}/sbin/nginx
ExecReload=/bin/kill -s HUP \$MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF
    ${COLOR}"开始设置Nginx配置文件"${END}
    [ -d /data/www ] || mkdir -pv /data/www/ &> /dev/null 
    cat > /apps/nginx/conf/nginx.conf <<EOF
worker_processes  auto;
events {
    worker_connections  10240;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    server_tokens off;
    log_format  main  '\$remote_addr - \$remote_user [\$time_local] "\$request" '
    sendfile        on;
    client_max_body_size 100m;
    keepalive_timeout  65;
    server {
        listen       80 default_server;
        server_name  ${SERVERNAME}; 
        root /data/www ;
        access_log  logs/nginx.access.log  main;
        location / {
            root   /data/www/;
            index  index.php index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        location ~ \.php$ {
            root           /data/www;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  \$document_root\$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
}
EOF
    systemctl daemon-reload
    systemctl enable --now nginx &> /dev/null 
    systemctl is-active nginx &> /dev/null ||  { ${COLOR}"NGINX 启动失败,退出!"${END} ; exit; }
    ${COLOR}"NGINX安装完成"${END}
}

install_php (){
    ${COLOR}"开始安装PHP"${END}
    ${COLOR}"开始安装PHP依赖包"${END}
    if [[ ${OS_RELEASE_VERSION} == 8 ]] &> /dev/null;then
    cat > /etc/yum.repos.d/PowerTools.repo <<-EOF
[PowerTools]
name=PowerTools
baseurl=https://mirrors.aliyun.com/centos/8/PowerTools/x86_64/os/
        https://mirrors.huaweicloud.com/centos/8/PowerTools/x86_64/os/
        https://mirrors.cloud.tencent.com/centos/8/PowerTools/x86_64/os/
        https://mirrors.tuna.tsinghua.edu.cn/centos/8/PowerTools/x86_64/os/
        http://mirrors.163.com/centos/8/PowerTools/x86_64/os/
        http://mirrors.sohu.com/centos/8/PowerTools/x86_64/os/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
EOF
    fi
    if [ ${OS_ID} == "CentOS" ] &> /dev/null;then
        yum -y install gcc make libxml2-devel bzip2-devel libmcrypt-devel sqlite-devel oniguruma-devel &> /dev/null
    else
        apt update &> /dev/null;apt -y install gcc make openssl pkg-config libssl-dev libsqlite3-dev libonig-dev curl libbz2-dev libxml2-dev libjpeg-dev libpng-dev libfreetype6-dev libzip-dev &> /dev/null 
    fi
    cd ${SRC_DIR}
    tar xf ${PHP}
    PHP_DIR=`echo ${PHP}| sed -nr 's/^(.*[0-9]).*/\1/p'`
    cd ${PHP_DIR}
    ./configure --prefix=${PHP_INSTALL_DIR} --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-openssl --with-zlib --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --enable-mbstring --enable-xml --enable-sockets --enable-fpm --enable-maintainer-zts --disable-fileinfo
    make -j ${CPUS} && make install
    [ $? -eq 0 ] && ${COLOR}"PHP编译安装成功"${END} ||  { ${COLOR}"PHP编译安装失败,退出!"${END};exit; }
    echo 'PATH=$PHP_INSTALL_DIR/bin:$PATH' >/etc/profile.d/lamp.sh
    . /etc/profile.d/lamp.sh
    cp php.ini-production  /etc/php.ini
    cp sapi/fpm/php-fpm.service /lib/systemd/system/
    cd ${PHP_INSTALL_DIR}/etc
    cp php-fpm.conf.default  php-fpm.conf
    cd php-fpm.d/
    cp www.conf.default www.conf
    sed -i.bak -e 's/^user.*/user = nginx/' -e 's/^group.*/group = nginx/' -e 's/#pm.status_path =.*/pm.status_path = \/fpm_status/' -e 's/#ping.path =.*/ping.path = \/ping/' ${PHP_INSTALL_DIR}/etc/php-fpm.d/www.conf
    mkdir /etc/php.d/
    cat > /etc/php.d/opcache.ini <<-EOF
[opcache]
zend_extension=opcache.so
opcache.enable=1
EOF
    systemctl daemon-reload
    systemctl enable --now php-fpm &> /dev/null 
    systemctl is-active  php-fpm &> /dev/null ||  { ${COLOR}"PHP-FPM 启动失败,退出!"${END} ; exit; }
    $COLOR"PHP安装完成"${END}
}

install_phpmyadmin(){
    ${COLOR}"开始安装PHPMYADMIN"${END}
    cd ${SRC_DIR}
    rpm -q unzip &> /dev/null || yum -y install unzip &> /dev/null
    unzip ${APP} &> /dev/null
    [ -d ${WEB_DIR} ] || mkdir -p ${WEB_DIR}
    mv phpMyAdmin-5.1.0-all-languages/* ${WEB_DIR}/
    yum -y install php-json php-xml &> /dev/null
    systemctl restart php-fpm
    cd ${WEB_DIR}
    mv config.sample.inc.php config.inc.php
    sed  -i.bak -e 's/localhost/127.0.0.1/' -e '16c\$cfg['blowfish_secret'] = 'a1b2c3000111222333aaabbbccc00012';' config.inc.php
    mkdir ${WEB_DIR}/tmp
    chmod 777 ${WEB_DIR}/tmp
    ${COLOR}"PHPMYADMIN安装完成"${END}
}

main(){
    os
    check_file
    install_mysql
    install_nginx
    install_php
    install_phpmyadmin
}

main
posted @ 2021-03-18 01:14  网络之鹰  阅读(833)  评论(0编辑  收藏  举报