本节内容:
apache2中的worker模式。
说明:
apache mpm worker,即多核 cpu 下的进程/线程混合调度机制,是apache 在2.2版本之后引入的。
官方介绍:
This Multi-Processing Module (MPM) implements a hybrid multi-process multi-threaded server. By using threads to serve requests, it is able to serve a large number of requests with fewer system resources than a process-ba
sed server. However, it retains much of the stability of a process-based server by keeping multiple processes available, each with many threads.
worker 模式的最大优点:高处理效率。
出于稳定和跨平台的考虑,apache2 默认编译 为 prefork模式(多进程模式)。
所以,要使用 worker模式,需要对apache服务器进行相关配置与调整。
一,apache worker模式的编译和安装:
复制代码 代码示例:
#有关参数的解释和更多参数,可使用 --help 参数 获得帮助
[root@jbxue httpd-2.2.17]$ ./configure --prefix=/usr/local/apache2worker --enable-so --with-mpm=worker
[root@jbxue httpd-2.2.17]$ make clean
[root@jbxue httpd-2.2.17]$ make
[root@jbxue httpd-2.2.17]$ su
[root@
redhatFancp httpd-2.2.17]$ mkdir /usr/local/apache2worker
[root@RedhatFancp httpd-2.2.17]$ chown fancp /usr/local/apache2worker
[root@RedhatFancp httpd-2.2.17]$ exit
[root@jbxue httpd-2.2.17]$ make install
[root@jbxue httpd-2.2.17]$ /usr/local/apache2worker/bin/apachectl start
1,查看到五个进程:
复制代码 代码示例:
[root@jbxue bin]$ ps -ef |grep httpd
fancp 15242 1 0 23:19 ? 00:00:00 /usr/local/apache2worker/bin/httpd -k start
fancp 15243 15242 0 23:19 ? 00:00:00 /usr/local/apache2worker/bin/httpd -k start
fancp 15244 15242 0 23:19 ? 00:00:00 /usr/local/apache2worker/bin/httpd -k start
fancp 15246 15242 0 23:19 ? 00:00:00 /usr/local/apache2worker/bin/httpd -k start
fancp 15248 15242 0 23:19 ? 00:00:00 /usr/local/apache2worker/bin/httpd -k start
fancp 15329 4762 0 23:19 pts/1 00:00:00 grep httpd
从以上进程列表,发现进程 15242 的父进程 pid 是1,同时它也是 进程 15243,15244,15246, 15248 的父进程。
为什么是5个进程呢?5个进程之间有什么关系呢?
mpm_default.h 中设置了默认进程数(DEFAULT_START_DAEMON),最大进程数(DEFAULT_MAX_FREE_DAEMON),最小进程数(DEFAULT_MIN_FREE_DAEMON)。
在没有参数配置的情况下,会应用这些值。在 2.2.17 的版本中, DEFAULT_START_DAEMON 的值是 3。
所以,上面的5个进程的组成是:一个listen 进程,3个 recv进程.还有一个进程,我就不知道啦。
2,修改配置文件 http.conf,加入以下内容,设置主线程为1:
复制代码 代码示例:
# worker MPM
# StartServers: initial number of server processes to start
# MaxClients: maximum number of simultaneous client connections
# MinSpareThreads: minimum number of worker threads which are kept spare
# MaxSpareThreads: maximum number of worker threads which are kept spare
# ThreadsPerChild: constant number of worker threads in each server process
# MaxRequestsPerChild: maximum number of requests a server process serves
<IfModule mpm_worker_module>
StartServers 1
MaxClients 15
MinSpareThreads 1
MaxSpareThreads 1
ThreadsPerChild 1
MaxRequestsPerChild 0
</IfModule>
3,查看进程:
复制代码 代码示例:
[root@jbxue httpd-2.2.17]$ /usr/local/apache2worker/bin/apachectl restart
[root@jbxue httpd-2.2.17]$ ps -ef |grep httpd
fancp 15242 1 0 23:19 ? 00:00:00 /usr/local/apache2worker/bin/httpd -k start
fancp 16035 15242 0 23:45 ? 00:00:00 /usr/local/apache2worker/bin/httpd -k start
fancp 16036 15242 0 23:45 ? 00:00:00 /usr/local/apache2worker/bin/httpd -k start
fancp 16041 4762 0 23:45 pts/1 00:00:00 grep httpd