Apache provides multiple 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 it themselves). However, if we do not choose during compilation, Apache will select the corresponding MPM module according to different operating systems according to the following table. This is also the MPM module recommended by Apache for different platforms.
(Recommended tutorial:apache)
Default MPM module on different operating systems
Operating system MPM module description
Windowsmpm_winnt No need to introduce it:)
Unix/Linuxmpm_prefork No need to introduce it:)
BeOSmpm_beos is a multimedia operating system developed by Be Company. The official version has stopped updating.
Netwarempm_netware A network operating system launched by NOVELL
OS/2mpmt_os2 An operating system originally jointly developed by Microsoft and IBM, now developed solely by IBM (Microsoft gave up OS/2 , switch to developing Windows)
mpm_event module can be regarded as a variant of mpm_worker module, but it is experimental and is generally not recommended for use.
Of course, Apache also provides finished Apache on its official website that has compiled corresponding MPM modules according to different operating systems. 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 internally in an Apache, we can enter the Apache installation directory\bin using the command line, and then type the command httpd -l to view Which MPM module is currently used internally by Apache.
Use the httpd -l command to view the compiled module
Since in normal development work, BeOS, NetWare, OS/2 and other operating systems are not common, here we mainly focus on Windows and Unix/ The MPM module on the Linux operating system is explained. On Windows and Unix/Linux operating systems, there are three main MPM modules: mpm_winnt, mpm_prefork, and mpm_worker.
mpm_prefork module
mpm_prefork module is mainly used in the Apache server on Unix/Linux platform. Its main working method is: when the Apache server is started, the mpm_prefork module will pre-create multiple child processes (the default is 5), after receiving the client's request, the mpm_prefork module then transfers the request to the sub-process for processing, and each sub-process can only be used to process a single request at the same time. If the current number of requests will exceed the number of pre-created sub-processes, the mpm_prefork module will create new sub-processes to handle the additional requests. Apache always tries to keep some spare or idle child processes available for upcoming requests. In this way, the client's request does not need to wait for the child process to be generated after receiving it.
Since in the mpm_prefork module, each request corresponds to a child process, it occupies more system resources than the other two modules. However, the advantage of the mpm_prefork module is that each of its sub-processes will handle the corresponding single request independently, so that if a problem occurs with one of the requests, it will not affect other requests. At the same time, the mpm_prefork module can be applied to third-party modules that are not thread-safe (such as non-thread-safe versions of PHP), and is easy to debug on platforms that do not support thread debugging. In addition, the mpm_prefork module also has higher stability than the mpm_worker module.
mpm_worker module
The mpm_worker module is also mainly used in the Apache server on the Unix/Linux platform. It can be regarded as an improved version of the mpm_prefork module. The mpm_worker module works similarly to the mpm_prefork module. However, when processing the same request, process-based (such as mpm_prefork) takes up more system resources than thread-based processing. Therefore, unlike the mpm_prefork module, the mpm_worker module will allow each child process to create a fixed number of service threads and a listening thread, and let each service thread handle the client's request. The listening thread is used to monitor access requests and send them Passed to the service thread for processing and response. Apache always tries to maintain a pool of spare or idle service threads. In this way, the client does not need to wait for a new thread or process to be established before it can be processed.
Compared with the mpm_prefork module, the mpm_worker module can further reduce system resource overhead. In addition, it also uses multiple processes, and each process has multiple threads, so it adds a certain degree of stability compared to a completely thread-based processing method.
mpm_winnt module
mpm_winnt module is an MPM module optimized and designed specifically for Windows operating systems. It only creates a separate child process and spawns multiple threads in this child process in turn to handle the request.
Modify MPM module configuration
After having a certain understanding of Apache's MPM module, we can modify Apache's maximum concurrent connection configuration for different MPM modules.
1. Enable MPM module configuration file
There is a configuration file named httpd-mpm.conf in the Apace installation directory/conf/extra directory. This file is mainly used to configure the MPM module. However, by default, Apache's MPM module configuration file is not enabled. Therefore, we need to enable this configuration file in the httpd.conf file as follows:
# Server-pool management (MPM specific)Include conf/extra/httpd-mpm.conf (remove the Comment symbol "#")
2. Modify the relevant configuration in the MPM module configuration file
After starting the MPM module configuration file, we can use a text editor to open the configuration file, we You can see that there are many configuration nodes in this configuration file, as shown in the figure below:
The corresponding configuration will only take effect when Apache uses the corresponding MPM module
At this time, we need Modify the parameter configuration under the corresponding node according to the MPM module currently used by the Apache server. First, let's take a look at the default configuration under the mpm_winnt module:
#Since the mpm_winnt module will only create one sub-process, the parameter settings for a single sub-process here are equivalent to the parameter settings for the entire Apache. ThreadsPerChild 150# Recommended settings: Small website=1000 Medium website=1000~2000 Large website=2000~3500MaxRequestsPerChild 0# Recommended settings: Small=10000 Medium or large=20000~100000
The corresponding configuration parameters are 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 handle. If the cumulative number of requests processed exceeds this value, the subprocess will end (and then determine whether to create a new subprocess as needed). Setting this value to 0 means that the total number of requests is not limited (the subprocess will never end).
It is recommended to set this parameter to a non-zero value, which can bring the following two benefits:
It can prevent possible memory leaks in the program from continuing indefinitely, thereby exhausting memory.
Give processes a limited lifetime to help reduce the number of active processes when server load is reduced.
Note: Among the above parameters related to counting the number of requests, for KeepAlive connections, only the first request will be counted.
Next, let’s take a look at the default configuration under the mpm_perfork module and mpm_worker module:
#mpm_perfork module StartServers 5# Recommended settings: Small = Default Medium = 20~50 Large = 50~100MinSpareServers 5# Recommended settings: consistent with StartServers MaxSpareServers 10# Recommended settings: Small=20 Medium=30~80 Large=80~120 MaxClients 150# Recommended settings: Small=500 Medium=500~1500 Large=1500~3000MaxRequestsPerChild 0# Recommended Settings: Small = 10000 Medium or Large = 10000~500000 (In addition, you need to set the ServerLimit parameter additionally, which is best consistent with the value of MaxClients.)
# StartServers: 数量的服务器进程开始 # MinSpareServers: 最小数量的服务器进程,保存备用 # MaxSpareServers: 最大数量的服务器进程,保存备用 # MaxRequestWorkers: 最大数量的服务器进程允许开始 # MaxConnectionsPerChild: 最大连接数的一个服务器进程服务
prefork The control process initially establishes "StartServers" After the child process is created, create a process to meet the needs set by MinSpareServers, wait for one second, continue to create two, wait for another second, continue to create four... In this way, the number of created processes will be increased exponentially, up to a maximum of one second. 32, until the value set by MinSpareServers is met. This mode eliminates the need to create a new process when a request comes, thereby reducing system overhead and increasing performance. MaxSpareServers sets the maximum number of idle processes. If the number of idle processes is greater than this value, Apache will automatically kill some redundant processes. Do not set this value too large, but if the value is smaller than MinSpareServers, Apache will automatically adjust it to MinSpareServers 1. If the site load is heavy, consider increasing both MinSpareServers and MaxSpareServers.
MaxRequestsPerChild sets the number of requests that each child process can handle. Each child process will be automatically destroyed after processing "MaxRequestsPerChild" requests. 0 means infinite, that is, the child process is never destroyed. Although the default setting to 0 allows each child process to handle more requests, setting it to a non-zero value also has two important benefits:
1. It can prevent accidental memory leaks. 2. When the server load decreases, the number of child processes will be automatically reduced.
Therefore, this value can be adjusted according to the load of the server.
The MaxRequestWorkers directive set will limit the number of requests that can be serviced simultaneously. Any connection attempts within the MaxRequestWorkerslimit will usually be queued, up to a number of directives based on the ListenBacklog.
In versions prior to apache2.3.13, MaxRequestWorkers was called MaxClients.
(MaxClients is the most important of these instructions. It sets the requests that Apache can handle at the same time. It is the parameter that has the greatest impact on Apache performance. Its default value of 150 is far from enough. If the request The total number has reached this value (can be confirmed by ps -ef|grep http|wc -l), then subsequent requests will be queued until a processed request is completed. This means that there are still a lot of system resources left but HTTP access is not The main reason for being very slow. Although in theory, the larger the value, the more requests that can be processed, but Apache's default limit cannot be greater than 256.)
#mpm_worker module StartServers 2#Recommended settings: Small=Default Medium=3~5 Large=5~10MaxClients 150#Recommended settings: Small=500 Medium=500~1500 Large=1500~3000MinSpareThreads 25#Recommended settings: Small= Default medium=50~100 Large=100~200MaxSpareThreads 75#Recommended settings: Small=Default medium=80~160 Large=200~400 ThreadsPerChild 25#Recommended settings: Small=Default medium=50~100 Large=100~200MaxRequestsPerChild 0# Recommended settings: Small = 10000 Medium or Large = 10000~50000 (In addition, if MaxClients/ThreadsPerChild is greater than 16, you need to set the ServerLimit parameter additionally. ServerLimit must be greater than or equal to the value of MaxClients/ThreadsPerChild.)
Corresponding configuration The parameters are as follows:
StartServers
The number of child processes created when starting Apache.
MinSpareServers
The minimum number of child processes that are idle.
The so-called idle child process refers to a child process that is not processing requests. If the current number of idle child processes is less than MinSpareServers, Apache will spawn new child processes at a maximum rate of one per second. Adjusting this parameter is only necessary on very busy machines. This value should not be too large.
MaxSpareServers
The maximum number of child processes that are idle.
This parameter needs to be adjusted only on very busy machines. This value should not be too large. If you set the value of this directive to be smaller than MinSpareServers, Apache will automatically modify it to MinSpareServers 1.
MaxClients
The maximum number of requests allowed for simultaneous connections.
Any request that exceeds the MaxClients limit will enter the waiting queue until the maximum value of the ListenBacklog directive limit is reached.
For non-threaded MPM (that is, mpm_prefork), MaxClients indicates the maximum number of child processes that can be used to handle client requests. The default value is 256. To increase this value, you must also increase ServerLimit.
For threaded or mixed MPM (that is, mpm_beos or mpm_worker), MaxClients represents the maximum number of threads that can be used to process client requests. The default value of threaded mpm_beos is 50. The default value for mixed MPM is 16 (ServerLimit) times 25 (ThreadsPerChild). Therefore, when increasing MaxClients to more than 16 processes to provide, you must also increase the value of ServerLimit.
MinSpareThreads
Minimum number of threads in idle state.
Different MPMs handle this command differently:
The default value of mpm_worker is 75. This MPM will monitor the number of idle threads on an entire server basis. If the total number of idle threads in the server is too few, the child process will generate new idle threads. The default value of mpm_netware is 10. Since this MPM only runs a single child process, of course this MPM also monitors the number of idle threads based on the entire server. mpm_beos and mpmt_os2 work similar to mpm_netware. The default value of mpm_beos is 1; the default value of mpmt_os2 is 5.
MaxSpareThreads
The maximum number of threads in idle state.
Different MPMs handle this command differently:
The default value of mpm_worker is 250. This MPM will monitor the number of idle threads on an entire server basis. If the total number of idle threads in the server is too large, the child process will kill the excess idle threads. The default value of mpm_netware is 100. Since this MPM only runs a single child process, of course this MPM also monitors the number of idle threads based on the entire server. mpm_beos and mpmt_os2 work similar to mpm_netware. The default value of mpm_beos is 50; the default value of mpmt_os2 is 10.
Note: ServerLimit represents the maximum number of processes that Apache allows to create. It is worth noting that Apache has an internal hard limit of ServerLimit 20000 at compile time (ServerLimit 200000 for the mpm_prefork module). You cannot exceed this limit.
Be especially careful when using this command. If ServerLimit is set to a value much higher than actually needed, too much shared memory will be allocated. If you set ServerLimit and MaxClients to exceed the system's processing capabilities, Apache may not start, or the system may become unstable.
Note: When configuring relevant parameters, please first ensure that the server has sufficient hardware performance (for example: CPU, memory, etc.). If you find that the server's memory usage has increased as the server's running time has increased since startup, it may be a memory leak in the program. Please adjust the value of the parameter MaxRequestsPerChild downward to reduce the impact of the memory leak, and then proceed as soon as possible. Find out where the problem is in the program.
The above is the detailed content of Apache changes the maximum number of concurrent connections. For more information, please follow other related articles on the PHP Chinese website!