Apache 工作模式的优化深入

Apache 2.X 支持插入式并行处理模块,称为多路处理模块(MPM)。在编译apache时必须选择也只能选择一个MPM,对类UNIX系统,有几个不同的MPM可供选择,它们会影响到apache的速度和可伸缩性。
  Prefork MPM : 这个多路处理模块(MPM)实现了一个非线程型的、预派生的web服务器,它的工作方式类似于Apache 1.3。它适合于没有线程安全库,需要避免线程兼容性问题的系统。它是要求将每个请求相互独立的情况下最好的MPM,这样若一个请求出现问题就不会影响到其他请求。
  这个MPM具有很强的自我调节能力,只需要很少的配置指令调整。最重要的是将MaxClients设置为一个足够大的数值以处理潜在的请求高峰,同时又不能太大,以致需要使用的内存超出物理内存的大小。
  Worker MPM : 此多路处理模块(MPM)使网络服务器支持混合的多线程多进程。由于使用线程来处理请求,所以可以处理海量请求,而系统资源的开销小于基于进程的MPM。但是,它也使用了多进程,每个进程又有多个线程,以获得基于进程的MPM的稳定性。
  每个进程可以拥有的线程数量是固定的。服务器会根据负载情况增加或减少进程数量。一个单独的控制进程(父进程)负责子进程的建立。每个子进程可以建立ThreadsPerChild数量的服务线程和一个监听线程,该监听线程监听接入请求并将其传递给服务线程处理和应答。
  不管是Worker模式或是Prefork 模式,Apache总是试图保持一些备用的(spare)或者是空闲的子进程(空闲的服务线程池)用于迎接即将到来的请求。这样客户端就不需要在得到服务前等候子进程的产生。
  总的来说 prefork方式速度要稍高于 worker, 然而它需要的CPU 和memory 资源也稍多于worker。

           **下面将对   Prefork MPM  、Worker MPM  这两种工作模式的配置介绍下 **

首先编译那种Apache服务

            # tar xzvf httpd-2.4.2.tar.gz -C /opt
            # tar xzvf apr-1.4.6.tar.gz -C /opt  (支持Apache上层应用跨平台,提供底层接口库)
            # tar xzvf apr-util-1.4.1.tar.gz -C /opt
            # cp -R apr-1.4.6/ /opt/httpd-2.4.2/srclib/apr
            # cp -R apr-util-1.4.1/ /opt/httpd-2.4.2/srclib/apr-util
            # yum install gcc gcc-c++ make pcre pcre-devel -y      //安装环境 (pcre : 一个Perl库,支持正则表达式)
            # yum install zlib-devel -y

.

            # cd /opt/httpd-2.4.2
            # ./configure \
            --prefix=/usr/local/httpd \
            --enable-deflate \
            --with-mpm=prefork \                           //选择为prefork工作模式
            --enable-expires \
            --enable-so \
            --enable-rewrite \
            --enable-charset-lite \
            --enable-cgi

.

            # make && make install
            # grep -v "#" /usr/local/httpd/bin/apachectl > /etc/init.d/httpd   
            # vi /etc/init.d/httpd 在文件最前面插入下面的
                        #!/bin/sh
                        # chkconfig:2345 85 15
                        # description:Apache is a World Wide Web server.

.

            # chmod +x /etc/init.d/httpd
            # chkconfig --add httpd
            # chkconfig --list httpd
            # chkconfig --level 35 httpd on    //开机自启动
            # ln -s /usr/local/httpd/conf/httpd.conf /etc/httpd.conf    //生成软连接方便管理朱配置文件
         # vim /etc/httpd.conf(设置主配置文件)
              ServerName www.benet.com:80
               Listen 192.168.100.102:80
                #Listen 80       //注释该行

.

prefork模式

    # vim /etc/httpd.conf 
    Include conf/extra/httpd-mpm.conf      //去# 启用httpd-mpm.conf 文件
    # vim /usr/local/httpd/conf/extra/httpd-mpm.conf
<IfModule mpm_prefork_module>
StartServers                     10      # 启动时进程数
MinSpareServers             10      # 最小空闲进程数
MaxSpareServers            50      # 最大空闲进程数
MaxRequestWorkers      150     # 最大并发进程数
MaxConnectionsPerChild   0     # 最大连接数限制
</IfModule>

.

    # /usr/local/httpd/bin/httpd -l    //查看当前工作模式

Apache 工作模式的优化深入

# lsof -i:80     //查看Apache 进程运行的情况

Apache 工作模式的优化深入
.

worker模式

     编译安装Apache  下面编译工作模式指定为 --with-mpm=worker
            其他安装步骤参考上面
            # cd /opt/httpd-2.4.2
            # ./configure \
            --prefix=/usr/local/httpd \
            --enable-deflate \
            --with-mpm=worker \                           //选择为worker工作模式
            --enable-expires \
            --enable-so \
            --enable-rewrite \
            --enable-charset-lite \
            --enable-cgi

. .

# vim /usr/local/httpd/conf/extra/httpd-mpm.conf
 <IfModule mpm_worker_module>
StartServers                      2        #启动时进程数
MinSpareThreads             25      #最小空闲线程数
MaxSpareThreads           75       #最大空闲线程数
ThreadsPerChild              25       #每个进程可以启动的线程数量
MaxRequestWorkers       150     #线程数量最大值
MaxConnectionsPerChild   0      #最大连接数限制
ThreadLimit                      64      #每个进程可以启动的线程数量上限值
</IfModule>

猜你喜欢

转载自blog.51cto.com/13630803/2128584