Apache optimization: modify the maximum number of concurrent connections

Apache is a cross-platform web server, which is widely used in various fields of computer technology due to its simple, efficient, stable and secure characteristics. Now, with its huge number of users, Apache has become the number one web server in terms of users.

Nevertheless, in the actual production environment, it is still impossible for us to directly use the default configuration of Apache as a server. After all, in order to use the Apache server more reasonably, we should all make some necessary adjustments to the default configuration of Apache according to our actual needs. In the optimization configuration process for Apache, it is particularly important to modify the maximum number of concurrent connections of Apache.

Before modifying the maximum number of concurrent connections of Apache, we need to know some knowledge about Apache in advance.

As we all know, Apache is a cross-platform, modular design server. In order to cope with the various requirements generated by different platforms and different environments, and to achieve the best results in specific platforms or environments, Apache also provides basic functions of the web server (port binding, receiving requests, etc.) The modular design is also adopted. The core module of Apache is called the Multi-Processing Module (MPM).

Apache provides several different MPM modules for different operating systems, such as: mpm_beos, mpm_event, mpm_netware, mpmt_os2, mpm_prefork, mpm_winnt, mpm_worker. If conditions permit, we can compile the specified MPM module into our own Apache according to actual needs (Apache's source code is open, allowing users to compile by themselves). However, if we do not choose at compile time, Apache will select the corresponding MPM module according to the following table according to different operating systems, which is also the MPM module recommended by Apache for different platforms.

Default MPM Modules on Different Operating Systems OS MPM Module Descriptions
Windows mpm_winnt No introduction needed :)
Unix/Linux mpm_prefork No introduction needed :)
BeOS mpm_beos A multimedia operating system developed by Be Company, the official version has stopped updating.
Netware mpm_netware A network operating system launched by NOVELL
OS/2 mpmt_os2 An operating system originally developed by Microsoft and IBM, but now developed by IBM alone (Microsoft dropped OS/2 in favor of Windows)

The mpm_event module can be regarded as a variant of the mpm_worker module, but it is experimental and generally not recommended.

Of course, Apache also provides finished Apaches that have been compiled with corresponding MPM modules according to different operating systems on their official website. You can click here to enter the Apache official website to download.

In addition, if we want to know what kind of MPM module is used inside an Apache, we can enter it from the command line Apache安装目录\bin, and then type the command httpd -lto see what kind of MPM module is currently used inside Apache.

Use the httpd -l command to view compiled modulesUse the httpd -l command to view compiled modules

Since operating systems such as BeOS, NetWare, and OS/2 are not common in normal development work, here we mainly explain the MPM modules on Windows and Unix/Linux operating systems. On Windows and Unix/Linux operating systems, there are three main types of MPM modules: mpm_winnt, mpm_prefork, and mpm_worker.

mpm_prefork module

mpm_prefork模块主要应用于Unix/Linux平台的Apache服务器,其主要工作方式是:当Apache服务器启动后,mpm_prefork模块会预先创建多个子进程(默认为5个),当接收到客户端的请求后,mpm_prefork模块再将请求转交给子进程处理,并且每个子进程同时只能用于处理单个请求。如果当前的请求数将超过预先创建的子进程数时,mpm_prefork模块就会创建新的子进程来处理额外的请求。Apache总是试图保持一些备用的或者是空闲的子进程用于迎接即将到来的请求。这样客户端的请求就不需要在接收后等候子进程的产生。

由于在mpm_prefork模块中,每个请求对应一个子进程,因此其占用的系统资源相对其他两种模块而言较多。不过mpm_prefork模块的优点在于它的每个子进程都会独立处理对应的单个请求,这样,如果其中一个请求出现问题就不会影响到其他请求。同时,mpm_prefork模块可以应用于不具备线程安全的第三方模块(比如PHP的非线程安全版本),且在不支持线程调试的平台上易于调试。此外,mpm_prefork模块还具有比mpm_worker模块更高的稳定性。

mpm_worker模块

mpm_worker模块也主要应用于Unix/Linux平台的Apache服务器,它可以看作是mpm_prefork模块的改进版。mpm_worker模块的工作方式与mpm_prefork模块类似。不过,由于处理相同请求的情况下,基于进程(例如mpm_prefork)比基于线程的处理方式占用的系统资源要多。因此,与mpm_prefork模块不同的是,mpm_worker模块会让每个子进程创建固定数量的服务线程和一个监听线程,并让每个服务线程来处理客户端的请求,监听线程用于监听接入请求并将其传递给服务线程处理和应答。Apache总是试图维持一个备用或是空闲的服务线程池。这样,客户端无须等待新线程或新进程的建立即可得到处理。

mpm_prefork模块相比,mpm_worker模块可以进一步减少系统资源的开销。再加上它也使用了多进程,每个进程又有多个线程,因此它与完全基于线程的处理方式相比,又增加了一定的稳定性。

mpm_winnt模块

mpm_winnt模块是专门针对Windows操作系统而优化设计的MPM模块。它只创建一个单独的子进程,并在这个子进程中轮流产生多个线程来处理请求。

修改MPM模块配置

在对Apache的MPM模块具备一定了解后,我们就可以针对不同的MPM模块来修改Apache的最大并发连接数配置了。

1.启用MPM模块配置文件

Apace安装目录/conf/extra目录中有一个名为httpd-mpm.conf的配置文件。该文件主要用于进行MPM模块的相关配置。不过,在默认情况下,Apache的MPM模块配置文件并没有启用。因此,我们需要在httpd.conf文件中启用该配置文件,如下所示:

# Server-pool management (MPM specific)
Include conf/extra/httpd-mpm.conf (去掉该行前面的注释符号"#")

2.修改MPM模块配置文件中的相关配置

在启动MPM模块配置文件后,我们就可以使用文本编辑器打开该配置文件,我们可以看到,在该配置文件中有许多<IfModule>配置节点,如下图所示:

The corresponding configuration will take effect only when Apache uses the corresponding MPM module只有Apache使用对应MPM模块时,对应配置才会生效

此时,我们就需要根据当前Apache服务器所使用的MPM模块,来修改对应<IfModule>节点下的参数配置。首先,我们来看看mpm_winnt模块下的默认配置:

#由于mpm_winnt模块只会创建1个子进程,因此这里对单个子进程的参数设置就相当于对整个Apache的参数设置。

<IfModule mpm_winnt_module>
ThreadsPerChild      150 #推荐设置:小型网站=1000 中型网站=1000~2000 大型网站=2000~3500
MaxRequestsPerChild    0 #推荐设置:小=10000 中或大=20000~100000
</IfModule>

对应的配置参数作用如下:

ThreadsPerChild
每个子进程的最大并发线程数。
MaxRequestsPerChild
每个子进程允许处理的请求总数。如果累计处理的请求数超过该值,该子进程将会结束(然后根据需要确定是否创建新的子进程),该值设为0表示不限制请求总数(子进程永不结束)。

该参数建议设为非零的值,可以带来以下两个好处:

  1. 可以防止程序中可能存在的内存泄漏无限进行下去,从而耗尽内存。
  2. 给进程一个有限寿命,从而有助于当服务器负载减轻的时候减少活动进程的数量。

注意:在以上涉及到统计请求数量的参数中,对于KeepAlive的连接,只有第一个请求会被计数。

接着,我们再来看看mpm_perfork模块和mpm_worker模块下的默认配置:

#mpm_perfork模块

<IfModule mpm_prefork_module>
StartServers          5 #推荐设置:小=默认 中=20~50 大=50~100
MinSpareServers       5 #推荐设置:与StartServers保持一致
MaxSpareServers      10 #推荐设置:小=20 中=30~80 大=80~120 
MaxClients          150 #推荐设置:小=500 中=500~1500 大型=1500~3000
MaxRequestsPerChild   0 #推荐设置:小=10000 中或大=10000~500000
(此外,还需额外设置ServerLimit参数,该参数最好与MaxClients的值保持一致。)
</IfModule>
#mpm_worker模块

<IfModule mpm_worker_module>
StartServers          2 #推荐设置:小=默认 中=3~5 大=5~10
MaxClients          150 #推荐设置:小=500 中=500~1500 大型=1500~3000
MinSpareThreads      25 #推荐设置:小=默认 中=50~100 大=100~200
MaxSpareThreads      75 #推荐设置:小=默认 中=80~160 大=200~400 
ThreadsPerChild      25 #推荐设置:小=默认 中=50~100 大型=100~200
MaxRequestsPerChild   0 #推荐设置:小=10000 中或大=10000~50000
(此外,如果MaxClients/ThreadsPerChild大于16,还需额外设置ServerLimit参数,ServerLimit必须大于等于 MaxClients/ThreadsPerChild 的值。)
</IfModule>

对应的配置参数作用如下:

StartServers
启动Apache时创建的子进程数。
MinSpareServers
处于空闲状态的最小子进程数。

所谓空闲子进程是指没有正在处理请求的子进程。如果当前空闲子进程数少于MinSpareServers,那么Apache将以最大每秒一个的速度产生新的子进程。只有在非常繁忙机器上才需要调整这个参数。此值不宜过大。

MaxSpareServers
处于空闲状态的最大子进程数。

只有在非常繁忙机器上才需要调整这个参数。此值不宜过大。如果你将该指令的值设置为比MinSpareServers小,Apache将会自动将其修改成MinSpareServers+1

MaxClients
允许同时连接的最大请求数量。

任何超过MaxClients限制的请求都将进入等待队列,直到达到ListenBacklog指令限制的最大值为止。

对于非线程型的MPM(也就是mpm_prefork),MaxClients表示可以用于处理客户端请求的最大子进程数量,默认值是256。要增大这个值,你必须同时增大ServerLimit

对于线程型或者混合型的MPM(也就是mpm_beosmpm_worker),MaxClients表示可以用于处理客户端请求的最大线程数量。线程型的mpm_beos的默认值是50。对于混合型的MPM默认值是16(ServerLimit)乘以25(ThreadsPerChild)的结果。因此要将MaxClients增加到超过16个进程才能提供的时候,你必须同时增加ServerLimit的值。

MinSpareThreads
处于空闲状态的最小线程数。

不同的MPM对这个指令的处理是不一样的:

mpm_worker的默认值是75。这个MPM将基于整个服务器监视空闲线程数。如果服务器中总的空闲线程数太少,子进程将产生新的空闲线程。mpm_netware的默认值是10。既然这个MPM只运行单独一个子进程,此MPM当然亦基于整个服务器监视空闲线程数。mpm_beosmpmt_os2的工作方式与mpm_netware差不多,mpm_beos的默认值是1;mpmt_os2的默认值是5。

MaxSpareThreads
处于空闲状态的最大线程数。

不同的MPM对这个指令的处理是不一样的:

mpm_workerThe default value is 250. This MPM will monitor the number of idle threads based on the entire server. If the total number of idle threads in the server is too high, the child process will kill the excess idle threads. mpm_netwareThe default value is 100. Since this MPM only runs a single child process, this MPM of course also monitors the number of idle threads based on the entire server. mpm_beosand mpmt_os2works in much the same way mpm_netware, with mpm_beosa default value of 50; mpmt_os2and a default value of 10.

Note : ServerLimitIndicates the maximum number of processes that Apache is allowed to create. It's worth noting that Apache has a hard limit ServerLimit 20000(for mpm_preforkmodules ServerLimit 200000) internally at compile time. You cannot go beyond this limit.
Be especially careful when using this command. If you ServerLimitset it to a value much higher than what is actually needed, too much shared memory will be allocated. If you set ServerLimitand MaxClientsexceed the processing power of your system, Apache may fail to start, or the system will become unstable.

Note : When configuring related parameters, please ensure that the server has sufficient hardware performance (for example: CPU, memory, etc.). If it is found that the memory usage of the server increases with the running time of the server since it MaxRequestsPerChildstarts the problem in the program.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326528475&siteId=291194637