返回列表 发帖

Apache2.4的MPM模式(prefork)

笺注:这是在 LNMP一键安装包(lamp_CentOS6.9) 的基础上进行的。


查看Apache的版本:
[root@localhost ~]# find / -name httpd
/usr/local/apache/bin/httpd
[root@localhost ~]#
[root@localhost ~]# /usr/local/apache/bin/httpd -v
Server version: Apache/2.4.20 (Unix)
Server built:   Jun 25 2020 01:46:46


查看Apache编译安装的参数:
[root@localhost ~]# find / -name config.nice
/usr/local/apache/build/config.nice
[root@localhost ~]#
[root@localhost ~]# cat /usr/local/apache/build/config.nice
#! /bin/sh
#
# Created by configure

"./configure" \
"--prefix=/usr/local/apache" \
"--enable-mods-shared=most" \
"--enable-headers" \
"--enable-mime-magic" \
"--enable-proxy" \
"--enable-so" \
"--enable-rewrite" \
"--with-ssl" \
"--enable-ssl" \
"--enable-deflate" \
"--with-pcre" \
"--with-included-apr" \
"--with-apr-util" \
"--enable-mpms-shared=all" \
"--with-mpm=prefork" \
"--enable-remoteip" \
"$@"


查看Apache现在使用的MPM模式:
[root@localhost ~]# /usr/local/apache/bin/httpd -V
Server version: Apache/2.4.20 (Unix)
Server built:   Jun 25 2020 01:46:46
Server's Module Magic Number: 20120211:57
Server loaded:  APR 1.5.2, APR-UTIL 1.5.4
Compiled using: APR 1.5.2, APR-UTIL 1.5.4
Architecture:   64-bit
Server MPM:     prefork
  threaded:     no
    forked:     yes (variable process count)
Server compiled with....
-D APR_HAS_SENDFILE
-D APR_HAS_MMAP
-D APR_HAVE_IPV6 (IPv4-mapped addresses enabled)
-D APR_USE_SYSVSEM_SERIALIZE
-D APR_USE_PTHREAD_SERIALIZE
-D SINGLE_LISTEN_UNSERIALIZED_ACCEPT
-D APR_HAS_OTHER_CHILD
-D AP_HAVE_RELIABLE_PIPED_LOGS
-D DYNAMIC_MODULE_LIMIT=256
-D HTTPD_ROOT="/usr/local/apache"
-D SUEXEC_BIN="/usr/local/apache/bin/suexec"
-D DEFAULT_PIDLOG="logs/httpd.pid"
-D DEFAULT_SCOREBOARD="logs/apache_runtime_status"
-D DEFAULT_ERRORLOG="logs/error_log"
-D AP_TYPES_CONFIG_FILE="conf/mime.types"
-D SERVER_CONFIG_FILE="conf/httpd.conf"


笺注:Apache2.4有三种稳定的MPM(Multi-Processing Module,多进程处理模块)模式,分别是prefork、worker、event。

prefork是比较古老而又稳定的MPM模式,特点是每个进程都是单线程,在一个时间点只能处理一个连接,需要启动大量的进程来处理高并发的请求。


在Apache的主配置文件中指明使用prefork:
[root@localhost ~]# cat /usr/local/apache/conf/httpd.conf |grep 'mpm_'
#LoadModule mpm_event_module modules/mod_mpm_event.so
LoadModule mpm_prefork_module modules/mod_mpm_prefork.so


MPM模式的配置文件:
[root@localhost ~]# cat /usr/local/apache/conf/httpd.conf |grep -A1 "MPM specific"
# Server-pool management (MPM specific)
Include conf/extra/httpd-mpm.conf

[root@localhost ~]# vi /usr/local/apache/conf/extra/httpd-mpm.conf
prefork MPM:
图片1.png
2021-3-16 00:40


笺注:
MaxRequestWorkers 表示的是最大子进程数(即最大并发连接数),任何高于这个数量的连接,都会放入等待队列中;
Apache的MPM模式prefork,对高并发的支持稍差;


测试:
[root@localhost ~]# ab
-bash: ab: command not found

[root@localhost ~]# yum -y install httpd-tools


[root@localhost ~]# ab -n 1000 -c 250 http://192.168.168.130/phpinfo.php  #指定并发请求数为250,总请求数为1000,对http://192.168.168.130/phpinfo.php进行压力测试
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 192.168.168.130 (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests


Server Software:        Apache
Server Hostname:        192.168.168.130
Server Port:            80

Document Path:          /phpinfo.php
Document Length:        87189 bytes

Concurrency Level:      250
Time taken for tests:   1.567 seconds
Complete requests:      1000
Failed requests:        105
   (Connect: 0, Receive: 0, Length: 105, Exceptions: 0)
Write errors:           0
Total transferred:      87531723 bytes
HTML transferred:       87351828 bytes
Requests per second:    638.18 [#/sec] (mean)
Time per request:       391.741 [ms] (mean)
Time per request:       1.567 [ms] (mean, across all concurrent requests)
Transfer rate:          54551.44 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    9  77.2      0    1000
Processing:     2  276 416.2    145    1550
Waiting:        0  271 416.3    140    1549
Total:         16  285 422.3    146    1561

Percentage of the requests served within a certain time (ms)
  50%    146
  66%    150
  75%    152
  80%    153
  90%   1443
  95%   1512
  98%   1543
  99%   1553
100%   1561 (longest request)

笺注:可以看到每秒最大处理请求次数为638.18次,每个请求响应时间是1.567毫秒。





#######

假如MaxRequestWorkers的值超过256:
[root@localhost ~]# vi /usr/local/apache/conf/extra/httpd-mpm.conf
prefork MPM:
图片2.png
2021-3-16 00:42



[root@localhost ~]# service httpd restart
restart apache... AH00180: WARNING: MaxRequestWorkers of 300 exceeds ServerLimit value of 256 servers, decreasing MaxRequestWorkers to 256. To increase, please see the ServerLimit directive.
done


修改ServerLimit的值为300
prefork MPM:
<IfModule mpm_prefork_module>
    StartServers             5
    MinSpareServers          5
    MaxSpareServers         10
    ServerLimit            300
    MaxRequestWorkers      300
    MaxConnectionsPerChild   0
</IfModule>

注释:
ServerLimit表示Apache允许创建的最大进程数,默认为256;
MaxRequestWorkers的值不可以大于ServerLimit的值;
ServerLimit、MaxRequestWorkers的最大值为200000,超过20万的话,在重启Apache时会报错;
在服务器资源(CPU、内存等等)充足的情况下,ServerLimit、MaxRequestWorkers的值可以调大一些;


[root@localhost ~]# service httpd restart
restart apache...  done


加大并发请求数,再次测试:
[root@localhost ~]# ab -n 1000 -c 300 http://192.168.168.130/phpinfo.php #指定并发请求数为300,总请求数为1000,对http://192.168.168.130/phpinfo.php进行压力测试
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 192.168.168.130 (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests


Server Software:        Apache
Server Hostname:        192.168.168.130
Server Port:            80

Document Path:          /phpinfo.php
Document Length:        87189 bytes

Concurrency Level:      300
Time taken for tests:   13.990 seconds
Complete requests:      1000
Failed requests:        94
   (Connect: 0, Receive: 0, Length: 94, Exceptions: 0)
Write errors:           0
Total transferred:      87498966 bytes
HTML transferred:       87319250 bytes
Requests per second:    71.48 [#/sec] (mean)
Time per request:       4197.027 [ms] (mean)
Time per request:       13.990 [ms] (mean, across all concurrent requests)
Transfer rate:          6107.77 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    5  63.1      0    1000
Processing:     1 1167 2763.6    144   13980
Waiting:        0 1163 2763.8    139   13979
Total:         15 1173 2763.3    144   13983

Percentage of the requests served within a certain time (ms)
  50%    144
  66%    149
  75%    151
  80%    154
  90%   4719
  95%   4773
  98%  13962
  99%  13975
100%  13983 (longest request)

笺注:可以看到每秒最大处理请求次数为71.48次,每个请求响应时间是13.990毫秒。



加大并发请求数,再次测试:
[root@localhost ~]# ab -n 1000 -c 400 http://192.168.168.130/phpinfo.php  #指定并发请求数为400,总请求数为1000,对http://192.168.168.130/phpinfo.php进行压力测试
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 192.168.168.130 (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
apr_poll: The timeout specified has expired (70007)
Total of 759 requests completed





相关文章:
Apache2.4的MPM模式(event/prefork)
Nginx的并发连接数

返回列表