Apache is a cross-platform Web server, because of its simple and efficient, stable security features, is widely used in computer technology in all areas. Now, with its huge number of users, Apache has become the number one Web server for users.
However, in a real production environment, it is still not possible to use the default configuration of Apache directly to act as a server. After all, in order to use Apache server more fully and rationally, we should make some necessary adjustments to Apache's default configuration according to our actual needs. While optimizing the configuration for Apache, it is particularly important to modify the maximum number of concurrent connections for Apache.
Before we modify the maximum number of concurrent connections to Apache, we need to know some of the knowledge of Apache in advance.
As we all know, Apache is a cross-platform, modular design of the server. In order to meet the different needs of various platforms and different environments, and to achieve the best results in a specific platform or environment, Apache has also adopted a modular design for the basic functions of the Web server (port bindings, receiving requests, etc.). This Apache core module is called the Multi-Path processing module (multi-processing module, or 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 the condition permits, we can compile the specified MPM module into our own Apache according to the actual requirement (Apache source code is open, allowing the user to compile by themselves). However, if we do not have a choice at compile time, Apache will choose the corresponding MPM module according to the different operating system, which is the MPM module recommended by Apache for different platforms.
The default MPM module on different operating systems |
Operating System |
MPM Module |
Description |
Windows |
Mpm_winnt |
Don't introduce it:) |
Unix/linux |
Mpm_prefork |
Don't introduce it:) |
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 that was originally developed by Microsoft and IBM and is now developed separately by IBM (Microsoft abandons OS/2, and instead develops windows) |
The Mpm_event module can be regarded as a variant of the Mpm_worker module, but it has experimental properties and is generally not recommended for use.
Of course, Apache also provides a finished Apache on its official website that corresponds to the MPM modules that have been compiled for different operating systems. You can click here to go to the official Apache website to download.
In addition, if we want to know what kind of MPM module is used inside an Apache, we can enter it as a command line and Apache安装目录\bin
then type the command to httpd -l
see what MPM module is currently in use within Apache.
To view the compilation module using the Httpd-l command
Because BeOS, NetWare, OS/2 and other operating systems are not common in the usual development work, here we mainly explain the MPM modules on the windows and Unix/linux operating systems. In the Windows and Unix/linux operating systems, the MPM module mainly has mpm_winnt
, mpm_prefork
, mpm_worker
three kinds.
Mpm_prefork Module
mpm_prefork
The module mainly applies to the Apache server of the Unix/linux platform, which mainly works as follows: When the Apache server is started, the mpm_prefork
module will pre-create several sub-processes (the default is 5), when the client's request is received, the mpm_prefork
module then forwards the request to the child process processing, And each child process can only be used to process a single request at the same time. 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.
Mpm_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.
MPM_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.
Modifying the MPM module configuration
With a certain understanding of the Apache MPM module, we can modify the maximum number of concurrent connections in Apache for different MPM modules.
1. Enable MPM Module configuration file
Apace安装目录/conf/extra
There is a configuration file named in the directory httpd-mpm.conf
. This file is mainly used for the configuration of the MPM module. However, by default, the MPM module configuration file for Apache is not enabled. Therefore, we need to httpd.conf
enable the configuration file in the file as follows:
(Remove the comment symbol "#" in front of the line)
2. Modify the relevant configuration in the MPM module configuration file
After starting the MPM module configuration file, we can open the configuration file with a text editor, and we can see that there are many configuration nodes in the configuration file, <IfModule>
as shown in:
The corresponding configuration will only take effect if Apache uses the corresponding MPM module.
At this point, we need to modify the parameter configuration under the corresponding node according to the MPM module used by the current Apache server <IfModule>
. First, let's look at mpm_winnt
the default configuration under the module:
#由于mpm_winnt模块只会创建1个子进程, the parameter setting for a single subprocess here is equivalent to the entire Apache parameter setting. <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:
#mpm_perfork模块 <ifmodule mpm_prefork_module>startservers #推荐设置: small = default =20~50 large =50~100minspareservers #推荐设置: Consistent with startservers maxspareservers #推荐设置: Small =20 =30~80 large =80~120 maxclients #推荐设置: small = 500 medium =500~1500 Large =1500~3000maxrequestsperchild #推荐设置: Small =10000 Medium or large =10000~500000(in addition, An additional set of serverlimit parameters is required, which is best consistent with the value of maxclients. ) </IfModule>
# startservers: Number of server processes started
# Minspareservers: Minimum number of server processes, saving alternate
# Maxspareservers: Maximum number of server processes, saving alternate
# Maxrequestworkers: Maximum number of server processes allowed to start
# Maxconnectionsperchild: One server process service with maximum number of connections
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. )
#mpm_worker模块 <ifmodule mpm_worker_module>startservers #推荐设置: small = =3~5 big =5~10maxclients in default # Recommended settings: Small =500 =500~1500 large =1500~3000minsparethreads #推荐设置: small = default in =50~100 big =100~200maxsparethreads # Recommended settings: small = default in =80~160 large =200~400 threadsperchild #推荐设置: Small =10000 in or large =10000~50000(also, if maxclients/ Threadsperchild is greater than 16 and additional serverlimit parameters are required, 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.
This article transferred from: http://www.365mini.com/page/apache-concurrency-configuration.htm
Another article:
How to configure the maximum number of concurrency for Apache
MPM (Multi-path processing module)
Common:
1.perfork preprocessing process mode
2.worker worker Mode
3.winnt used in Windows
Case: Configure the maximum concurrency of Apache to 1000
1. First confirm Apache's MPM mode
Cmd>httpd.exe-l can see what the pattern is.
Here's the mpm_xxx.c, this xxx is that.
2. Modify the httpd.conf file
Search MPM, find Server-pool Management (MPM specific)
Remove # Include conf/extra/httpd-mpm.conf
3. Modify the Conf/extra/httpd-mpm.conf file
The Prefork mode is modified here.
<ifmodule mpm_prefork_module>
Startservers 5 # Pre-opened process
Minspareservers 5 # Minimum of 5 reserved
Maxspareservers 10 # Max Stay 10
MaxClients 150 # Maximum number of concurrent *
Maxrequestsperchild 0 # Maximum number of requests 0 No limit
</IfModule>
Winnt mode
<ifmodule mpm_winnt_module>
Threadsperchild 150 # Max Concurrent number *
Maxrequestsperchild 0 # Maximum number of processing requests 0 No Limit
</IfModule>
Modify the value of the field followed by * and restart Apache
Description: The configuration to how large, may not necessarily support such a large concurrency, taking into account its own Apache machine hardware performance (such as: Memory, CPU, hard disk IO)
System is Linux/unix, configuration perfork
<ifmodule mpm_prefork_module>
Startservers 5
Minspareservers 5
Maxspareservers 10
MaxClients * #并发量
Maxrequestsperchild 0
</IfModule>
Give everyone a reasonable proposal configuration, for most sites, medium-sized website configuration
<ifmodule mpm_prefork_module>
Startservers 5 # Pre-boot
Minspareservers 5
Maxspareservers 10 # Max Idle Process
Serverlimit 1500 *# for modifying Apache programming parameters
MaxClients 1000 *# Max concurrent number
Maxrequestsperchild 0
</IfModule>
Note: apache2.2 after the serverlimit this parameter, where the Serverlimit value is greater than the MaxClients value
If the PV value of the site is millions
Serverlimit 2500 *# for modifying Apache programming parameters
MaxClients 2000 *# Max concurrent number
Note: Adjust to this is the limit, if the site access or large, which will increase the Apache server
Original: Http://www.tuicool.com/articles/RJBzmai
For window use commands
LS can be listed in the folder below the current directory
Httpd-t-D dump_modules case
Httpd-t-D Dump_modules
Httpd-help
D:\wamp\bin\apache\apache2.2.22\bin>httpd-l
Compiled in Modules:
Core.c
Mod_win32.c
Mpm_winnt.c
Http_core.c
Mod_so.c
Apache Platform Process Threading Module parsing