第十五周作业

1、写出 MPM multi-processing module 工作模式原理以及区别

 答:

  • prefork模式

多进程I/O模型,一个主进程生成多个子进程,每个子进程同一时间响应一个请求

  • work模式

复用的多进程I/O模型,一个主进程生成多个子进程,每个子进程再负责生成多个线程,每个线程响应一个请求,相比prefork可用处理的请求更多,并且占用的内存较少。但是在keep-alive方式下,线程还需要保持会话,即在处理完一个请求后没有新的请求处理也会一直被占据,直到超时才释放该线程,这样就会降低线程的使用效率,并且当高并发场景下,后来的请求有可能无线程可用。

  • event模式

事件驱动模型,一个主进程生成多个子进程,每个子进程再生成多个线程,分成的线程分为1个监听线程和多个工作线程。监听线程负责向工作线程分配请求并和客户端保持会话连接,这样工作线程只需要负责处理请求,不用保持会话,提高了每个工作线程的使用效率和高并发场景下的请求处理能力

 

2、编译安装httpd 2.4 

答:

  • 安装依赖包
[root@centos8 ~]#yum install -y make gcc pcre-devel openssl-devel expat-devel
  • 下载apr、apr-util、httpd源码包
[root@centos8 ~]#wget https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-1.7.0.tar.bz2
[root@centos8 ~]#wget https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-util-1.6.1.tar.bz2
[root@centos8 ~]#wget https://mirrors.tuna.tsinghua.edu.cn/apache/httpd/httpd-2.4.46.tar.bz2
  • 将apr、apr-util源码和httpd源码合并
[root@centos8 ~]#tar xf apr-1.7.0.tar.bz2 
[root@centos8 ~]#tar xf apr-util-1.6.1.tar.bz2 
[root@centos8 ~]#tar xf httpd-2.4.46.tar.bz2 
[root@centos8 ~]#mv apr-1.7.0 httpd-2.4.46/srclib/apr
[root@centos8 ~]#mv apr-util-1.6.1 httpd-2.4.46/srclib/apr-util
[root@centos8 ~]#ls httpd-2.4.46/srclib/
apr  apr-util  Makefile.in
  • 编译安装
[root@centos8 httpd-2.4.46]#./configure \
> --prefix=/apps/httpd24 \
> --enable-so \
> --enable-ssl \
> --enable-cgi \
> --enable-rewrite \
> --enable-modules=most \
> --enable-mpms-shared=all \
> --with-zlib \
> --with-pcre \
> --with-include-apr

[root@centos8 httpd-2.4.46]#make -j 2 && make install
  • 创建apache用户,并指定其为httpd的运行用户
[root@centos8 httpd-2.4.46]#useradd -s /sbin/nologin apache
[root@centos8 httpd-2.4.46]#sed -ri.bak 's/^(User) daemon/\1 apache/' /apps/httpd24/conf/httpd.conf
[root@centos8 httpd-2.4.46]#sed -ri.bak 's/^(Group) daemon/\1 apache/' /apps/httpd24/conf/httpd.conf
  • 将httpd下的命令路径添加到PATH环境变量中
[root@centos8 httpd-2.4.46]#echo 'PATH=/apps/httpd24/bin:$PATH' > /etc/profile.d/httpd24.sh
[root@centos8 httpd-2.4.46]#source /etc/profile.d/httpd24.sh
  • 将帮助文件路径添加到man配置文件中
[root@centos8 httpd-2.4.46]#echo "MANDATORY_MANPATH    /apps/httpd24/man" >> /etc/man_db.conf 
  • 创建service文件(centos7及以上版本)
[root@centos8 httpd24]#vim /lib/systemd/system/httpd.service 

[Unit]
Description=The Apache HTTP Server
#Wants=httpd-init.service
After=network.target remote-fs.target nss-lookup.target httpd-init.service
Documentation=man:httpd(8)
Documentation=man:apachectl(8)


[Service]
Type=forking
#Environment=LANG=C

ExecStart=/apps/httpd24/bin/apachectl start
ExecReload=/apps/httpd24/bin/apachectl graceful
ExecStop=/apps/httpd24/bin/apachectl stop
# Send SIGWINCH for graceful stop
KillSignal=SIGWINCH
KillMode=mixed
PrivateTmp=true

[Install]
WantedBy=multi-user.target

 

 

3、编写一个一键部署 LAMP 架构之 wordpress 脚本

答:

set -e
RED='\e[1;31m'
GREEN='\e[1;32m'
END='\e[0m'
#脚本所在目录
DIR=`pwd`
#centos版本
OS_VERSION=`cat /etc/centos-release|sed -r 's#.* ([0-9]+).*#\1#'`

####################################################################################################

#编译安装mysql8.0.21函数
function install_mysql(){
#二进制包下载路径
URL=https://mirrors.tuna.tsinghua.edu.cn/mysql/downloads/MySQL-8.0/mysql-8.0.21-linux-glibc2.12-x86_64.tar.xz
#mysql压缩包名
MYSQL_TAR=${URL##*/}
#mysql解压后目录名
MYSQL_DIR=${MYSQL_TAR%.tar*}
#mysql安装目录
INSTALL_DIR=/usr/local
#mysql的软链接目录
LINK_DIR=/usr/local/mysql
#mysql数据库目录
DATA_DIR=/data/mysql

#检查环境是否已经安装了mysql数据库
which mysql &> /dev/null || [ -h ${LINK_DIR} ] && { echo -e "${RED}当前环境已有数据库${END}";return 0; }

#准备用户和数据目录
id mysql &> /dev/null || useradd -r -d ${DATA_DIR} mysql
mkdir -p ${DATA_DIR}
chown mysql:mysql ${DATA_DIR}

#下载依赖包
echo -e "${GREEN}开始下载mysql的依赖包${END}"
 yum install -y  wget libaio ncurses-compat-libs || { echo -e "${RED}下载依赖包失败,请检查${END}";exit 1; }
#下载二进制包并解压
echo -e "${GREEN}开始下载mysql二进制包,并解压${END}"
[ ! -f ${MYSQL_TAR} ] && wget --no-check-certificate ${URL} 
tar xf ${MYSQL_TAR} -C ${INSTALL_DIR}

#准备二进制程序的符号链接
echo -e "${GREEN}准备二进制符号链接${END}"
cd ${INSTALL_DIR}
ln -s ${MYSQL_DIR} ${LINK_DIR}
chown -R root:root ${LINK_DIR}/


#创建配置文件
echo -e "${GREEN}准备配置文件${END}"
cat >/etc/my.cnf<<EOF
[mysqld]
datadir=${DATA_DIR}
socket=${DATA_DIR}/mysql.sock
pid-file=${DATA_DIR}/mysql.pid
log-error=${DATA_DIR}/mysql.log
skip-name-resolve
innodb-file-per-table

[client]
socket=${DATA_DIR}/mysql.sock
EOF

#生成系统数据库,并配置环境变量PATH
echo 'PATH=/usr/local/mysql/bin/:$PATH' >> /etc/profile.d/lamp.sh
echo -e "${GREEN}环境变量PATH已配置,请重新source /etc/profile.d/lamp.sh文件"
cd ${LINK_DIR}
./bin/mysqld --initialize --user=mysql --datadir=/data/mysql

#准备启动文件,并修改初始登陆密码
echo -e "${GREEN}准备启动文件${END}"
if [ ${OS_VERSION} -eq 6 ];then
    cp ${LINK_DIR}/support-files/mysql.server /etc/rc.d/init.d/mysqld
    chkconfig --add mysqld
    service mysqld start && echo -e "${GREEN}mysql8.0启动成功${END}"
else
cat >/etc/systemd/system/mysqld.service<<EOF
[Unit]
Description=MySQL 8.0 database server
After=syslog.target
After=network.target

[Service]
Type=notify
User=mysql
Group=mysql
ExecStart=/usr/local/mysql/bin/mysqld --basedir=/usr/local/
TimeoutSec=300
RestartPreventExitStatus=1
LimitNOFILE = 10000
Environment=MYSQLD_PARENT_PID=1

[Install]
WantedBy=multi-user.target
EOF
    systemctl daemon-reload
    systemctl start mysqld && echo -e "${GREEN}mysql8.0启动成功${END}"
fi
INITIAL_PASSWORD=`awk '/temporary password/{print $NF}' ${DATA_DIR}/mysql.log`
${LINK_DIR}/bin/mysqladmin -uroot -p${INITIAL_PASSWORD} password magedu
}

####################################################################################################

#编译安装httpd2.4函数
function install_httpd() {
#源码包下载地址
APR_URL="https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-1.7.0.tar.bz2"
APR_UTIL_URL="https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-util-1.6.1.tar.bz2"
HTTPD_URL="https://mirrors.tuna.tsinghua.edu.cn/apache/httpd/httpd-2.4.46.tar.bz2"
#压缩包名
APR_TAR=`echo ${APR_URL} | sed -r 's#.*/(.*)#\1#'`
APR_UTIL_TAR=`echo ${APR_UTIL_URL} | sed -r 's#.*/(.*)#\1#'`
HTTPD_TAR=`echo ${HTTPD_URL} | sed -r 's#.*/(.*)#\1#'`
#解压后目录名
APR_DIR=`echo ${APR_TAR} | sed -r 's#(.*).tar.*#\1#'`
APR_UTIL_DIR=`echo ${APR_UTIL_TAR} | sed -r 's#(.*).tar.*#\1#'`
HTTPD_DIR=`echo ${HTTPD_TAR} | sed -r 's#(.*).tar.*#\1#'`
#编译安装目录
INSTALL_DIR=/apps/httpd24

#检测httpd是否已安装
[ -f /etc/init.d/httpd -o -f /usr/lib/systemd/system/httpd.service ] && { echo -e "${RED}httpd已经编译安装${END}";return 0; }
rpm -q httpd &> /dev/null && { echo -e "${RED}httpd已经yum安装${END}";return 0; }

#安装依赖包
echo -e "${GREEN}开始安装httpd的依赖包${END}"
yum install -y wget make gcc pcre-devel openssl-devel expat-devel lbzip2 &> /dev/null || echo -e "${RED}安装依赖包失败,请检查${END}"

#下载并解所有压源码包
echo -e "${GREEN}开始下载源码包,并解压${END}"
[ ! -f ${APR_TAR} ] && wget --no-check-certificate ${APR_URL} 
[ ! -f ${APR_UTIL_TAR} ] && wget --no-check-certificate ${APR_UTIL_URL}
[ ! -f ${HTTPD_TAR} ] && wget --no-check-certificate ${HTTPD_URL} 
tar xf ${APR_TAR} &> /dev/null 
tar xf ${APR_UTIL_TAR} &> /dev/null 
tar xf ${HTTPD_TAR} &> /dev/null 

#合并源码
mv ${APR_DIR} ${HTTPD_DIR}/srclib/apr
mv ${APR_UTIL_DIR} ${HTTPD_DIR}/srclib/apr-util

#编译安装
cd ${HTTPD_DIR}
./configure \
--prefix=${INSTALL_DIR} \
--enable-so \
--enable-ssl \
--enable-cgi \
--enable-rewrite \
--enable-modules=most \
--enable-mpms-shared=all \
--with-zlib \
--with-pcre \
--with-included-apr
make && make install && echo -e "${GREEN}编译安装httpd2.4完成${END}"

echo -e "${GREEN}准备配置文件${END}"
#修改运行用户
useradd -rs /sbin/nologin apache
sed -ri.bak 's#^User daemon#User apache#' ${INSTALL_DIR}/conf/httpd.conf
sed -ri.bak 's#^Group daemon#Group apache#' ${INSTALL_DIR}/conf/httpd.conf

#配置环境变量PATH
echo 'PATH=/apps/httpd24/bin:$PATH' >> /etc/profile.d/lamp.sh
echo -e "${GREEN}环境变量PATH已配置,请重新source /etc/profile.d/lamp.sh文件${END}"

#配置帮助文件
if [ ${OS_VERSION} -eq 6 ];then
    echo "MANPATH    /apps/httpd24/man" >> /etc/man.config
else
    echo "MANDATORY    /apps/httpd24/man" >> /etc/man_db.conf
fi

#配置httpd支持fcgi
sed -ri.bak 's@#(LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so)@\1@' ${INSTALL_DIR}/conf/httpd.conf
sed -ri 's@#(LoadModule proxy_module modules/mod_proxy.so)@\1@' ${INSTALL_DIR}/conf/httpd.conf
sed -ri 's/DirectoryIndex index.htm/DirectoryIndex index.php index.htm/' ${INSTALL_DIR}/conf/httpd.conf
echo 'ProxyRequests off
ProxyPassMatch "/(.*\.php)" fcgi://127.0.0.1:9000/apps/httpd24/htdocs/$1' >> ${INSTALL_DIR}/conf/httpd.conf

#创建启动脚本
echo -e "${GREEN}配置httpd启动文件${END}"
if [ ${OS_VERSION} -eq 6 ];then
cat > /etc/rc.d/init.d/httpd <<-"EOF"
#!/bin/bash
#
# httpd        Start up the httpd server daemon
#
# chkconfig: 2345 99 01
# description: httpd is a protocol for web server.
# This service starts up the httpd server daemon.
#
# processname: httpd
case $1 in
start)
/apps/httpd24/bin/apachectl start
;;
stop)
/apps/httpd24/bin/apachectl stop
;;
status)
/apps/httpd24/bin/apachectl status
;;
*)
echo "err"
;;
esac
EOF
    chmod +x /etc/rc.d/init.d/httpd
    chkconfig --add httpd
    service httpd start && echo -e "${GREEN}httpd启动成功${END}"
else

cat > /usr/lib/systemd/system/httpd.service <<EOF
[Unit]
Description=The Apache HTTP Server
#Wants=httpd-init.service
After=network.target remote-fs.target nss-lookup.target httpd-init.service
Documentation=man:httpd(8)
Documentation=man:apachectl(8)


[Service]
Type=forking
#Environment=LANG=C

ExecStart=/apps/httpd24/bin/apachectl start
ExecReload=/apps/httpd24/bin/apachectl graceful
ExecStop=/apps/httpd24/bin/apachectl stop
# Send SIGWINCH for graceful stop
KillSignal=SIGWINCH
KillMode=mixed
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF
    systemctl daemon-reload
    systemctl enable --now httpd && echo -e "${GREEN}httpd启动成功${END}"
fi
}

####################################################################################################

#编译安装php函数
function install_php() {
#php源码包下载路径
URL74=http://mirrors.sohu.com/php/php-7.4.10.tar.bz2
URL73=http://mirrors.sohu.com/php/php-7.3.22.tar.bz2
[ ${OS_VERSION} -eq 6 ] && URL=${URL73} || URL=${URL74}
#php压缩包名
PHP_TAR=${URL##*/}
#php压缩包目录
PHP_DIR=${PHP_TAR%.tar*}
#php安装目录
INSTALL_DIR=/apps/php

#检查php是否已经安装
[ -f /etc/init.d/php-fpm -o -f /usr/lib/systemd/system/php-fpm.service ] && { echo -e "${RED}php已经编译安装${END}";return 0; }
rpm -q php &> /dev/null && { echo -e "${RED}php已经yum安装${END}";return 0; }
#安装依赖包
echo -e "${GREEN}开始安装php依赖包${END}"
yum install -y gcc make openssl-devel libxml2-devel bzip2-devel lbzip2 libmcrypt-devel sqlite-devel wget &> /dev/null || { echo -e "${RED}依赖包安装失败,请检测原因${END}";exit 1; }

#安装PowerTools库或安装oniguruma-devel
if [ ${OS_VERSION} -eq 8 ];then
cat > /etc/yum.repos.d/PowerTools.repo <<EOF
[PowerTools]
name=powertools-centos8
baseurl=https://mirrors.aliyun.com/centos/8/PowerTools/x86_64/os/
gpgcheck=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
EOF
yum install -y oniguruma-devel &> /dev/null || { echo -e "${RED}yum安装oniguruma-devel失败,请检查${END}";exit 2; }
else
yum install -y oniguruma-devel &> /dev/null || { echo -e "${RED}yum安装oniguruma-devel失败,请检查${END}";exit 2; }
fi
echo -e "${GREEN}依赖包安装完毕${END}"

#下载并解压源码包
echo -e "${GREEN}开始下载php源码包,并解压${END}"
if [ ! -f ${PHP_TAR} ];then
wget ${URL} || { echo -e "${RED}下载源码包失败,请检查${END}";exit 3; }
fi
tar xf ${PHP_TAR} &> /dev/null || { echo -e "${RED}解压php压缩包失败,请检查${END}";exit 3; }

#编译安装php
echo -e "${GREEN}开始编译安装php${END}"
cd ${PHP_DIR}
./configure \
--prefix=${INSTALL_DIR} \
--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 \
--disable-fileinfo 
make && make install && echo -e "${GREEN}以fcgi方式安装php完成,开始设定配置${END}"

#准备php配置文件
cp ${DIR}/${PHP_DIR}/php.ini-production /etc/php.ini
cd ${INSTALL_DIR}/etc
cp php-fpm.conf.default php-fpm.conf
cp php-fpm.d/www.conf.default php-fpm.d/www.conf

#配置PATH环境变量
echo 'PATH=/apps/php/bin:$PATH' >> /etc/profile.d/lamp.sh
echo -e "${GREEN}PATH环境变量已配置,请重新source /etc/profile.d/lamp.sh${END}"

#修改配置文件
sed -ri.bak 's/(user =) nobody/\1 apache/' ${INSTALL_DIR}/etc/php-fpm.d/www.conf
sed -ri 's/(group =) nobody/\1 apache/' ${INSTALL_DIR}/etc/php-fpm.d/www.conf

#准备php-fpm启动文件
if [ ${OS_VERSION} -eq 6 ];then
    cp ${DIR}/${PHP_DIR}/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
    chmod +x /etc/init.d/php-fpm
    chkconfig --add php-fpm
    service php-fpm start && echo -e "${GREEN}php-fpm启动成功${END}"
else
     cp ${DIR}/${PHP_DIR}/sapi/fpm/php-fpm.service /usr/lib/systemd/system/
     systemctl daemon-reload
     systemctl start php-fpm && echo -e "${GREEN}php-fpm启动成功${END}"
fi
}

####################################################################################################
function install_wordpress() {
URL=https://cn.wp.xz.cn/latest-zh_CN.tar.gz
WP_TAR=${URL##*/}

#下载wordpress压缩包
echo -e "${GREEN}下载wordpress压缩包,并解压${END}"
[ ! -f ${WP_TAR} ] && wget --no-check-certificate ${URL}
tar xf ${WP_TAR} 
mv wordpress /apps/httpd24/htdocs/wp
chown -R apache.apache /apps/httpd24/htdocs/wp

#创建数据库
/usr/local/mysql/bin/mysql -uroot -pmagedu -e 'CREATE DATABASE wp;'
/usr/local/mysql/bin/mysql -uroot -pmagedu -e 'CREATE USER wpuser@"%" IDENTIFIED BY "magedu";'
/usr/local/mysql/bin/mysql -uroot -pmagedu -e 'GRANT ALL ON wp.* TO wpuser@"%";'

}
####################################################################################################
install_mysql
cd ${DIR}
install_httpd
cd ${DIR}
install_php
cd ${DIR}
install_wordpress

 

posted @ 2020-10-01 22:26  jojohyj  阅读(125)  评论(0编辑  收藏  举报