Apache 2.x has several concurrent processing modules, which must be specified through-with-MPM = xxx during compilation. The common concurrent processing modes are prefork and worker. The prefork mode is old, with pure process concurrency and no threads. The processing method is the same as apache1.x. It is applicable to servers without a thread library or do not want to use threads; worker is a multi-process and multi-thread mode. In worker mode, there is a unique parent process (Control Process). This parent process will generate several child processes as needed, each sub-process has one listening thread and several service threads (the number of service threads of each sub-process is fixed and specified by threadsperchild ), the listening thread is responsible for listening to client requests and handing them over to the service thread for processing. Theoretically, the worker mode has better performance than the prefork mode. when processing the same number of requests, it consumes less resources than the prefork mode.
According to the official document:
The server can be better customized for the needs of the participating site. For example, sites that need a great deal of scalability can choose to use a threaded MPM likeworker
Orevent
, While sites requiring stability or compatibility with older software can useprefork
.
Worker
This multi-processing module (MPM) implements a hybrid multi-process multi-threaded server. by using threads to serve requests, it is able to serve a large number of requests with fewer system resources than a process-based server. however, it retains much of the stability of a process-based server by keeping multiple processes available, each with threads.
Prefork
This multi-processing module (MPM) implements a non-threaded, pre-forking web server that handles requests in a manner similar to Apache 1.3. it is appropriate for sites that need to avoid threading for compatibility with non-thread-safe libraries. it is also the best MPM for isolating each request, so that a problem with a single request will not affect any other.
Configuration parameters of the worker module:
Threadsperchild |
Number of service threads for each sub-process |
Default Value: 25 |
Startservers |
Number of sub-processes enabled when Apache is started |
Default Value: 3. |
Minsparethreads |
Minimum number of Idle threads |
Default Value: 75 |
Maxsparethreads |
The maximum number of Idle threads. Apache will keep the total number of Idle threads between minsparethreads and maxsparethreads when running. You can use the default values for these two parameters. Generally, you do not need to adjust them. |
Default Value: 250 |
Maxclients |
The maximum number of concurrent requests on the client (the maximum number of threads). The value obtained by dividing maxclients by threadsperchild is the maximum number of sub-processes that may be reached. If the number of concurrent requests on the server exceeds maxclients, apache will report the following error: Server reached maxclients setting, consider raising the maxclients setting |
The default value is 16*25 = 400. |
Serverlimit |
Maximum number of processes. The value must be greater than or equal to the value obtained by dividing maxclients by threadsperchild. This parameter is a hard limit. Directly restarting Apache (APACHE restart) does not take effect. You must stop Apache (APACHE stop) and then start Apache (APACHE start) to take effect. |
The default value is 16 in worker mode and 256 in prefork mode. |
Threadlimit |
The maximum number of threads per process. That is to say, this parameter determines the maximum value of threadsperchild. If this parameter is set to be much larger than threadsperchild, a lot of shared memory will be wasted. If the configuration is too large, Apache cannot be started or the system is unstable. This parameter is also a hard limitation. Serverlimit and threadlimit must be placed before other settings. |
Default Value 64 |
Maxrequestsperchild |
The total number of requests processed by a sub-process in its lifecycle is limited. When the total number of requests processed by a sub-process reaches this limit, the process will be recycled, if it is set to 0, the process will never expire (in this way, if there is a memory leak, it will continue to leak ......) |
--------------------------------------------------------
Apache has three running modes:
1, prefork MPM;
2. Worker MPM;
3, perchild MPM.
Prefork mpm
The Administrator should select prefork MPM to implement a process-based Web server. Although process-based servers have slow performance, they provide stability and compatibility through modules, which do not support threads. To improve performance, the server's parent process fork multiple child processes and enables them to respond to requests. When a request is received, the server assigns it to the sub-process. If no sub-process is available, the server creates a new sub-process and adds it to the database. However, the creation of sub-processes may delay. When this MPM is configured, the administrator can limit the number of sub-processes that can be created at startup and the maximum number of sub-processes. This MPM facilitates the formation of a stable Apache server, but it also affects performance and memory usage, because the size of a single process accounts for a large part.
The worker mpm
To implement hybrid servers, administrators can choose worker mpm, which provides higher reliability, fault recovery capabilities, and scalability than prefork MPM. At startup, the parent process creates a specified number of sub-processes. In turn, each sub-process contains multiple threads. Each sub-process has only one thread to listen to the network. It simplifies program code, reduces collaboration between processes, and improves performance. Although this MPM is stable and has better performance than prefork, because it is thread-based, all modules used with it must be completely thread-safe. Most Apache 1.3 modules are NOT thread-safe and are not backward compatible with worker MPM. Therefore, administrators using this MPM cannot use the Apache 1.3 Module and Apache 2.0 together. However, the prefork MPM is used to compile the server to allow administrators to use the Apache 1.3 Module.
The perchild mpm
If the Apache process runs based on different user IDs in consideration of security and performance, the administrator can use perchild MPM to compile the Hybrid server. Internet service providers (ISPs) usually use this MPM for virtual hosting. When perchild starts, it creates a specified number of processes, each of which has a specified number of threads and a dedicated user ID. If the load on the server increases, it uses one of the existing processes to create a new thread, rather than creating a new process. This MPM is the most extensible but unreliable.