Apache (multi-processing Modules, multi-channel processing module) for managing network connection processing requests
First, Introduction
For operating system processing module,,,,,, mpm_beos
mpm_event
mpm_netware
mpmt_os2
mpm_prefork
mpm_winnt
mpm_worker
which
Linux systems use Mpm_worker mpm_prefork
Windows system uses MPM_WINNT
Second, view the machine's MPM mode
$ httpd-v|grep "Server MPM"
Server Mpm:prefork
$ httpd-l
Compiled in Modules:
Core.c
Prefork.c
Http_core.c
Mod_so.c
Third, performance optimization configuration
Httpd.conf Open MPM Configuration
# Server-pool Management (MPM specific)
Include conf/extra/httpd-mpm.conf
MPM configuration file/usr/local/apache/conf/extra/httpd-mpm.conf
Prefork Introduction
When the Apache server is started, the mpm_prefork
module will pre-create multiple child processes (by default, 5), and when a request is received from the client, the mpm_prefork
module then forwards the request to the child process, and each child process can only be used to process a single request. If the current number of requests exceeds the number of pre-created child processes, the mpm_prefork
module creates a new child process to handle the additional requests. Apache always tries to keep some spare or idle child processes in the process of meeting incoming requests. This way the client's request does not need to wait for the child process to be generated after receiving it.
Because in mpm_prefork
a module, each request corresponds to a child process, it consumes more system resources than the other two modules. The advantage of the module, however mpm_prefork
, is that each of its child processes handles the corresponding individual request independently, so that if one of the requests fails, it does not affect other requests. At the same time, mpm_prefork
modules can be applied to third-party modules that do not have thread safety (e.g., non-thread-safe versions of PHP) and are easy to debug on platforms that do not support thread debugging. In addition, the mpm_prefork
module has a higher mpm_worker
stability than the module.
Recommended configuration
<ifmodule mpm_prefork_module>startservers #推荐设置: small = default in =20~50 large =50~100 number of server processes start Minspareservers #推荐设置: Keep the minimum number of server processes consistent with Startservers, save the standby maxspareservers #推荐设置: =30~80 big =80~120 in small =20 Maximum number of server processes, save standby maxclients #推荐设置: Small =500 =500~1500 large =1500~3000 maximum number of server processes allowed to start maxrequestsperchild #推荐设置: A server process service in small =10000 or large =10000~500000 maximum number of connections (in addition, additional settings are required for the Serverlimit parameter, which is best aligned with the value of maxclients. ) </IfModule>
Prefork control process After initially establishing the "startservers" sub-process, in order to meet the needs of minspareservers settings to create a process, wait a second, continue to create two, wait a second, continue to create four ... This increases the number of processes created by the number of digits, up to 32 per second, until the value of the Minspareservers setting is met. This pattern eliminates the need to generate new processes when the request arrives, reducing overhead to increase performance. Maxspareservers sets the maximum number of idle processes, and if the number of idle processes is greater than this value, Apache will automatically kill some of the unwanted processes. This value should not be set too large, but if you set a value smaller than minspareservers, Apache will automatically adjust it to minspareservers+1. If the site load is large, consider increasing minspareservers and maxspareservers at the same time.
Maxrequestsperchild sets the number of requests that can be processed by each child process. Each child process is automatically destroyed after it has processed "maxrequestsperchild" requests. 0 means infinity, that is, the child process never destroys. Although the default setting of 0 enables each subprocess to process more requests, there are two important benefits if set to a value other than 0:
1. To prevent accidental memory leaks. 2. When the server load drops, the number of child processes will be automatically reduced.
Therefore, this value can be adjusted according to the load of the server.
The maxrequestworkers instruction set limits the number of service requests at the same time. Any connection attempts at Maxrequestworkerslimit will normally be queued, up to several based on the Listenbacklog directive.
Prior to apache2.3.13 version maxrequestworkers was called MaxClients.
(MaxClients is the most important of these directives, setting the request that Apache can process at the same time, which is the most influential parameter to Apache performance.) Its default value of 150 is far from enough, if the total number of requests has reached this value (can be confirmed by Ps-ef|grep Http|wc-l), then the subsequent request will be queued until a processed request is complete. This is the main reason why the system resources are still a lot left and HTTP access is slow. Although theoretically the larger the value, the more requests can be processed, but the Apache default limit cannot be greater than 256. )
Worker Module
mpm_worker
The module is also mainly used in the Apache server of the Unix/linux platform, which can be seen as an mpm_prefork
improved version of the module. mpm_worker
modules work in a similar way to mpm_prefork
modules. However, because process-based processes (for example mpm_prefork
) consume more system resources than thread-based processing, because of processing the same request. Therefore, unlike mpm_prefork
modules, the mpm_worker
module causes each child process to create a fixed number of service threads and a listener thread, and let each service thread handle requests from the client, which listens to the access request and passes it to the service thread for processing and answering. Apache always tries to maintain a pool of standby or idle service threads. This way, the client does not have to wait for new threads or new processes to be established to be processed.
mpm_prefork
mpm_worker
modules can further reduce the overhead of system resources compared to modules. Plus it also uses multiple processes, each with multiple threads, so it adds a certain amount of stability to a completely thread-based approach.
<ifmodule mpm_worker_module>startservers #推荐设置: small = default =3~5 large =5~10maxclients #推荐设置: =500 in small =500 ~1500 large =1500~3000minsparethreads #推荐设置: small = default in =50~100 large =100~200maxsparethreads #推荐设置: small = default in =80~160 Big =200~400 threadsperchild #推荐设置: Small =10000 Medium or large =10000~50000(also, if Maxclients/threadsperchild is greater than 16, You also need to set the Serverlimit parameter additional, serverlimit must be greater than or equal to Maxclients/threadsperchild value. ) </IfModule>
The corresponding configuration parameters function as follows:
-
-
Startservers
-
-
number of child processes created when Apache was started.
-
-
Minspareservers
-
-
The number of the least-kid processes in the idle state.
The so-called idle child process refers to a child process that is not processing the request. If the number of currently idle child processes is less than MinSpareServers
, then Apache will produce a new subprocess at a maximum speed of one second. This parameter needs to be adjusted only on very busy machines. This value shouldn't be too large.
-
-
Maxspareservers
-
-
the maximum number of child processes in an idle state.
This parameter needs to be adjusted only on very busy machines. This value shouldn't be too large. If you set the value of the directive to be MinSpareServers
smaller, Apache will automatically change it to MinSpareServers+1
.
-
-
MaxClients
-
-
the maximum number of requests allowed to connect at the same time.
Any MaxClients
request exceeding the limit will enter the waiting queue until ListenBacklog
the maximum of the instruction limit is reached.
For non-threaded MPM (that mpm_prefork
is), the MaxClients
maximum number of child processes that can be used to process client requests, the default value is 256. To increase this value, you must increase it at the same time ServerLimit
.
For a threaded or mixed mpm (that is mpm_beos
, or mpm_worker
), the MaxClients
maximum number of threads that can be used to process client requests. The default value for the thread type mpm_beos
is 50. The default value for the hybrid MPM is the ServerLimit
result of a () multiplied by ( ThreadsPerChild
). So MaxClients
you have to increase the value at the same time to increase to more than 16 processes to provide ServerLimit
.
-
-
Minsparethreads
-
-
the minimum number of threads in an idle state.
Different MPM handles this instruction differently:
mpm_worker
The default value is 75. 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 small, the child process will produce a new idle thread. mpm_netware
The default value is 10. Since this MPM only runs a single sub-process, this MPM will of course monitor the number of idle threads based on the entire server. The default value is mpm_beos
mpmt_os2
1, and the mpm_netware
mpm_beos
mpmt_os2
default value is 5.
-
-
Maxsparethreads
-
-
the maximum number of threads in an idle state.
Different MPM handles this instruction differently:
mpm_worker
The 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 many, the child process kills the extra idle threads. mpm_netware
The default value is 100. Since this MPM only runs a single sub-process, this MPM will of course monitor the number of idle threads based on the entire server. The default value is 10, which is the mpm_beos
mpmt_os2
same as the working mode mpm_netware
mpm_beos
mpmt_os2
.
Note : ServerLimit
represents the maximum number of processes allowed to be created by Apache. It is important to note that Apache has a hard limit (for modules) inside the compile time ServerLimit 20000
mpm_prefork
ServerLimit 200000
. You can't go beyond that limit.
Be especially careful when using this command. If it is ServerLimit
set to a higher value than is actually required by many values, there will be too much shared memory being allocated. If ServerLimit
set to MaxClients
exceed the system's processing power, Apache may not boot or the system will become unstable.
Note : When configuring the relevant parameters, first ensure that the server has sufficient hardware performance (for example: CPU, memory, etc.). If you notice that the server's memory footprint increases as the server's running time increases, and perhaps a memory leak occurs in the program, adjust the value of the parameter downward MaxRequestsPerChild
to reduce the impact of the memory leak, and then find out where the problem is in the program as quickly as possible.
Winnt module
mpm_winnt
Modules are MPM modules designed specifically for the Windows operating system. It creates only a single child process, and in this subprocess takes turns producing multiple threads to process the request.
<ifmodule mpm_winnt_module>threadsperchild #推荐设置: Small website =1000 medium Web site =1000~2000 Large web site =2000~ 3500MaxRequestsPerChild #推荐设置: Small =10000 Medium or large =20000~100000</ifmodule>
The corresponding configuration parameters function as follows:
-
Threadsperchild
-
The maximum number of concurrent threads per child process.
-
Maxrequestsperchild
-
The total number of requests that each child process is allowed to process. If the accumulated number of requests exceeds this value, the child process ends (and then determines whether to create a new child process as needed), which is set to 0 to not limit the total number of requests (the child process never ends).
This parameter is recommended to be set to a value other than zero, which provides the following two benefits:
- You can run out of memory by preventing any memory leaks that may be present in the program from going on indefinitely.
- Gives the process a limited lifespan, which helps reduce the number of active processes when the server load is reduced.
Note: in the above parameters that relate to the number of statistics requests, only the first request is counted for keepalive connections.
Next, let's look at mpm_perfork
mpm_worker
the default configuration under modules and modules:
Reference
72724605 parameter specific calculation
Https://www.cnblogs.com/fazo/p/5588644.html Module Detailed
70182476
http://sanyue.iteye.com/blog/2095222
Apache MPM Mode optimization