博客作业第七周-shell脚本一键安装LAMP架构的WordPress

一键安装LAMP架构的wordpress

在这里插入图片描述
工作过程:
1、当客户端请求的是静态资源时,web服务器会直接把静态资源返回给客户端;
2、当客户端请求的是动态资源时,httpd的php模块会进行相应的动态资源运算,如果此过程还需要数据库的数据作为运算参数时,php会连接mysql取得数据然后进行运算,运算的结果转为静态资源由web服务器返回到客户端;

WordPress是一种使用PHP语言开发的博客平台,用户可以在支持PHP和MySQL数据库的服务器
上架设属于自己的网站。也可把 WordPress当作一个内容管理系统(CMS)来使用,官网:http
s://cn.wordpress.org/

#!/bin/bash
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(){
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.gjz.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"

shell脚本二进制安装mariadb-10.5.5

#!/bin/bash
#
#********************************************************************
#Author:		gaojinzhou
#QQ: 			525184587
#Date: 			2020-09-25
#FileName:		mysql_install.sh
#URL: 			https://blog.csdn.net/GJZ23456789
#Description:		The test script
#Copyright (C): 	2020 All rights reserved
#********************************************************************

NAME=mariadb-10.5.5-linux-x86_64.tar.gz
DATA_DIR="/data/mysql"


#安装需要软件包

yum -y install libaio perl-Data-Dumper autoconf ncurses-compat-libs  > /dev/null

#创建mysql账户

if `id mysql &>/dev/null` ;then
    echo '该账户已存在'
else
    groupadd -r -g 306 mysql
    useradd -r -g 306 -u 306 -d /data/mysql mysql
fi

#准备数据目录

if [ -d $DATA_DIR  ];then
    echo '数据库已经安装'
else
    mkdir $DATA_DIR
    chown mysql:mysql $DATA_DIR
fi

#准备二进制程序
if [ -e $NAME ];then
    tar xf $NAME -C /usr/local
else
    echo '请先将数据库安装包移动到/root/目录下'
fi

cd /usr/local
ln -s mariadb-10.5.5-linux-x86_64  mysql
chown -R root:root /usr/local/mysql/

#准备配置文件

cat > /etc/my.cnf <<EOF
[mysqld]
datadir= $DATA_DIR
EOF

#创建数据库文件
cd /usr/local/mysql/
./scripts/mysql_install_db --datadir=/data/mysql  --user=mysql > /dev/null 

#准备服务脚本,并启动服务

if [ -e /usr/local/mysql/support-files/mysql.server ];then
    cp support-files/mysql.server  /etc/rc.d/init.d/mysqld
else
    echo '该文件不存在'
fi
chkconfig --add mysqld
service mysqld start

#PATH路径
echo 'PATH=/usr/local/mysql/bin:$PATH'> /etc/profile.d/mysql.sh 
. /etc/profile.d/mysql.sh

shell脚本编译安装http-2.4.46

#!/bin/bash
#
#********************************************************************
#Author:		gaojinzhou
#QQ: 			525184587
#Date: 			2020-10-27
#FileName:		httpd_src_install.sh
#URL: 			https://www.cnblogs.com/gaojinzhou/
#Description:		The test script
#Copyright (C): 	2020 All rights reserved
#********************************************************************
source /etc/init.d/functions
apr=apr-1.7.0.tar.bz2
apr_util=apr-util-1.6.1.tar.bz2
httpd=httpd-2.4.46.tar.bz2

#编译准备安装相关包
install(){
yum -y install gcc make pcre-devel openssl-devel expat-devel wget bzip2 &> /dev/null

if  [ -e $apr ];then action 'apr文件已存在'
else
wget https://downloads.apache.org//apr/apr-1.7.0.tar.bz2 &> /dev/null
fi

if [ -e $apr_util ];then action 'apr-util文件已存在'
else
wget https://downloads.apache.org//apr/apr-util-1.6.1.tar.bz2 &> /dev/null
fi

if [ -e $httpd ];then action 'httpd文件已存在'
else
wget https://downloads.apache.org//httpd/httpd-2.4.46.tar.bz2 &> /dev/null
fi

}

#解压软件包
install1(){
cd /root/
tar xf $apr
tar xf $apr_util
tar xf $httpd
mv `basename -s .tar.bz2 $apr` httpd-2.4.46/srclib/apr
mv `basename -s .tar.bz2 $apr_util` httpd-2.4.46/srclib/apr-util
action '软件包解压完成'
}

#开始编译
compile(){
cd /root/httpd-2.4.46/
./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=prefork &> /dev/null && make -j 4 &> /dev/null && make install &> /dev/null
action '编译完成'
}

#编译完成后配置
configure(){
useradd -s /sbin/nologin -r apache 
sed -i 's/^User.*/User apache/' /apps/httpd24/conf/httpd.conf
sed -i 's/^Group.*/Group apache/' /apps/httpd24/conf/httpd.conf
echo 'PATH="/apps/httpd24/bin:$PATH"' > /etc/profile.d/httpd.sh
source /etc/profile.d/httpd.sh
echo 'MANDATORY_MANPATH  /apps/httpd24/man' >> /etc/man_db.conf
action '基本配置完成'
}

#配置启动服务
service(){
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/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
systemctl daemon-reload
systemctl enable --now httpd.service
action '服务已启动'
}


#调用函数
install
install1
compile
configure
service
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值