shell 脚本实现二进制安装 LAMP 架构的 wordpress

#!/bin/bash
#
#***********************************************************************
#Author:                yangxiaohao
#QQ:                    1443964024
#Date:                  2020-10-31
#FileName:              install_LAMP.sh
#Description:           The test script
#Copyright(C):         2020All rights reserved
#**********************************************************************
SRC_DIR=/usr/local/src
NAME="mariadb-10.2.31-linux-x86_64.tar.gz"
MYSQL="mariadb-10.2.31-linux-x86_64"
FULL_NAME=${SRC_DIR}/${NAME}
DATA_DIR="/data/mysql"
RURPLE="\033[1;35m"
RED="\033[1;31m"
END="\033[0m"
HTTPD='httpd-2.4.43.tar.bz2'
APR='apr-1.7.0.tar.bz2 '
APRU='apr-util-1.6.1.tar.gz'
WORDPRESS='wordpress-5.4.2-zh_CN.tar.gz'
PHP='php-7.4.7.tar.bz2'

echo -e " $RURPLE 开始安装基于LAMP的wordpress $END"
sleep 2

check_file(){
    cd $SRC_DIR
    echo -e "$RED 请将相关软件放在${SRC_DIR}目录下 $END"
if [ ! -e ${HTTPD} ];then
  echo -e "$RED 缺少 ${HTTPD}文件 $END"
        exit
elif [ ! -e ${APR} ];then
  echo -e "$RED 缺少 ${APR}文件  $END"
        exit
elif [ ! -e ${APRU} ];then
  echo -e $RED "缺少 ${APRU}文件 $END"
        exit
elif [ ! -e ${PHP} ];then
  echo -e "$RED 缺少 ${PHP}文件 $END"
        exit
elif [ ! -e ${WORDPRESS} ];then
  echo -e "$RED缺少 ${WORDPRESS}文件 $END"
    exit
elif [ ! -e ${NAME} ];then
  echo -e "$RED 缺少 ${NAME}文件 $END"
    exit
else
  echo -e " $RURPLE 相关文件已经准备好 $END"
fi
}

install_mysql(){
 echo -e " $RED 开始安装MYSQL数据库$END"
  echo -e "$RURPLE 开始安装有关安装包,请耐心等候... $END"
yum -y install wget libaio numactl-libs  &> /dev/null
echo -e  "$RURPLE 安装包下载完毕! $END"
sleep 2

echo -e "$RURPLE 填写配置文件my.cnf  $END"
cat > /etc/my.cnf <<EOF
[mysqld]
datadir=/data/mysql
skip_name_resolve=1
socket=/data/mysql/mysql.sock
log-error=/data/mysql/mysql.log
pid-file=/data/mysql/mysql.pid

[client]
socket=/data/mysql/mysql.sock
EOF
echo -e "$RURPLE 配置文件编写完成!$END"

if [ -f ${FULL_NAME} ];then
    echo -e "$RURPLE 安装文件存在$END"
else
    echo -e "$RURPLE 安装文件不存在 $END"
    exit 1
fi
if [ -h /usr/local/mysql ];then
    echo -e "$RURPLE mysql 已经安装! $END"
    exit 2
else
    tar xf ${FULL_NAME} -C /usr/local/
    ln -s /usr/local/${MYSQL}/ /usr/local/mysql &> /dev/null
    if id mysql;then
        echo -e "$RURPLE mysql 用户已经存在! $END"
    else
        groupadd mysql
        useradd -r -g mysql -s /bin/false mysql
    fi

    if id mysql;then
        chown -R root.root /usr/local/mysql/
        if [ ! -d /data/mysql ];then
            mkdir -p /data/mysql && chown -R mysql.mysql /data/mysql &> /dev/null
            /usr/local/mysql/scripts/mysql_install_db --user=mysql --datadir=/data/mysql --basedir=/usr/local/mysql/ &> /dev/null
           cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld &> /dev/null
            chmod +x /etc/init.d/mysqld
            ln -s /usr/local/mysql/bin/* /usr/bin/ &> /dev/null
            chkconfig --add mysqld
        service mysqld start
        else
            echo -e " $RURPLE  mysql数据目录已经存在 $END"
            exit 3
        fi
    fi
fi
mysql -u root -e "create database wordpress;
                  create user wordpress@'10.0.0.7' identified by 'magedu';
                  grant all on wordpres.* to wordpress@'10.0.0.7';"
echo -e "$RED 数据库安装配置完毕 $END"
}

install_httpd(){
   echo -e "$RED 开始安装apache服务 $END"
    yum -y install lrzsz gcc pcre-devel openssl-devel expat-devel bzip2
    cd ${SRC_DIR}
    tar xvf ${APR}
    tar xvf ${APRU}
    tar xvf ${HTTPD}
   echo -e "$RED 进行合并编译,请等候..... $END"
    mv apr-1.7.0 httpd-2.4.43/srclib/apr
    mv apr-util-1.6.1 httpd-2.4.43/srclib/apr-util
     cd httpd-2.4.43/
./configure --prefix=/apps/httpd24 \
 --enable-so \
 --enable-ssl \
 --enable-cgi \
 --enable-rewrite \
 --with-zlib \
 --with-pcre \
 --with-included-apr \
 --enable-modules=most \
 --enable-mpms-shared=all \
 --with-mpm=event    
make && make install
[ $? -eq 0 ] && echo -e "$RED apache编译安装成功 $END"  || { echo -e "$RED apache编译安装失败!退出$END";exit;}
 echo 'PATH=/apps/httpd24/bin:$PATH' > /etc/profile.d/lamp.sh   
. /etc/profile.d/lamp.sh
useradd -s /sbin/nologin -r -u 88 apache  
sed -i.bak  -e  's/^User.*/User  apache/' -e 's/^Group.*/Group  apache/'   /apps/httpd24/conf/httpd.conf
sed -i 's/^#LoadModule proxy_module modules\/mod_proxy.so/LoadModule proxy_module modules\/mod_proxy.so/' /apps/httpd24/conf/httpd.conf
sed -i 's/^#LoadModule proxy_fcgi_module modules\/mod_proxy_fcgi.so/LoadModule proxy_fcgi_module modules\/mod_proxy_fcgi.so/' /apps/httpd24/conf/httpd.conf
sed -i 's/DirectoryIndex index.html/DirectoryIndex index.php index.html/' /apps/httpd24/conf/httpd.conf
apachectl start
cat > /usr/lib/systemd/system/httpd.server<<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=/apps/httpd24/bin/apachectl start
#ExecStart=/apps/httpd24/bin/httpd $OPTIONS -k start
ExecReload=/apps/httpd24/bin/apachectl graceful
#ExecReload=/apps/httpd24/bin/httpd $OPTIONS -k graceful
ExecStop=/apps/httpd24/bin/apachectl stop
KillSignal=SIGCONT
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF

echo -e "
AddType application/x-httpd-php .php
#AddType application/x-httpd-php-source .phps
ProxyRequests Off
<virtualhost *:80>
servername 10.0.0.7
documentroot /data/wordpress
<directory /data/wordpress>
require all granted
</directory>
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/data/wordpress/$1
ProxyPassMatch ^/(fpm_status|ping)$ fcgi://127.0.0.1:9000/$1
CustomLog "logs/access_wordpress_log" common
</virtualhost>
" >>  /apps/httpd24/conf/httpd.conf

apachectl restart
echo -e "$RED apache服务安装完毕! $END"

}

install_php(){
   echo -e " ${RED} 开始安装PHP $END"
    yum -y install gcc libxml2-devel bzip2-devel libmcrypt-devel sqlite-devel oniguruma-devel openssl-devel
    cd ${SRC_DIR}
    tar xf ${PHP}
    cd php-7.4.7
./configure \
 --prefix=/apps/php\
 --enable-mysqlnd \
 --with-mysqli=mysqlnd \
 --with-pdo-mysql=mysqlnd \
 --with-openssl \
 --with-zlib \
 --with-config-file-path=/etc \
 --with-config-file-dir=/etc/php.d \
 --enable-mbstring \
 --enable-xml \
 --enable-sockets \
--enable-fpm \
 --enable-maintainer-zts \
 --disable-fileinfo
make && make install
[ $? -eq 0 ]  && echo -e "$REDPHP编译安装成功!$END" || { echo -e  " $RED PHP编译安装失败,退出!$END";exit;}
echo 'PATH=/apps/php/bin:/apps/httpd24/bin:$PATH' > /etc/profile.d/php74.sh
. /etc/profile.d/php74.sh
cp php.ini-production /etc/php.ini  
cp sapi/fpm/php-fpm.service /usr/lib/systemd/system/
 cd /apps/php/etc/
cp php-fpm.conf.default php-fpm.conf
cp /apps/php/etc/php-fpm.d/www.conf.default /apps/php/etc/php-fpm.d/www.conf
sed -i.bak  -e  's/^user.*/user = apache/' -e 's/^group.*/group = apache/' -e 's/;pm.status_path = /status/pm.status_path = /status/' -e 's/;ping.path = /ping /ping.path = /ping /' /apps/php/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.service
mkdir /data/wordpress
echo -e "$RED PHP安装完成!$END"
}
install_wordpress(){
    cd ${SRC_DIR}
    tar xf ${WORDPRESS} -C /data/
    setfacl -R -m u:apache:rwx /data/wordpress/
   echo -e " $RURPLE WORDPRESS安装完成!!$END"
}

check_file
install_mysql
install_httpd
install_php
install_wordpress

posted @ 2020-11-01 19:31  肖豪  阅读(83)  评论(0编辑  收藏  举报