Httpd服务入门知识-Httpd服务常见配置案例之MPM( Multi-Processing Module)多路处理模块
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
一.MPM( Multi-Processing Module)多路处理模块
MPM( Multi-Processing Module)多路处理模块分为:"prefork, worker, event",接下来我们查看一下如何配置吧。
1>.prefork的配置
StartServers
启动服务时默认会启动8个进程
MinSpareServers
最少空闲的进程数默认为5,当响应请求时就不需要在浪费创建进程的时间啦,而是直接让空闲进程去响应,因此咱们可以指定最少的进程数。
MaxSpareServers
最大进程空闲数默认为20,当所有的进程都处理完请求后,可能会有很多进程空闲,此时会释放部分进程,因此咱们可以指定最多的空闲进程数。
ServerLimit
最多开启的进程数默认为256 最多进程数的最大值为20000
MaxRequestsPerChild
每个子进程最多处理默认为4000个请求,即子进程最多能处理的请求数量为4000 。在处理MaxRequestsPerChild个请求之后,子进程将会被父进程终止,这时候子进程占用的内存就会释放(为0时永远不释放),在重新开启新的子进程来响应。
2>.worker的配置
ServerLimit 最多开启的进程数默认为16 最多进程数的最大值为20000 StartServers 启动服务时默认会启动2个进程
MaxRequestWorkers 150 每个子线程最多处理的请求数默认为150.
MinSpareThreads 25 最小空闲的线程数默认是25. MaxSpareThreads 75 最大空闲的线程数默认是75. ThreadsPerChild 25 每个子进程最多开放的线程数默认为25.
二.切换使用的MPM
1>.查看默认的多路处理模块
[root@node101.yinzhengjie.org.cn ~]# cat /etc/httpd/conf.modules.d/00-mpm.conf #启用要启用的MPM相关的LoadModule指令即可 # Select the MPM module which should be used by uncommenting exactly # one of the following LoadModule lines: # prefork MPM: Implements a non-threaded, pre-forking web server # See: http://httpd.apache.org/docs/2.4/mod/prefork.html LoadModule mpm_prefork_module modules/mod_mpm_prefork.so # worker MPM: Multi-Processing Module implementing a hybrid # multi-threaded multi-process web server # See: http://httpd.apache.org/docs/2.4/mod/worker.html # #LoadModule mpm_worker_module modules/mod_mpm_worker.so # event MPM: A variant of the worker MPM with the goal of consuming # threads only for connections with active processing # See: http://httpd.apache.org/docs/2.4/mod/event.html # #LoadModule mpm_event_module modules/mod_mpm_event.so [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# ss -ntl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:80 *:* LISTEN 0 128 *:22 *:* LISTEN 0 128 :::22 :::* [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# ps auxf | grep apache root 5754 0.0 0.0 112708 984 pts/0 S+ 16:05 0:00 | \_ grep --color=auto apache apache 5577 0.0 0.0 224184 3196 ? S 15:08 0:00 \_ /usr/sbin/httpd -DFOREGROUND apache 5578 0.0 0.0 224184 3724 ? S 15:08 0:00 \_ /usr/sbin/httpd -DFOREGROUND apache 5579 0.0 0.0 224184 3716 ? S 15:08 0:00 \_ /usr/sbin/httpd -DFOREGROUND apache 5580 0.0 0.0 224184 3716 ? S 15:08 0:00 \_ /usr/sbin/httpd -DFOREGROUND apache 5581 0.0 0.0 224184 3196 ? S 15:08 0:00 \_ /usr/sbin/httpd -DFOREGROUND apache 5625 0.0 0.0 224184 3660 ? S 15:13 0:00 \_ /usr/sbin/httpd -DFOREGROUND [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# pstree -p | grep httpd |-httpd(5026)-+-httpd(5577) | |-httpd(5578) | |-httpd(5579) | |-httpd(5580) | |-httpd(5581) | `-httpd(5625) [root@node101.yinzhengjie.org.cn ~]#
2>.切换至worker模块
[root@node101.yinzhengjie.org.cn ~]# egrep -v "^ *#|^$" /etc/httpd/conf.modules.d/00-mpm.conf #默认使用的就是prefock处理模块 LoadModule mpm_prefork_module modules/mod_mpm_prefork.so [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# vim /etc/httpd/conf.modules.d/00-mpm.conf [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# egrep -v "^ *#|^$" /etc/httpd/conf.modules.d/00-mpm.conf #将默认的模块注释掉,切换成worker处理模块 LoadModule mpm_worker_module modules/mod_mpm_worker.so [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# systemctl reload httpd [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# ss -ntl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:80 *:* LISTEN 0 128 *:22 *:* LISTEN 0 128 :::22 :::* [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# ps auxf | grep apache root 5976 0.0 0.0 112708 984 pts/0 S+ 16:09 0:00 | \_ grep --color=auto apache apache 5887 0.0 0.0 224256 2908 ? S 16:08 0:00 \_ /usr/sbin/httpd -DFOREGROUND apache 5888 0.0 0.0 511084 3420 ? Sl 16:08 0:00 \_ /usr/sbin/httpd -DFOREGROUND apache 5889 0.0 0.0 576620 3420 ? Sl 16:08 0:00 \_ /usr/sbin/httpd -DFOREGROUND apache 5890 0.0 0.0 576620 3428 ? Sl 16:08 0:00 \_ /usr/sbin/httpd -DFOREGROUND [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# pstree -p | grep http #不难发现有主进程多出来多个子进程,而子进程有分裂出多个线程 |-httpd(5788)-+-httpd(5789) | |-httpd(5790)-+-{httpd}(5794) | | |-{httpd}(5795) | | |-{httpd}(5796) | | |-{httpd}(5797) | | |-{httpd}(5798) | | |-{httpd}(5799) | | |-{httpd}(5800) | | |-{httpd}(5801) | | |-{httpd}(5803) | | |-{httpd}(5804) | | |-{httpd}(5805) | | |-{httpd}(5806) | | |-{httpd}(5807) | | |-{httpd}(5809) | | |-{httpd}(5812) | | |-{httpd}(5814) | | |-{httpd}(5816) | | |-{httpd}(5818) | | |-{httpd}(5820) | | |-{httpd}(5822) | | |-{httpd}(5824) | | |-{httpd}(5826) | | |-{httpd}(5828) | | |-{httpd}(5830) | | |-{httpd}(5832) | | `-{httpd}(5834) | |-httpd(5791)-+-{httpd}(5808) | | |-{httpd}(5810) | | |-{httpd}(5811) | | |-{httpd}(5813) | | |-{httpd}(5815) | | |-{httpd}(5817) | | |-{httpd}(5819) | | |-{httpd}(5821) | | |-{httpd}(5823) | | |-{httpd}(5825) | | |-{httpd}(5827) | | |-{httpd}(5829) | | |-{httpd}(5831) | | |-{httpd}(5833) | | |-{httpd}(5835) | | |-{httpd}(5836) | | |-{httpd}(5837) | | |-{httpd}(5838) | | |-{httpd}(5839) | | |-{httpd}(5840) | | |-{httpd}(5841) | | |-{httpd}(5842) | | |-{httpd}(5843) | | |-{httpd}(5844) | | |-{httpd}(5845) | | `-{httpd}(5846) | `-httpd(5792)-+-{httpd}(5848) | |-{httpd}(5849) | |-{httpd}(5850) | |-{httpd}(5851) | |-{httpd}(5852) | |-{httpd}(5853) | |-{httpd}(5854) | |-{httpd}(5855) | |-{httpd}(5856) | |-{httpd}(5857) | |-{httpd}(5858) | |-{httpd}(5859) | |-{httpd}(5860) | |-{httpd}(5861) | |-{httpd}(5862) | |-{httpd}(5863) | |-{httpd}(5864) | |-{httpd}(5865) | |-{httpd}(5866) | |-{httpd}(5867) | |-{httpd}(5868) | |-{httpd}(5869) | |-{httpd}(5870) | |-{httpd}(5871) | |-{httpd}(5872) | `-{httpd}(5873) [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# httpd -M | grep worker #我们发现worker模块已被加载啦~ mpm_worker_module (shared) [root@node101.yinzhengjie.org.cn ~]#
三.模块加载
1>.查看静态编译的模块
[root@node101.yinzhengjie.org.cn ~]# httpd -l Compiled in modules: core.c mod_so.c http_core.c [root@node101.yinzhengjie.org.cn ~]#
2>.查看静态编译及动态装载的模块
[root@node101.yinzhengjie.org.cn ~]# httpd -M
Loaded Modules:
core_module (static)
so_module (static)
http_module (static)
access_compat_module (shared)
actions_module (shared)
alias_module (shared)
allowmethods_module (shared)
auth_basic_module (shared)
auth_digest_module (shared)
authn_anon_module (shared)
authn_core_module (shared)
authn_dbd_module (shared)
authn_dbm_module (shared)
authn_file_module (shared)
authn_socache_module (shared)
authz_core_module (shared)
authz_dbd_module (shared)
authz_dbm_module (shared)
authz_groupfile_module (shared)
authz_host_module (shared)
authz_owner_module (shared)
authz_user_module (shared)
autoindex_module (shared)
cache_module (shared)
cache_disk_module (shared)
data_module (shared)
dbd_module (shared)
deflate_module (shared)
dir_module (shared)
dumpio_module (shared)
echo_module (shared)
env_module (shared)
expires_module (shared)
ext_filter_module (shared)
filter_module (shared)
headers_module (shared)
include_module (shared)
info_module (shared)
log_config_module (shared)
logio_module (shared)
mime_magic_module (shared)
mime_module (shared)
negotiation_module (shared)
remoteip_module (shared)
reqtimeout_module (shared)
rewrite_module (shared)
setenvif_module (shared)
slotmem_plain_module (shared)
slotmem_shm_module (shared)
socache_dbm_module (shared)
socache_memcache_module (shared)
socache_shmcb_module (shared)
status_module (shared)
substitute_module (shared)
suexec_module (shared)
unique_id_module (shared)
unixd_module (shared)
userdir_module (shared)
version_module (shared)
vhost_alias_module (shared)
dav_module (shared)
dav_fs_module (shared)
dav_lock_module (shared)
lua_module (shared)
mpm_worker_module (shared)
proxy_module (shared)
lbmethod_bybusyness_module (shared)
lbmethod_byrequests_module (shared)
lbmethod_bytraffic_module (shared)
lbmethod_heartbeat_module (shared)
proxy_ajp_module (shared)
proxy_balancer_module (shared)
proxy_connect_module (shared)
proxy_express_module (shared)
proxy_fcgi_module (shared)
proxy_fdpass_module (shared)
proxy_ftp_module (shared)
proxy_http_module (shared)
proxy_scgi_module (shared)
proxy_wstunnel_module (shared)
systemd_module (shared)
cgid_module (shared)
[root@node101.yinzhengjie.org.cn ~]#