2020-09-12

通过编写 shell 脚本实现一键安装 LAMP 架构的 word press

#!/bin/bash

DIR=/usr/local/src
NGINX="nginx-1.16.1.tar.gz"
HTTP="httpd-2.4.43.tar.bz2"
APR="apr-1.7.0.tar.bz2"
APRUTIL="apr-util-1.6.1.tar.gz"
MYSQL="mysql-5.7.29-el7-x86_64.tar.gz"
PHP="php-7.4.3.tar.xz"
APP="wordpress-5.4.2-zh_CN.tar.gz"
CPU=`lscpu | awk  '/^CPU\(/{print $NF}'`
MYSQL_PASSWD=centos
MYSQL_WORDPRESS_PASSWD=centos
COLOR='echo -e \e[1;32m'
END='\e[0m'

check_file (){
cd  $DIR
$COLOR"请将相关软件放在${DIR}目录下"$END
if  [ ! -e $PHP ];then
        $COLOR"缺少${PHP}文件"$END
        exit
elif [ ! -e $HTTP ];then
        $COLOR"缺少${HTTP}文件"$END
        exit
elif [ !  -e $APR ];then
        $COLOR"缺少${APR}文件"$END
        exit
elif [ !  -e $APRUTIL ];then
        $COLOR"缺少${APRUTIL}文件"$END
        exit
elif [ !  -e $MYSQL ];then
        $COLOR"缺少${MYSQL}文件"$END
        exit
#elif [ ! -e $NGINX ];then

#        $COLOR"缺少${NGINX}文件"$END

#        exit

elif [ ! -e $APP ];then
        $COLOR"缺少${APP}文件"$END
        exit
else
    $COLOR"相关文件已准备好"$END
fi
}

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

install_mysql(){
    $COLOR"开始安装MySQL数据库"$END
    cd $DIR
    tar xf $MYSQL -C /usr/local/
    if [ -e /usr/local/mysql ];then
        $COLOR"数据库已存在,安装失败"$END
        exit
    fi
    MYSQL_DIR=`echo $MYSQL| sed -nr 's/^(.*[0-9]).*/\1/p'`
    ln -s  /usr/local/$MYSQL_DIR /usr/local/mysql
    chown -R  root.root /usr/local/mysql/
    id mysql &> /dev/null || { useradd -s /sbin/nologin -r  mysql ; $COLOR"创建mysql用户"$END; }
    yum  -y -q install  deltarpm numactl-libs   libaio &> /dev/null

    echo 'PATH=/usr/local/mysql/bin/:$PATH' > /etc/profile.d/lamp.sh
    
    .  /etc/profile.d/lamp.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 ] || mkdir /data
    mysqld --initialize --user=mysql --datadir=/data/mysql 
    cp /usr/local/mysql/support-files/mysql.server  /etc/init.d/mysqld
    chkconfig --add mysqld
    chkconfig mysqld on
    service mysqld start
    [ $? -ne 0 ] && { $COLOR"数据库启动失败,退出!"$END;exit; }
    MYSQL_OLDPASSWORD=`awk '/A temporary password/{print $NF}' /data/mysql/mysql.log`
    mysqladmin  -uroot -p$MYSQL_OLDPASSWORD password $MYSQL_PASSWD &>/dev/null
    $COLOR"数据库安装完成"$END
}

install_httpd() {
    ${COLOR}"开始安装HTTP"$END
    id apache &> /dev/null || { useradd -s /sbin/nologin -r apache ; $COLOR"创建Apache用户"$END; } 
    $COLOR"安装HTTP相关包"$END
    yum  -y install gcc make deltarpm expat-devel libcurl-devel pcre-devel openssl-devel expat-devel  lbzip2      
    cd $DIR
    tar xf $HTTP
    tar xf $APR  
    tar xf $APRUTIL
    APR_DIR=`echo $APR | sed -nr 's/^(.*[0-9]).*[0-9]/\1/p'`
    APRUTIL_DIR=`echo $APRUTIL | sed -nr 's/^(.*[0-9]).*/\1/p'`
    HTTP_DIR=`echo $HTTP | sed -nr 's/^(.*[0-9]).*[0-9]/\1/p'`
    mv $APR_DIR  $HTTP_DIR/srclib/apr
    mv $APRUTIL_DIR  $HTTP_DIR/srclib/apr-util

    cd  $HTTP_DIR
    ./configure --prefix=/apps/httpd --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 -j $CPU  &> /dev/null && make install &> /dev/null
    [ $? -eq 0 ] && $COLOR"HTTP编译安装成功"$END || { $COLOR"HTTP编译安装失败"$END ; exit; }
    
    sed -ri.bak -e  's/^(Group) .*/\1 apache/' -e 's/^(User) .*/\1 apache/' /apps/httpd/conf/httpd.conf
    sed -ri -e 's/^#(.*mod_proxy.so)$/\1/' -e 's/^#(.*proxy_fcgi.so)$/\1/' /apps/httpd/conf/httpd.conf 
    sed -ri 's/(DirectoryIndex) index.html/\1 index.php index.html/' /apps/httpd/conf/httpd.conf
    
    echo -e 'AddType application/x-httpd-php .php\nAddType application/x-httpd-php-source .phps\nProxyRequests Off\nProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/apps/httpd/htdocs/$1' >> /apps/httpd/conf/httpd.conf 
    
    ln -s /apps/httpd/bin/* /usr/bin/   
    
    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=/apps/httpd/bin/apachectl start
#ExecStart=/apps/httpd/bin/httpd \$OPTIONS -k start
ExecReload=/apps/httpd/bin/apachectl graceful
#ExecReload=/apps/httpd/bin/httpd \$OPTIONS -k graceful
ExecStop=/apps/httpd24/bin/apachectl stop
KillSignal=SIGCONT
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF

    systemctl daemon-reload 
    systemctl enable --now httpd.service   &> /dev/null
    [ $? -eq 0 ] || { $COLOR"HTTP启动失败,退出!"$END ; exit; }
    $COLOR"HTTP安装完成"$END         

}

install_php() {
    ${COLOR}"开始安装PHP"$END
    yum -y -q  install gcc make openssl-devel   libxml2-devel bzip2-devel libmcrypt-devel libsqlite3x-devel oniguruma-devel &> /dev/null
    cd $DIR
    tar xf $PHP
    PHP_DIR=`echo $PHP | sed -nr 's/^(.*[0-9]).*/\1/p'`
    cd $PHP_DIR
     ./configure --prefix=/apps/php74 --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 $CPU && make install 
    [ $? -eq 0 ] && $COLOR"PHP编译安装成功"$END ||  { $COLOR"PHP编译安装失败,退出!"$END;exit; }
    cp php.ini-production  /etc/php.ini
    mkdir /etc/php.d/
    cat > /etc/php.d/opcache.ini < /dev/null || { useradd -s /sbin/nologin -r  nginx; $COLOR"创建nginx用户"$END; }

#    sed -i.bak  -e  's/^user.*/user = nginx/' -e 's/^group.*/group = nginx/' /apps/php74/etc/php-fpm.d/www.conf

    id apache  &> /dev/null || { useradd -s /sbin/nologin -r  apache; $COLOR"创建apache用户"$END; }
    sed -i.bak  -e  's/^user.*/user = apache/' -e 's/^group.*/group = apache/' /apps/php74/etc/php-fpm.d/www.conf
    systemctl daemon-reload
    systemctl start php-fpm 
    systemctl is-active  php-fpm &> /dev/null ||  { $COLOR"PHP-FPM 启动失败,退出!"$END ; exit; }
    $COLOR"PHP安装完成"

}


install_wordpress() {
    cd $DIR
    tar xf $APP  
    mv wordpress/* /apps/httpd/htdocs/
    chown -R apache.apache /apps/httpd/htdocs/

#   chown -R nginx.nginx /apps/httpd/htdocs/

    cd /apps/httpd/htdocs/
    mv wp-config-sample.php wp-config.php
    mysql -uroot -p"$MYSQL_PASSWD" -e "create database wordpress;grant all on wordpress.* to wordpress@'127.0.0.1' identified by '$MYSQL_WORDPRESS_PASSWD'" &> /dev/null
    sed -i.bak -e 's/database_name_here/wordpress/' -e 's/username_here/wordpress/' -e 's/password_here/'''$MYSQL_WORDPRESS_PASSWD'''/' -e 's/localhost/127.0.0.1/'  wp-config.php
    $COLOR"WORDPRESS安装完成"

}

check_file
install_mysql
install_httpd
install_php
install_wordpress

你可能感兴趣的:(2020-09-12)