Apache的模块化特性
1、前言
Apache是一个跨平台的web服务器,由于其简单高效、稳定安全的特性,被广泛应用于计算机技术的各个领域。现在,Apache凭借其庞大的用户数,已成为用户数排名第一的web服务器。
尽管如此,在实际的生产环境中,我们仍然不可能直接使用默认配置的Apache来充当服务器。毕竟,为了更充分合理地利用Apache服务器,我们都应该根据自己的实际需要对Apache的默认配置作出一些必要的调整。而针对Apache的优化配置过程中,修改Apache的最大并发连接数就显得尤为重要
2、这里我们就用到了一个动态共享模块:多路处理模块MPM
对于现在的Apache来说,版本已经升级到了Apache2.4.x的版本,我在写文章的时候用到的版本是Apache/2.4.29 (Unix)版本
截止到目前最新的Apache2.4.29,Apache总共支持三种MPM(多进程处理模块)模式,分别是Prefork、worker及event。这三种模式代表了Apache的演变和发展
Apache2.2中,默认启用prefork模式,同时引进了实验性质的event模式;
Apache2.4中,正式支持并且默认使用了event模式。并且可以在编译的时候增加了选项enable-mpms-shared来编译MPM,并在编译后可以动态加载。
3、那我们应该怎样的去查看我们的MPM的工作模式呢:
Apache2.2的版本:可以通过apachectl -V查看当前Apache的工作模式
Apache2.4的版本:可以通过apachectl -V 或者 httpd -V 查看当前Apache的工作模式
[root@BrianZhu bin]# pwd /usr/local/apache/bin # Apache的安装目录 [root@BrianZhu bin]# ./httpd -V # 查看工作模式的命令 Server version: Apache/2.4.29 (Unix) # Apache的版本号 Server built: Mar 13 2018 11:54:38 Server's Module Magic Number: 20120211:68 Server loaded: APR 1.6.3, APR-UTIL 1.6.1 Compiled using: APR 1.6.3, APR-UTIL 1.6.1 Architecture: 64-bit Server MPM: event # MPM的工作模式(默认) threaded: yes (fixed thread count) 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"
也可以通过静态模块的查看方式:
[root@BrianZhu extra]# /usr/local/apache/bin/apachectl -l Compiled in modules: core.c mod_so.c http_core.c event.c # 出现event.c就是event工作模式,出现prefork就是prefork工作模式
4、既然我们上面提到了工作模式有三种:分别是Prefork、worker及event,那这三种到底有什么区别呢:
这些工作模式的设置一般都会在Apache下面的conf下面的extra下面的httpd-mpm.conf 这个文件下,我的是在/usr/local/apache/conf/extra/httpd-mpm.conf中,下面是这个文件中的所有参数设置,在下面分开讲解的时候我会把没有配置参数单独拿出来解释
# # Server-Pool Management (MPM specific) # # # PidFile: The file in which the server should record its process # identification number when it starts. # # Note that this is the default PidFile for most MPMs. # <IfModule !mpm_netware_module> PidFile "logs/httpd.pid" </IfModule> # # Only one of the below sections will be relevant on your # installed httpd. Use "apachectl -l" to find out the # active mpm. # # prefork MPM (prefork 工作模式) # StartServers: number of server processes to start # MinSpareServers: minimum number of server processes which are kept spare # MaxSpareServers: maximum number of server processes which are kept spare # MaxRequestWorkers: maximum number of server processes allowed to start # MaxConnectionsPerChild: maximum number of connections a server process serves # before terminating <IfModule mpm_prefork_module> StartServers 5 MinSpareServers 5 MaxSpareServers 10 MaxRequestWorkers 250 MaxConnectionsPerChild 0 </IfModule> # worker MPM (worker 工作模式) # StartServers: initial number of server processes to start # 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 # MaxRequestWorkers: maximum number of worker threads # MaxConnectionsPerChild: maximum number of connections a server process serves # before terminating <IfModule mpm_worker_module> StartServers 3 MinSpareThreads 75 MaxSpareThreads 250 ThreadsPerChild 25 MaxRequestWorkers 400 MaxConnectionsPerChild 0 </IfModule> # event MPM (event 工作模式) # StartServers: initial number of server processes to start # 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 # MaxRequestWorkers: maximum number of worker threads # MaxConnectionsPerChild: maximum number of connections a server process serves # before terminating <IfModule mpm_event_module> StartServers 3 MinSpareThreads 75 MaxSpareThreads 250 ThreadsPerChild 25 MaxRequestWorkers 400 MaxConnectionsPerChild 0 </IfModule> # NetWare MPM # ThreadStackSize: Stack size allocated for each worker thread # StartThreads: Number of worker threads launched at server startup # MinSpareThreads: Minimum number of idle threads, to handle request spikes # MaxSpareThreads: Maximum number of idle threads # MaxThreads: Maximum number of worker threads alive at the same time # MaxConnectionsPerChild: Maximum number of connections a thread serves. It # is recommended that the default value of 0 be set # for this directive on NetWare. This will allow the # thread to continue to service requests indefinitely. <IfModule mpm_netware_module> ThreadStackSize 65536 StartThreads 250 MinSpareThreads 25 MaxSpareThreads 250 MaxThreads 1000 MaxConnectionsPerChild 0 </IfModule> # OS/2 MPM # StartServers: Number of server processes to maintain # MinSpareThreads: Minimum number of idle threads per process, # to handle request spikes # MaxSpareThreads: Maximum number of idle threads per process # MaxConnectionsPerChild: Maximum number of connections per server process <IfModule mpm_mpmt_os2_module> StartServers 2 MinSpareThreads 5 MaxSpareThreads 10 MaxConnectionsPerChild 0 </IfModule> # WinNT MPM # ThreadsPerChild: constant number of worker threads in the server process # MaxConnectionsPerChild: maximum number of connections a server process serves <IfModule mpm_winnt_module> ThreadsPerChild 150 MaxConnectionsPerChild 0 </IfModule> # The maximum number of free Kbytes that every allocator is allowed # to hold without calling free(). In threaded MPMs, every thread has its own # allocator. When not set, or when set to zero, the threshold will be set to # unlimited. <IfModule !mpm_netware_module> MaxMemFree 2048 </IfModule> <IfModule mpm_netware_module> MaxMemFree 100 </IfModule>
1、Prefork工作模式
Prefork MPM实现了一个非线程、预派生的工作模式。它在Apache启动之初,就会预派生一些子进程,然后等待连接。可以减少频繁创建和销毁进程的开销,每个子进程只有一个线程。它成熟稳定,可以兼容新老模块,也不需要担心线程安全问题。但是一个进程相对地占用更多的资源,消耗大量内存,不擅长处理高并发的场景。
# prefork MPM # StartServers: number of server processes to start # MinSpareServers: minimum number of server processes which are kept spare # MaxSpareServers: maximum number of server processes which are kept spare # MaxRequestWorkers: maximum number of server processes allowed to start # MaxConnectionsPerChild: maximum number of connections a server process serves # before terminating <IfModule mpm_prefork_module> StartServers 5 # 启动服务器进程的数量 MinSpareServers 5 # 最少的服务器进程数量 MaxSpareServers 10 # 最大数量的服务器进程,这是备用的 MaxRequestWorkers 250 # 允许启动的服务器进程的最大数量 MaxConnectionsPerChild 0 # 服务器进程服务的最大连接数 </IfModule>
2、worker工作模式
与Prefork工作模式相比,worker使用了多进程和多线程的混合模式,worker模式也同样会预派生一些子进程,然后每个子进程创建一些线程,同时包括一个监听线程,每个请求过来会被分配到一个线程来服务。线程比进程更加轻量级,因为线程通常会共享父进程的内存地址的,因此内存占用会减少一些。
同时如果一个线程异常挂了,会导致父进程和它的其他正常子线程都挂了,这样也只会影响Apache的一部分,而不是整个服务。
缺点使必须考虑线程安全性,因为多个子进程是共享父进程的内存地址的。如果使用keep-alive的长连接方式,某个线程会被一直占据,也许中间没有任何请求,需要等到超时才会被释放。如果过多的线程被这样占据,也会导致在高并发下的无服务线程可用。
# worker MPM # StartServers: initial number of server processes to start # 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 # MaxRequestWorkers: maximum number of worker threads # MaxConnectionsPerChild: maximum number of connections a server process serves # before terminating <IfModule mpm_worker_module> StartServers 3 # 启动服务器进程的初始数量 MinSpareThreads 75 # 最少的工作线程数量 MaxSpareThreads 250 # 最大工作线程数,这些线程是备用的 ThreadsPerChild 25 # 每个服务器进程中的工作线程数量不变 MaxRequestWorkers 400 # 最大工作线程数 MaxConnectionsPerChild 0 # 服务器进程服务的最大连接数 </IfModule>
3、event 工作模式
和worker工作模式很像,最大的区别是解决了在keepalive场景下,长期被占用的线程的资源浪费问题,在event MPM中,会有一个专门的线程来管理这些keepalive线程,当有真实请求过来的时候,将请求传递给服务线程,执行完毕后,又允许它释放,这样增强了在高并发场景下的请求处理能力。
# event MPM # StartServers: initial number of server processes to start # 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 # MaxRequestWorkers: maximum number of worker threads # MaxConnectionsPerChild: maximum number of connections a server process serves # before terminating <IfModule mpm_event_module> StartServers 3 # 启动服务器进程的初始数量 MinSpareThreads 75 # 最少的工作线程数量 MaxSpareThreads 250 # 最大工作线程数,这些线程是备用的 ThreadsPerChild 25 # 每个服务器进程中的工作线程数量不变 MaxRequestWorkers 400 # 最大工作线程数 MaxConnectionsPerChild 0 # 服务器进程服务的最大连接数 </IfModule>
注:以上工作模式的配置参数的值都可以根据自己生产环境中的实际情况进行修改,下面的两个命令可以对你修改值做个响应的参考
其中httpd平均占用内存: ps aux|grep -v grep|awk '/httpd/{sum+=$6;n++};END{print sum/n}' httpd的进程数 ps -ef | grep httpd | wc -l