One, Apache three kinds of MPM introduction
The Apache 2.X supports a plug-in parallel processing module called a multi-Path processing module (MPM). When compiling Apache, you have to choose only one mpm, and there are several different MPM options for Unix-like systems that can affect the speed and scalability of Apache. These three types of MPM are recorded in the conf/extra/httpd-mpm.conf file.
prefork MPM: This multi-path processing module (MPM) implements a non-threaded, pre-derived Web server that works like Apache 1.3. It is suitable for systems that do not have a thread-safe library and need to avoid threading compatibility issues. It is the best mpm to require each request to be independent of each other, so that if a request has a problem it will not affect the other request.
This MPM has a strong self-tuning capability and requires very little adjustment of the configuration instructions. The most important thing is to set the maxclients to a value that is large enough to handle the potential request spikes, and not too large to use more memory than the size of the physical memory.
Worker MPM: This multi-processing module (MPM) enables a network server to support mixed multithreaded multi-process. Because a thread is used to process requests, a large amount of requests can be processed, while the overhead of system resources is less than the process-based MPM. However, it also uses a multi-process, with each process having multiple threads to obtain the stability of the process-based MPM.
The number of threads that each process can have is fixed. The server increases or decreases the number of processes depending on the load. A separate control process (parent process) is responsible for the creation of child processes. Each child process can establish a threadsperchild number of service threads and a listener thread that listens to the access request and passes it to the service thread for processing and answering.
Whether it's worker mode or prefork mode, Apache always tries to keep some spare (spare) or idle child processes (the free service thread pool) to meet the incoming requests. This way, the client does not need to wait for the child process to be generated before the service is received.
Event MPM: Both of these stable MPM methods are somewhat inadequate under very busy server applications. Although the keepalive way of HTTP can reduce the number of TCP connections and network load, keepalive needs to be bound to the service process or thread, which causes a busy server to consume all the threads. The Event MPM is a new model for solving this problem, which separates the service process from the connection. The event MPM approach is most effective when the server is processing fast and has a very high click-through rate, the number of threads available is the critical resource limit. A busy server that works as a worker mpm can withstand a good tens of thousands of visits per second (for example, at the peak of a large news Service site), and event MPM is used to handle higher loads. It is important to note that theEvent MPM does not work under secure HTTP (HTTPS) Access .
Two, configure three kinds of MPM
View Apache-Installed MPM
$ apachectl-l Compiled in modules:core.c prefork.c http_core.c mod_so.c
If you see PERFORK.C, the current perfork MPM mode is indicated. The WORKER.C is represented as the worker MPM mode. The event.c is represented as the event MPM mode.
The prefork is the default MPM on the UNIX platform, and the pre-derived subprocess method used in Apache 1.3 is also used in the pattern. Prefork itself is not using the thread, version 2.0 uses it to maintain compatibility with version 1.3, and on the other hand, the process of handling different instructions with separate sub-processes is independent of each other, which makes it one of the most stable mpm.
Configuring the Apache MPM
You need to specify the mode when the Apache configuration is installed:
$./configure--prefix=/usr/local/apache2worker--enable-so--with-mpm=worker$ make$ make install
Configure the event MPM
As with the method above, just add the following parameters to the installation: –enable-nonportable-atomics=yes
It is important to note that the event MPM may not be supported for older CPUs.
Three, three kinds of MPM parameter analysis
3.1 Perfork MPM Analysis
mpm_prefork_module> startservers 5 #数量的服务器进程开始 MinSpareServers 5 #最小数量的服务器进程, save alternate MaxSpareServers 10 #最大数量的服务器进程, save alternate maxrequestworkers 250 #最大数量的服务器进程允许开始 maxconnectionsperchild 0 #每个服务器进程的最大连接数
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 allows each child process to process more requests, there are two important benefits if set to a non-0 value: 1, which prevents 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.
Maxrequestworkers is one of 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.
3.2 Worker MPM Analysis
Mpm_worker_module> startservers 3 #初始数量的服务器进程开始 minsparethreads #最小数量的工作线程, save alternate Maxspare Threads #最大数量的工作线程, save Threadsperchild #固定数量的工作线程在每个服务器进程 maxrequestworkers #最大数量 The worker thread Maxconnectionsperchild 0 #每个服务器进程的最大连接数
The worker generates a "startservers" sub-process by the master control process, each of which contains a fixed number of threadsperchild threads, and each thread processes the request independently. Similarly, minsparethreads and maxsparethreads set the minimum and maximum number of idle threads in order not to generate threads when the request arrives;
The maxrequestworkers sets the maximum number of clients that are simultaneously connected. If the total number of threads in an existing child process does not meet the load, the control process will derive the new child process.
Example: 5 startservers x threadsperchild = maxrequestworkers.
The maximum default values for Minsparethreads and Maxsparethreads are 75 and 250, respectively. These two parameters have little effect on Apache performance, and can be adjusted according to the actual situation.
Threadsperchild is the most performance-related instruction in the worker mpm. The maximum default value for Threadsperchild is 64, and 64 is not enough if the load is large. At this point, to explicitly use the THREADLIMIT directive, its maximum default value is 20000.
The total number of requests that can be processed concurrently in worker mode is determined by the child process (startservers) multiplied by the Threadsperchild value and should be greater than or equal to maxrequestworkers. If the load is large and the number of existing child processes is not met, the control process derives the new child process. The default maximum number of child processes is 16, and you need to explicitly declare serverlimit (the maximum value is 20000) when you increase it. It is important to note that if Serverlimit is explicitly declared, then it must be multiplied by the value of threadsperchild to be greater than or equal to maxrequestworkers. And maxrequestworkers must be an integer multiple of threadsperchild, otherwise Apache will automatically adjust to a corresponding value.
3.3 Event MPM Analysis
Mpm_event_module> startservers 3 #初始数量的服务器进程开始 minsparethreads #最小数量的工作线程, save alternate Maxsparet Hreads #最大数量的工作线程, save Threadsperchild #固定数量的工作线程在每个服务器进程 maxrequestworkers #最大数量的 Worker thread Maxconnectionsperchild 0 #每个服务器进程的最大连接数
Apache prefork, worker and event three modes of operation