第十五周作业

------------恢复内容开始------------

------------恢复内容开始------------

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

 prework:多进程I/O模型,每个进程响应一个请求,Centos7默认模型

     一个主进程:生成和回收n个子进程,创建套接字,不响应请求

     多个子进程:工作work进程,每个子进程处理一个请求;系统初始时,预先生成多个空闲进程等待请求。

Prefork MPM:预先生模式,有一个主控制进程,然后生成多个子进程,每个子进程有一个独立的线程                        响应用户请求,相对比较占用内存,但是比较稳定,可以设置最大和最小的进程数,是最古             老的一种模式,也是最稳定的模式,适用于访问量不是很大的场景。

优点:稳定

缺点:慢,占用资源,不适用于高并发场景

worker:复用的多进程I/O模型,多线程多进程,iis使用此模型

    一个主进程:生成m个子进程,每个子进程负责生n个线程,每个线程响应一个请求,并发响应请求:m*n

work MPM: 是一种多进程和多线程的混合模型,有一个控制进程,启动多个子进程,每个子进程里面包含固定的线程,使用线程来处理请求,当线程不够使用的时候会在启动一个新的子进程,然后在进程里面再启动线程处理请求,由于其使用了线程处理请求,因此可以承受更高的并发。  

优点:相比于prefork占用的内存比较少,可以同时处理更多的请求

缺点:使用keep-alive的长连接方式,某个线程会一直被占据,即使没有传输数据,也需要一直等待到超时才会被释放。如果过多的线程,被这样占据,也会导致在高并发场景下的无服务线程可用。 

event:事件驱动模型(worker模型的变种):一个主进程,生成m个子进程,每个子进程负责生成n个线程,每个线程响应一个请求,并发响应请求:m*n,有专门的监控线程来管理这些keep-alive类型的线程,当有真实请求时,将请求传递给服务线程,执行完毕后,又允许释放。这样增强了高并发场景下的请求处理能力。

uevent MPM:Apache中最新的模式,属于事件驱动模型,每个进程响应多个请求,在现在版本里的已经是最稳定可用的模式。它和worker模式很像,最大的区别在于,他解决了keep-alive场景下,长期被占用的线程的资源浪费问题(某些线程因为被keep—alive,空挂在哪里等待,中间几乎没有请求过来,甚至等到超时)event MPM中,会有一个专门的线程来管理这些keep-alive类型的线程,当有真实请求过来的时候,将请求传递给服务线程,执行完毕后,又允许它释放。这样增强了高并发场景下的请求处理能力。event只在有数据发送的时候才开始建立连接,连接请求才会触发工作线程,即使用了TCP的一个选项,叫做延迟接收连接TCP_DEFER_ACCEPT,加了这个选项后,若客户端只进行TCP的一个选项,不发送请求,则不会触发Accept操作,也就不会触发工作线程去干活,进行了简单的防攻击(TCP连接)
优点:单线程响应多请求,占据更少的内存,高并发下表现更优秀,会有一个专门的线程来管理、keep-alive类型的线程,当有真实请求的过来的时候,将请求传递给服务线程,执行完毕后,又允许它释放
缺点:没有线程安全控制

2、编译安装httpd 2.4 

tar xf apr-1.7.0.tar.bz2

tar xvf apr-util-1.6.1.tar.bz2 

tar xvf httpd-2.4.46.tar.bz2 

ll

yum install -y bzip2

mv apr-1.7.0 httpd-2.4.46/srclib/apr

mv apr-util-1.6.1 httpd-2.4.46/srclib/apr-util

ls httpd-2.4.46/srclib/

cd httpd-2.4.46/

./configure --prefix=/app/htppd24 --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-included-apr --enable-mpms-shared=all --with-mpm=prefork

make -j 4 && make install

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

httpd_install_ver=(
    apr-1.7.0.tar.gz 
    apr-util-1.6.1.tar.gz  
    httpd-2.4.39.tar.bz2
)
tar_wordpress="wordpress-5.2.tar.gz"
tar_discuz="Discuz_X3.3_SC_UTF8.zip"
tar_php="php-7.3.5.tar.bz2"
tar_mariadb="mariadb-10.2.23-linux-x86_64.tar.gz"
let cpu_num=`lscpu|awk -F ‘ +‘ ‘/^CPU\(s\):/{print $2}‘`-1
mysql_install_dir="/app/mysql"
mysql_data_dir="/data/mysql"
mysql_user=mysql
mysql_login_name=root
mysql_root_passwd=123456
package_name=`echo $tar_mariadb | sed -r ‘s/(.*).(tar.gz)/\1/‘`
httpd_user=apache
httpd_prefix="/app/httpd24"
php_prefix="/app/php"
wordpress_dir="$httpd_prefix/htdocs/wordpress"
wordpress_database=wordpress
wordpress_user=wpuser
wordpress_passwd=\‘fanfubin\‘
wordpress_passwd_php=`echo $wordpress_passwd | tr -d "\‘"`
wordpress_host=\‘localhost\‘
wordpress_host_php=localhost
discuz_dir="$httpd_prefix/htdocs/discuz"
discuz_database=ultrax
discuz_user=discuz
read -p "Pls you choose num  1-php-local  2-php-remote": choose_num
[ $choose_num -ne 1 ] && { echo "remote pattern nonsupport!"; exit; }
[ ! -e $tar_wordpress ] && { echo "$tar_wordpress is not exist"; exit; }
#Mariadb .tar. is exist?
[ ! -e $tar_mariadb ] && { echo "$tar_mariadb is not exist"; exit; }
#Php .tar. is exist?
[ ! -e $tar_php ] && { echo "$tar_php is not exist"; exit; }
#httpd  .tar. is exist?
for ver in ${httpd_install_ver[*]}
    do
        if [ ! -e $ver ];then
            echo "${ver} is not exist!"
            exit
        fi
    done
[ `rpm -q php-mysql` -eq 0 ] && { echo "Pls complete discharge php-mysql"; exit; }
####Install httpd
[ ! -d $httpd_prefix ] && mkdir -p $httpd_prefix
yum -y install gcc pcre-devel openssl-devel expat-devel autoconf libtool gcc-c++ lbzip2 expat-devel unzip 
id $httpd_user
if [ `echo $?` -ne 0 ];then
    useradd -r -s /sbin/nologin $httpd_user
fi
#tar xf
for ver1 in ${httpd_install_ver[*]}
    do
        tar xf $ver1
    done
#httpd_file name && httpd_install_dir
httpd_install_num=`echo ${#httpd_install_ver[@]}`
num=0
for name in ${httpd_install_ver[*]}
    do
        if [ $num -ne $httpd_install_num ];then
            httpd_file_name[${num}]=`echo $name | awk -F ‘.tar.‘ ‘{print $1}‘`
            if [ `echo $name | awk -F ‘.tar.‘ ‘{print $1}‘ | grep -i httpd | wc -l` -eq 1 ];then
                httpd_install_dir=`echo $name | awk -F ‘.tar.‘ ‘{print $1}‘`
            fi
            let num++
        fi
    done
for file_mv in ${httpd_file_name[*]}
    do
        if [ `echo $file_mv | grep -i httpd |wc -l` -ne 1 ];then
            mv $file_mv ${httpd_install_dir}/srclib/`echo $file_mv | sed -r ‘s/(.*)-.*/\1/‘`
        fi
    done
cd $httpd_install_dir
./configure --prefix=$httpd_prefix --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
sleep 1
make -j $cpu_num  && make install
sleep 1
echo ‘PATH=‘${httpd_prefix}‘/bin:$PATH‘ > /etc/profile.d/httpd.sh
sleep 1
source /etc/profile.d/httpd.sh
. /etc/profile.d/httpd.sh
sed -ir ‘s/User daemon/User ‘$httpd_user‘/‘ $httpd_prefix/conf/httpd.conf
sleep 1
sed -ir ‘s/Group daemon/Group ‘$httpd_user‘/‘ $httpd_prefix/conf/httpd.conf
echo "$httpd_prefix/bin/apachectl start" >> /etc/rc.d/rc.local
chmod +x /etc/rc.d/rc.local
sed -ri ‘s%#LoadModule proxy_module modules/mod_proxy.so%LoadModule proxy_module modules/mod_proxy.so%‘ $httpd_prefix/conf/httpd.conf
sed -ri ‘s%#LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so%LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so%‘ $httpd_prefix/conf/httpd.conf
sed -ri ‘s/DirectoryIndex index.html/DirectoryIndex index.php index.html/‘ $httpd_prefix/conf/httpd.conf
cat >>$httpd_prefix/conf/httpd.conf<<EOF
addType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
<virtualhost *:80>
documentroot $httpd_prefix/htdocs/wordpress
servername  blog.test.com
ProxyRequests Off
#ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/data/wordpress/
ProxyPassMatch ^/(.*\.php)$ unix:/var/run/php.sock|fcgi://localhost${httpd_prefix}/htdocs/wordpress
<directory $httpd_prefix/htdocs/wordpress>
require all granted
require all granted
###Install php-fpm
yum -y install libxml2-devel bzip2-devel libmcrypt-devel phpize lbzip2
tar xf $tar_php 
sleep 1
php_install_dir=`echo $tar_php | awk -F ‘.tar.‘ ‘{print $1}‘`
sleep 1
chown -R  root.root ${php_install_dir}
cd ${php_install_dir}
./configure --prefix=$php_prefix --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-openssl --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --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
sleep 2
make -j $cpu_num && make install
cp php.ini-production  /etc/php.ini
sed -ri ‘s#;date.timezone =#date.timezone = "Asia/Shanghai"#‘ /etc/php.ini
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm
cp $php_prefix/etc/php-fpm.conf.default $php_prefix/etc/php-fpm.conf
cp $php_prefix/etc/php-fpm.d/www.conf.default $php_prefix/etc/php-fpm.d/www.conf
sleep 1
sed -ir ‘s/user = nobody/user = ‘$httpd_user‘/‘ $php_prefix/etc/php-fpm.d/www.conf
sed -ir ‘s/group = nobody/group = ‘$httpd_user‘/‘ $php_prefix/etc/php-fpm.d/www.conf
if [ $choose_num -eq 1 ];then
    sed -ri ‘s#listen = 127.0.0.1:9000#listen = /var/run/php.sock#‘ $php_prefix/etc/php-fpm.d/www.conf
    sed -ri ‘s#;listen.mode = 0660#listen.mode = 0666#‘ $php_prefix/etc/php-fpm.d/www.conf
fi
cd -
sleep 2
####Binary install mariadb
id $mysql_user &>/dev/null
if [ `echo $?` -ne 0 ];then
    userdel -r $mysql_user &>/dev/null
    useradd -r -u 336 -s /sbin/nologin -d $mysql_data_dir $mysql_user &>/dev/null
else
    useradd -r -u 336 -s /sbin/nologin -d $mysql_data_dir $mysql_user &>/dev/null
fi 
rpm -q libaio &>/dev/null
[ `echo $?` -ne 0 ] && yum -y install libaio
rpm -q expect &>/dev/null
[ `echo $?` -ne 0 ] && yum -y install expect
#此文件可能会造成影响,所以先清空 
\rm -rf /etc/my.cnf
tar xf $tar_mariadb -C /usr/local/
sleep 2
cd  /usr/local/
ln -s $package_name mysql
chown -R root.root /usr/local/mysql/
echo ‘PATH=/usr/local/mysql/bin:$PATH‘ > /etc/profile.d/mysql.sh 
sleep 1
source /etc/profile.d/mysql.sh
. /etc/profile.d/mysql.sh
mkdir $mysql_data_dir -p
chown ${mysql_user}.${mysql_user} $mysql_data_dir
cd -
cd /usr/local/mysql
./scripts/mysql_install_db --datadir=$mysql_data_dir --user=$mysql_user
mkdir -p /etc/mysql
cp /usr/local/mysql/support-files/my-huge.cnf /etc/mysql/my.cnf
#禁止主机名解析,建议使用
sed -ri ‘/^\[mysqld\]/askip_name_resolve = on‘ /etc/mysql/my.cnf
sed -ri ‘/^\[mysqld\]/adatadir=\/data\/mysql‘ /etc/mysql/my.cnf
cp /usr/local/mysql/support-files/mysql.server  /etc/init.d/mysqld
chkconfig --add mysqld
service mysqld start
sleep 2
expect <<EOF
spawn mysql_secure_installation
expect {
"Enter current password for root" { send "\n";exp_continue }
"Set root password" { send "\n";exp_continue }
"New password" { send "${mysql_root_passwd}\n";exp_continue }
"Re-enter new password" { send "${mysql_root_passwd}\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 "]#" { send "exit\n" }
expect eof
EOF
mysqladmin -uroot -p${mysql_root_passwd} ping &>/dev/null
[ `echo $?` -eq 0 ] && echo ‘mysql is running !‘ || echo ‘mysql is stopped‘ 
#create wordpress 
mysql -uroot -p${mysql_root_passwd} -e "create database ${wordpress_database}"
#mysql -uroot -p${mysql_root_passwd} -e "grant all on ${wordpress_database}.* to ${wordpress_user}@‘192.168.36.%‘ identified by ‘fanfubin‘"
mysql -uroot -p${mysql_root_passwd} -e "grant all on ${wordpress_database}.* to ${wordpress_user}@‘localhost‘ identified by ‘fanfubin‘"‘"
cd -
sleep 2
###Install wordpress
tar xf $tar_wordpress -C $httpd_prefix/htdocs/
sleep 1
cp $wordpress_dir/wp-config-sample.php $wordpress_dir/wp-config.php
sed -ri ‘s/database_name_here/‘$wordpress_database‘/‘ $wordpress_dir/wp-config.php
sed -ri ‘s/username_here/‘$wordpress_user‘/‘ $wordpress_dir/wp-config.php
sed -ri ‘s/password_here/‘$wordpress_passwd_php‘/‘ $wordpress_dir/wp-config.php
sed -ri ‘s/localhost/‘$wordpress_host_php‘/‘ $wordpress_dir/wp-config.php
setfacl -R -m u:${httpd_user}:rwx $wordpress_dir
echo $wordpress_passwd_php
echo $wordpress_host_php
sleep 2
apachectl restart
sleep 2
service php-fpm start

 

 

 

 

posted @ 2020-09-08 08:44  xuziran  阅读(180)  评论(0编辑  收藏  举报