通过编写 shell 脚本实现二进制安装 LAMP 架构的 word press

#!/bin/bash
#
#********************************************************************
#Author:            rzx
#QQ:                970707452
#Date:              2020-10-31
#FileName:         auto_install_lamp.sh
#URL:               https://www.cnblogs.com/rzx-006/
#Description:      The test script
#Copyright (C):    2020 All rights reserved
#********************************************************************
source /etc/init.d/functions
HTTP=https://mirrors.tuna.tsinghua.edu.cn/apache/httpd/httpd-2.4.46.tar.bz2
HTTPDIR=httpd-2.4.46
HTTPPATH=/app/httpd
APR=https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-1.7.0.tar.bz2
APRDIR=apr-1.7.0
APRPATH=/app/apr
APRUTIL=https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-util-1.6.1.tar.bz2
APRUTILDIR=apr-util-1.6.1
APRUTILPATH=/app/apr-util
COM=.tar.bz2
DIR=/usr/local/src
PHP=https://www.php.net/distributions/php-7.4.12.tar.xz
PHPDIR=php-7.4.12
PHPCOM=.tar.xz
PHPPATH=/app/php
DBDIR=/usr/local
ID=mysql
VERSION=mariadb-10.5.5-linux-systemd-x86_64
PACKAGE=https://mirrors.tuna.tsinghua.edu.cn/mariadb//mariadb-10.5.5/bintar-linux-systemd-x86_64/mariadb-10.5.5-linux-systemd-x86_64.tar.gz
MYDIR=/data/mysql
WORDPRESS=https://cn.wordpress.org/latest-zh_CN.tar.gz
WORDPRESSFILE=${WORDPRESS##*/}
WORDPRESSDIR=wordpress
WORDPRESSPATH=/app/$WORDPRESSDIR

DBPATH(){
ln -s /usr/local/mysql/bin/* /usr/bin/
if [ -f /etc/profile.d/mysql.sh ];then
    . /etc/profile.d/mysql.sh
else    
    echo PATH=$DBDIR/mysql/bin:$PATH > /etc/profile.d/mysql.sh
    . /etc/profile.d/mysql.sh
fi
}

INSTALLDB(){
    DBPATH
    cat > /etc/my.cnf <<EOF
[mysqld]
datadir=$MYDIR
skip_name_resolve=1    
log-error=$MYDIR/mysql.log
pid-file=$MYDIR/mysql.pid
EOF
    mkdir -p $MYDIR
    chown -R mysql.mysql $MYDIR
    $DBDIR/mysql/scripts/mysql_install_db --datadir=$MYDIR --user=mysql &> /dev/null
}

SERVERVICE(){
    cp $DBDIR/mysql/support-files/systemd/mariadb.service /usr/lib/systemd/system/
    systemctl daemon-reload
    systemctl enable --now mariadb &> /dev/null
    cd ~
}

SECURE(){

   expect &> /dev/null <<EOF

spawn mysql_secure_installation

expect {
        "Enter current password for root" { send "\n";exp_continue }
        "Switch to unix_socket authentication" { send "no\n";exp_continue }
        "Change the root password" { send "y\n";exp_continue }
        "New password" { send "123456\n";exp_continue } 
        "Re-enter new password" { send "123456\n";exp_continue } 
        "Remove anonymous users" { send "y\n";exp_continue } 
        "Disallow root login remotely" { send "y\n";exp_continue } 
        "Remove test database and access to it" { send "y\n";exp_continue } 
        "Reload privilege tables now" { send "y\n" } 
}
expect eof 

EOF

}

INHTTP(){
    tar xf $HTTPDIR$COM -C /usr/local/src
    cd $DIR/$HTTPDIR
    mkdir -p $HTTPPATH
    $DIR/$HTTPDIR/configure --prefix=$HTTPPATH --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-apr=$APRPATH/ --with-apr-util=$APRUTILPATH/ --enable-modules=most -enable-mpms-shared=all --with-mpm=event &> /dev/null
    make -j 4 &> /tmp/make.log || echo -e "\e[1;31mERROR  编译失败,请查看/tmp/make.log查询失败原因! \e[0m" && make install &> /dev/null
    useradd -r -s /sbin/nologin -d $HTTPPATH -c Apache -u 48 apache
    setfacl -R -m u:apache:rwx $HTTPPATH
    sed -i.bak 's/^User daemon/User apache/' $HTTPPATH/conf/httpd.conf
    sed -i.bak 's/^Group daemon/Group apache/' $HTTPPATH/conf/httpd.conf
    ln -s $HTTPPATH/bin/* /usr/bin/
    if [ `sed -nr 's/.*release ([0-9])\..*/\1/p' /etc/redhat-release` -gt 6 ];then
        cat > /usr/lib/systemd/system/httpd.service <<EOF
[Unit]
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target
Documentation=man:httpd(8)
Documentation=man:apachectl(8)
[Service]
Type=forking
#EnvironmentFile=/etc/sysconfig/httpd
ExecStart=$HTTPPATH/bin/apachectl start
#ExecStart=$HTTPPATH/bin/httpd \$OPTIONS -k start
ExecReload=$HTTPPATH/bin/apachectl graceful
#ExecReload=$HTTPPATH/bin/httpd \$OPTIONS -k graceful
ExecStop=$HTTPPATH/bin/apachectl stop
KillSignal=SIGCONT
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
        systemctl daemon-reload
        systemctl enable --now httpd &> /dev/null
    else
        cat > /etc/rc.d/init.d/httpd <<EOF
#!/bin/bash
#
# httpd        Startup script for the Apache HTTP Server
#
# chkconfig: - 85 15
# description: The Apache HTTP Server is an efficient and extensible  \
#          server implementing the current HTTP standards.
# processname: httpd
# config: /etc/httpd/conf/httpd.conf
# config: /etc/sysconfig/httpd
# pidfile: /var/run/httpd/httpd.pid
#
### BEGIN INIT INFO
# Provides: httpd
# Required-Start: \$local_fs \$remote_fs \$network \$named
# Required-Stop: \$local_fs \$remote_fs \$network
# Should-Start: distcache
# Short-Description: start and stop Apache HTTP Server
# Description: The Apache HTTP Server is an extensible server 
#  implementing the current HTTP standards.
### END INIT INFO

# Source function library.
. /etc/rc.d/init.d/functions

if [ -f /etc/sysconfig/httpd ]; then
        . /etc/sysconfig/httpd
fi

# Start httpd in the C locale by default.
HTTPD_LANG=\${HTTPD_LANG-"C"}

# This will prevent initlog from swallowing up a pass-phrase prompt if
# mod_ssl needs a pass-phrase from the user.
INITLOG_ARGS=""

# Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server
# with the thread-based "worker" MPM; BE WARNED that some modules may not
# work correctly with a thread-based MPM; notably PHP will refuse to start.

# Path to the apachectl script, server binary, and short-form for messages.
apachectl=/usr/sbin/apachectl
httpd=\${HTTPD-/usr/sbin/httpd}
prog=httpd
pidfile=\${PIDFILE-/var/run/httpd/httpd.pid}
lockfile=\${LOCKFILE-/var/lock/subsys/httpd}
RETVAL=0
STOP_TIMEOUT=\${STOP_TIMEOUT-10}

# The semantics of these two functions differ from the way apachectl does
# things -- attempting to start while running is a failure, and shutdown
# when not running is also a failure.  So we just do it the way init scripts
# are expected to behave here.
start() {
        echo -n \$"Starting \$prog: "
        LANG=\$HTTPD_LANG daemon --pidfile=\${pidfile} \$httpd \$OPTIONS
        RETVAL=\$?
        echo
        [ \$RETVAL = 0 ] && touch \${lockfile}
        return \$RETVAL
}

# When stopping httpd, a delay (of default 10 second) is required
# before SIGKILLing the httpd parent; this gives enough time for the
# httpd parent to SIGKILL any errant children.
stop() {
    status -p \${pidfile} \$httpd > /dev/null
    if [[ \$? = 0 ]]; then
        echo -n \$"Stopping \$prog: "
        killproc -p \${pidfile} -d \${STOP_TIMEOUT} \$httpd
    else
        echo -n \$"Stopping \$prog: "
        success
    fi
    RETVAL=\$?
    echo
    [ \$RETVAL = 0 ] && rm -f \${lockfile} \${pidfile}
}

reload() {
    echo -n \$"Reloading \$prog: "
    if ! LANG=\$HTTPD_LANG \$httpd \$OPTIONS -t >&/dev/null; then
        RETVAL=6
        echo \$"not reloading due to configuration syntax error"
        failure \$"not reloading \$httpd due to configuration syntax error"
    else
        # Force LSB behaviour from killproc
        LSB=1 killproc -p \${pidfile} \$httpd -HUP
        RETVAL=\$?
        if [ \$RETVAL -eq 7 ]; then
            failure \$"httpd shutdown"
        fi
    fi
    echo
}

# See how we were called.
case "\$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  status)
        status -p \${pidfile} \$httpd
    RETVAL=\$?
    ;;
  restart)
    stop
    start
    ;;
  condrestart|try-restart)
    if status -p \${pidfile} \$httpd >&/dev/null; then
        stop
        start
    fi
    ;;
  force-reload|reload)
        reload
    ;;
  graceful|help|configtest|fullstatus)
    \$apachectl \$@
    RETVAL=\$?
    ;;
  *)
    echo \$"Usage: \$prog {start|stop|restart|condrestart|try-restart|force-reload|reload|status|fullstatus|graceful|help|configtest}"
    RETVAL=2
esac

exit \$RETVAL
EOF
        cd ~
        ln -s $HTTPPATH/bin/* /usr/sbin/
        chmod +x /etc/rc.d/init.d/httpd
        chkconfig --add httpd
        service httpd start &> /dev/null
    fi
}
INAPR(){
    tar xf $APRDIR$COM -C /usr/local/src
    cd $DIR/$APRDIR
    mkdir -p $APRPATH
    $DIR/$APRDIR/configure --prefix=$APRPATH &> /dev/null
    make -j 4 &> /dev/null && make install &> /dev/null
    echo -e "\e[1;32mApr软件包安装成功\e[0m"
    cd ~
}
INAPRUTIL(){
    tar xf $APRUTILDIR$COM -C /usr/local/src
    cd $DIR/$APRUTILDIR
    mkdir -p $APRUTILPATH
    $DIR/$APRUTILDIR/configure --prefix=$APRUTILPATH --with-apr=$APRPATH/ &> /dev/null
    make -j 4 &> /dev/null && make install &> /dev/null
    echo -e "\e[1;32mApr软件包安装成功\e[0m"
    cd ~
}
INSPHP(){
tar xf $PHPDIR$PHPCOM -C $DIR
cd $DIR/$PHPDIR
$DIR/$PHPDIR/configure \
--prefix=$PHPPATH \
--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 &> /tmp/conf.log
make -j 4 &> /tmp/php.log && make install &> /dev/null
ln -s $PHPPATH/bin/* /usr/local/sbin/ &> /dev/null
cd ~
}
tarwordpress(){
    tar xf $WORDPRESSFILE -C $DIR
    cp -r $DIR/$WORDPRESSDIR $HTTPPATH/htdocs/
    setfacl -R -m u:apache:rwx $HTTPPATH/htdocs/$WORDPRESSDIR
    rm -rf /app/httpd/htdocs/index.html
}

#安装编译安装apache的依赖包
yum config-manager --set-enabled PowerTools &> /dev/null
yum -y install bzip2 wget gcc make pcre-devel openssl-devel expat-devel autoconf libaio perl-Data-Dumper ncurses-compat-libs expect libxml2-devel bzip2-devel libmcrypt-devel sqlite-devel oniguruma-devel &> /dev/null

#编译安装apr
if ! [ -d $APRPATH ] &> /dev/null ;then
    echo -e "\e[1;32m正在安装apr,请稍后...\e[0m"
    if [ -e $APRDIR$COM ] ;then
        INAPR
    else
        wget $APR &> /dev/null
        INAPR
    fi
else
     echo -e '\e[1;31mapr 已安装,跳过apr安装步骤\e[0m'
fi
#编译安装apr-util
if ! [ -d $APRUTILPATH ] &> /dev/null ;then
    echo -e "\e[1;32m正在安装apr-util,请稍后...\e[0m"
    if [ -e $APRUTILDIR$COM ] ;then
        INAPRUTIL
    else
        wget $APRUTIL &> /dev/null
        INAPRUTIL
    fi
else
     echo -e '\e[1;31mapr-util 已安装,跳过apr-util安装步骤\e[0m'
fi
#编译安装http
if ! [ -d $HTTPPATH ] &> /dev/null ;then
    echo -e "\e[1;32m正在安装http,请稍后...\e[0m"
    if [ -e $HTTPDIR$COM ] ;then
        INHTTP
        echo -e "\e[1;32mapache 已成功安装apache\e[0m"
    else
        wget $HTTP &> /dev/null
        INHTTP
        echo -e "\e[1;32mapache 已成功安装apache\e[0m"
    fi
else
    echo -e '\e[1;31mapache 已安装,请勿重复安装\e[0m'
fi

#二进制源码包安装mariadb-10.5.5

echo -e "\e[1;32m正在安装mariadb,请稍后...\e[0m"
if `id $ID &> /dev/null`;then
    echo '用户已存在,跳过用户创建过程'
else   
    groupadd $ID
    useradd -r -g $ID -s /sbin/false $ID
fi

if [ -h $DBDIR/mysql ];then
    echo '数据库已安装,勿重复安装'  
    exit
else    
    if [ ! -f $VERSION.tar.gz ];then
        curl $PACKAGE -O /root &> /dev/null   
    else     
        echo '二进制包已存在,跳过下载源码包步骤'    
    fi    
    if [ ! -d $DBDIR/$VERSION ];then
        tar -xf $VERSION.tar.gz -C $DBDIR
        ln -s $DBDIR/$VERSION $DBDIR/mysql
        chown -R mysql.mysql $DBDIR/mysql/
        INSTALLDB
        SERVERVICE
        SECURE && action "数据库已安装并安全初始化成功"
    else       
        ln -s $DBDIR/$VERSION $DBDIR/mysql
        chown -R mysql.mysql $DBDIR/mysql/
        INSTALLDB
        SERVERVICE
        SECURE && action "数据库已安装并安全初始化成功"
    fi
fi

mysql -uroot -e 'create database wordpress'
mysql -uroot -e "grant all on wordpress.* to wordpress@'127.0.0.1' identified by '123456'"

#编译安装PHP

if [ -f $PHPDIR$PHPCOM ];then
    echo -e "\e[1;32m正在安装PHP,请稍后...\e[0m"
    INSPHP
    echo -e "\e[1;32mPHP安装成功\e[0m"
else
    echo -e "\e[1;32m正在安装PHP,请稍后...\e[0m"
    wget -t 0 -c $PHP &> /dev/null
    INSPHP
    echo -e "\e[1;32mPHP安装成功\e[0m"
fi

cp $DIR/$PHPDIR/php.ini-production /etc/php.ini
cp $DIR/$PHPDIR/sapi/fpm/php-fpm.service /usr/lib/systemd/system/
cp $PHPPATH/etc/php-fpm.conf.default $PHPPATH/etc/php-fpm.conf
cp $PHPPATH/etc/php-fpm.d/www.conf.default $PHPPATH/etc/php-fpm.d/www.conf
#修改进程所有者
sed -ri.bak 's/group = nobody/group = apache/' $PHPPATH/etc/php-fpm.d/www.conf
sed -ri.bak 's/user = nobody/user = apache/' $PHPPATH/etc/php-fpm.d/www.conf
#支持status和ping页面
sed -ri.bak 's/;(ping.path.*)/\1/' $PHPPATH/etc/php-fpm.d/www.conf
sed -ri.bak 's/;pm.status_path = \/status/pm.status_path = \/fpm_status/' $PHPPATH/etc/php-fpm.d/www.conf
#支持opcache加速
mkdir /etc/php.d
cat > /etc/php.d/opacche.ini <<EOF
[opcache]
zend_extension=opcache.so
opcache.enable=1
EOF
systemctl daemon-reload
systemctl enable --now php-fpm.service &> /dev/null

sed -ri.bak 's/#(LoadModule proxy_mod.*)/\1/' $HTTPPATH/conf/httpd.conf
sed -ri.bak 's/#(LoadModule proxy_fc.*)/\1/' $HTTPPATH/conf/httpd.conf

sed -ri.bak 's/DirectoryIndex index.html/DirectoryIndex index.php index.html/p' $HTTPPATH/conf/httpd.conf
echo -e AddType application/x-httpd-php .php\n#AddType application/x-httpd-php-source .phps\nProxyRequests Off >> $HTTPPATH/conf/httpd.conf
#利用虚拟主机实现访问
cat >> $HTTPPATH/conf/httpd.conf <<EOF
<virtualhost *:80>
servername blog.rzx.org
documentroot $HTTPPATH/htdocs/wordpress
<directory $HTTPPATH/htdocs/wordpress>
require all granted
</directory>
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000$HTTPPATH/htdocs/wordpress/$1
#实现status和ping页面
ProxyPassMatch ^/(fpm_status|ping)$ fcgi://127.0.0.1:9000/$1
CustomLog "logs/access_wordpress_log" common
</virtualhost>
EOF
systemctl restart httpd

if [ -f $WORDPRESSFILE ];then
    tarwordpress
else
    wget $WORDPRESS
    tarwordpress
fi

echo -e "\e[1;32mapache 已成功安装wordpress,请通过浏览器访问http://`hostname -I`访问网站\e[0m"

作者博客https://blog.csdn.net/qq_41811454/article/details/109432115