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-based 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模式,我們還需要多費些手腳。
worker 模式的編譯和安裝:
#有關參數的解釋和更多參數,可使用 --help 參數 獲得協助
[fancp@RedhatFancp httpd-2.2.17]$ ./configure --prefix=/usr/local/apache2worker --enable-so --with-mpm=worker
[fancp@RedhatFancp httpd-2.2.17]$ make clean[fancp@RedhatFancp httpd-2.2.17]$ make[fancp@RedhatFancp 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[fancp@RedhatFancp httpd-2.2.17]$ make install[fancp@RedhatFancp httpd-2.2.17]$ /usr/local/apache2worker/bin/apachectl start
可以看到五個進程。
fancp@RedhatFancp 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進程.還有一個進程,我就不知道啦。
修改設定檔 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>
查看進程
[fancp@RedhatFancp httpd-2.2.17]$ /usr/local/apache2worker/bin/apachectl restart
[fancp@RedhatFancp 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
[fancp@RedhatFancp httpd-2.2.17]$