第15周作业
1、写出 MPM multi-processing module 工作模式原理以及区别
prefork
多进程I/O模型,每个进程生成多个子进程,每个子进程生成一个线程,每个线程处理一个请求,
优点:稳定性好
缺点:并发数多的情况下,占用系统资源多,适用于访问量不是很大的场景
worker
复用的多进程I/O模型 一个主进程,生成m个子进程,每个子进程生成n个线程,每个线程相应一个请求,并发响应数m*n
优点:相比prefork模型,能处理更多的并发
缺点:使用keep-alive的长连接的方式,某个线程会一直被占据,即使没有传输数据,也需要一直等到超时才会被释放.如果过多线程,被这样占据,也会导致在高并发场景下的无服务线程可用.
event
一个主进程,生成m个子进程,每个子进程生成n个线程,每个线程响应一个请求,并发响应数m*n,有专门的监控线程来管理这些keep-alive类型的线程,当有真实请求时,将请求传递给服务线程,执行完毕后,有允许被释放.
2、编译安装httpd 2.4
#!/bin/bash
#创建安装目录,DIR1为解压目录,DIR2为安装目录
DIR1="/usr/local/src/"
DIR2="/app/httpd/"
[ -d $DIR2 ] || mkdir -p ${DIR2}
#相关变量
APR_URL="https://mirrors.bfsu.edu.cn/apache//apr/apr-1.7.0.tar.bz2"
APR_UTIL_URL="https://mirrors.bfsu.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_DIR=`echo ${APR_URL}|sed -rn 's#.*/(.*).tar.bz2$#1#p'`
APR_UTIL_DIR=`echo ${APR_UTIL_URL}|sed -rn 's#.*/(.*).tar.bz2$#1#p'`
HTTPD_DIR=`echo ${HTTPD_URL}|sed -rn 's#.*/(.*).tar.bz2$#1#p'`
CPU=`lscpu|sed -n '/^CPU(s)/p'|tr -s " "|cut -d " " -f2`
#安装函数
install_httpd() {
#编译并安装
yum -y install gcc make pcre-devel openssl-devel expat-devel wget
wget -P ${DIR1} ${APR_URL}
wget -P ${DIR1} ${APR_UTIL_URL}
wget -P ${DIR1} ${HTTPD_URL}
cd ${DIR1}
for file in *;do
tar xf $file
done
mv ${APR_DIR} ${HTTPD_DIR}/srclib/apr
mv ${APR_UTIL_DIR} ${HTTPD_DIR}/srclib/apr-util
cd ${HTTPD_DIR}/
./configure --prefix=/app/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=prefork
make -j ${CPU} && make install
#PATH,建用户
id apache &> /dev/null || useradd -r -s /sbin/nologin apache
sed -ri 's#^User.*#User apache#' /app/httpd/conf/httpd.conf
sed -ri 's#^Group.*#Group apache#' /app/httpd/conf/httpd.conf
echo PATH='/app/httpd/bin/:$PATH' > /etc/profile.d/httpd.sh
. /etc/profile.d/httpd.sh
sed -i '$aMANDATORY_MANPATH /app/httpd/man' /etc/man_db.conf
#创建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
ExecStart=${DIR2}bin/apachectl start
ExecReload=${DIR2}bin/apachectl graceful
ExecStop=${DIR2}bin/apachectl stop
KillSignal=SIGCONT
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable httpd.service
systemctl start httpd.service
}
install_httpd
3、编写一个一键部署 LAMP 架构之 wordpress 脚本
#!/bin/bash
#创建目录
DOWNLOAD_DIR="/usr/local/src"
#相关变量
CPU=`lscpu|sed -n '/^CPU(s)/p'|tr -s " "|cut -d " " -f2`
install_httpd() {
#定义变量
APR_URL="https://mirrors.bfsu.edu.cn/apache//apr/apr-1.7.0.tar.bz2"
APR_UTIL_URL="https://mirrors.bfsu.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_DIR=`echo ${APR_URL}|sed -rn 's#.*/(.*).tar.bz2$#1#p'`
APR_UTIL_DIR=`echo ${APR_UTIL_URL}|sed -rn 's#.*/(.*).tar.bz2$#1#p'`
HTTPD_DIR=`echo ${HTTPD_URL}|sed -rn 's#.*/(.*).tar.bz2$#1#p'`
INSTALL_HTTPD_DIR="/app/httpd"
[ -d ${INSTALL_HTTPD_DIR} ] || mkdir -p ${INSTALL_HTTPD_DIR}
#编译安装
yum -y install gcc make pcre-devel openssl-devel expat-devel wget
wget -P ${DOWNLOAD_DIR} ${APR_URL}
wget -P ${DOWNLOAD_DIR} ${APR_UTIL_URL}
wget -P ${DOWNLOAD_DIR} ${HTTPD_URL}
cd ${DOWNLOAD_DIR}
for file in *;do
tar xf $file
done
mv ${APR_DIR} ${HTTPD_DIR}/srclib/apr
mv ${APR_UTIL_DIR} ${HTTPD_DIR}/srclib/apr-util
cd ${HTTPD_DIR}/
./configure --prefix=/app/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=prefork
make -j ${CPU} && make install
#建用户
id apache &> /dev/null || useradd -r -s /sbin/nologin apache
#修改配置文件
sed -ri 's#^User.*#User apache#' ${INSTALL_HTTPD_DIR}/conf/httpd.conf
sed -ri 's#^Group.*#Group apache#' ${INSTALL_HTTPD_DIR}/conf/httpd.conf
echo PATH='/app/httpd/bin/:$PATH' > /etc/profile.d/httpd.sh
. /etc/profile.d/httpd.sh
#创建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
ExecStart=${INSTALL_HTTPD_DIR}/bin/apachectl start
ExecReload=${INSTALL_HTTPD_DIR}/bin/apachectl graceful
ExecStop=${INSTALL_HTTPD_DIR}/bin/apachectl stop
KillSignal=SIGCONT
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
#启动
systemctl daemon-reload
systemctl enable --now httpd.service
}
#安装mysql5.7
MYSQL_URL="http://mirrors.163.com/mysql/Downloads/MySQL-5.7/mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz"
MYSQL_RAR_NAME=`echo ${MYSQL_URL}|sed -rn 's#.*/(.*)$#1#p'`
MYSQL_DIR=`echo ${MYSQL_RAR_NAME}|sed -rn 's#(.*).tar.gz#1#p'`
MYSQL_ROOT_PASSWORD=magedu
INSTALL_MYSQL_DIR="/app/mysql"
COLOUR='echo -e 33[01;31m'
END=' 33[0m'
check_mysql (){
if [ $UID -ne 0 ]; then
$COLOUR"当前用户不是root,安装失败"$END
exit
fi
[ -d /usr/local/mysql ] && { $COLOUR"数据库已存在,安装失败"$END;exit; }
}
install_mysql (){
$COLOUR"开始安装MySQL数据库..."$END
yum install -y libaio numactl-libs perl-Data-Dumper make gcc-c++ cmake bison-devel ncurses-devel readline-devel libaio-devel perl wget lrzsz vim libnuma* bzip2 xz
yum install libncurses* wget -y
cd $DOWNLOAD_DIR
wget $MYSQL_URL
tar xf $MYSQL_RAR_NAME -C /usr/local
ln -s /usr/local/$MYSQL_DIR /usr/local/mysql
id mysql &> /dev/null || useradd -s /sbin/nologin -r mysql
chown -R root.root /usr/local/mysql
echo 'PATH=/usr/local/mysql/bin/:/$PATH' > /etc/profile.d/mysql.sh
. /etc/profile.d/mysql.sh
ln -s /usr/local/mysql/bin/* /usr/bin/
cat > /etc/my.cnf <<EOF
[mysqld]
server-id=1
log-bin
socket=/data/mysql/mysql.sock
datadir=/data/mysql
character-set-server=utf8mb4
log-error=/data/mysql/mysql.log
pid-file=/data/mysql/mysql.pid
[client]
socket=/data/mysql/mysql.sock
default-character-set=utf8mb4
EOF
[ -d /data/mysql ] || mkdir -p /data/mysql
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_ROOT_PASSWORD} &> /dev/null
}
#安装php
install_php() {
PHP_URL="http://mirrors.sohu.com/php/php-7.4.10.tar.bz2"
PHP_RAR_NAME=`echo ${PHP_URL}|sed -rn 's#.*/(.*)#1#p'`
PHP_DIR=`echo ${PHP_RAR_NAME}|sed -rn 's#(.*).t.*#1#p'`
INSTALL_PHP_DIR="/app/php"
INSTALL_HTTPD_DIR="/app/httpd"
CPU=`lscpu|sed -n '/^CPU(s)/p'|tr -s " "|cut -d " " -f2`
yum -y install gcc libxml2-devel bzip2-devel libmcrypt-devel sqlite-devel oniguruma-devel openssl-devel openssl wget
[ -f ${DOWNLOAD_DIR}/${PHP_RAR_NAME} ] || wget -P ${DOWNLOAD_DIR} ${PHP_URL}
cd ${DOWNLOAD_DIR}
tar xf ${PHP_RAR_NAME}
cd ${PHP_DIR}
./configure --prefix=${INSTALL_PHP_DIR} --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
echo "PATH=${INSTALL_PHP_DIR}/bin:$PATH" > /etc/profile.d/php.sh
. /etc/profile.d/php.sh
cp php.ini-production /etc/php.ini
cp sapi/fpm/php-fpm.service /usr/lib/systemd/system/
cd ${INSTALL_PHP_DIR}/etc
cp php-fpm.conf.default php-fpm.conf
cd php-fpm.d/
cp www.conf.default www.conf
sed -ri 's#(user = )nobody#1apache#' ${INSTALL_PHP_DIR}/etc/php-fpm.d/www.conf
sed -ri 's#(group = )nobody#1apache#' ${INSTALL_PHP_DIR}/etc/php-fpm.d/www.conf
sed -ri 's#;(pm.status_path = )/status#1/fpm_status#' ${INSTALL_PHP_DIR}/etc/php-fpm.d/www.conf
sed -ri 's#;(ping.path = /ping)#1#' ${INSTALL_PHP_DIR}/etc/php-fpm.d/www.conf
mkdir /etc/php.d -p
cat > /etc/php.d/opcache.ini << EOF
[opacache]
zend_extensio=opcache.so
opcache.enable=1
EOF
systemctl daemon-reload
systemctl enable --now php-fpm.service
sed -ri 's@#(LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so)@1@' ${INSTALL_HTTPD_DIR}/conf/httpd.conf
sed -ri 's@#(LoadModule proxy_module modules/mod_proxy.so)@1@' ${INSTALL_HTTPD_DIR}/conf/httpd.conf
sed -ri 's@(DirectoryIndex )(index.html)@1index.php 2@' ${INSTALL_HTTPD_DIR}/conf/httpd.conf
cat >> ${INSTALL_HTTPD_DIR}/conf/httpd.conf <<EOF
AddType application/x-httpd-php .php
ProxyRequests Off
<virtualhost *:80>
servername blog
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>
EOF
mkdir -pv /data/wordpress
systemctl restart httpd
systemctl restart php-fpm.service
}
#安装wordpress
install_wordpress(){
#网络下载比较慢,将包传到 DOWNLOAD_DIR
cd ${DOWNLOAD_DIR}
tar xf wordpress-5.4.2.tar.gz
mkdir /data
mv ${DOWNLOAD_DIR}/wordpress /data
chown -R apache:apache /data/wordpress
mysql -uroot -pmagedu -e 'create database wordpress;'
mysql -uroot -pmagedu -e 'grant all on wordpress.* to admin@"10.0.0.%" identified by "magedu";'
}
install_httpd
check_mysql
install_mysql
install_php
install_wordpress