To improve HTTP's "'keep alive" problem, Apache introduced the MPM (Multi-processing modules, multi-channel processing module) http://httpd.apache.org/docs/current/mpm.html in version 2.0
This article describes the basic working principles and application limitations of MPM (Multi-processing modules.
Run the following code:
$./Configure -- help | grep mpm
Shown as follows:
-- With-MPM = mpm
Choose the process model for Apache to use.
MPM = {BEOs | worker | prefork | mpmt_os2 | perchild | leader | threadpool}
Beosbeosnetwarempm_netwareos/2mpmt_os2unixpreforkwindowsmpm_winn
The leader and threadpool are both worker-based variants and are still in the experimental stage. This article mainly describes the working principles of the prefork and worker modes. (For more information, see the official Apache documentation [url] routing.
)
Working Principle and configuration of prefork
If "-- with-MPM" is not used to explicitly specify an MPM, prefork is the default MPM on UNIX platforms. It adopts the pre-school child process mode, which is also used in Apache 1.3. Prefork itself does not use threads. Version 2.0 uses it to maintain compatibility with version 1.3. On the other hand, prefork uses a separate sub-process to process different requests, processes are independent from each other, which makes them one of the most stable MPM.
If prefork is used, after make compilation and make install, "httpd-L" is used to determine the currently used MPM. C (if you see worker. C indicates that worker MPM is used, and so on ). The httpd. conf configuration file generated by default contains the following configuration segments:
<Ifmodule prefork. c>;
Startservers 5
Minspareservers 5
Maxspareservers 10
Maxclients 150
Maxrequestsperchild 0
</Ifmodule>;
The working principle of prefork is that after the control process initially creates a "startservers" sub-process, it creates a process to meet the needs of the minspareservers settings, waits for one second, creates two more, and then waits for one second, create four more ...... In this way, the number of Created processes is increased exponentially, up to 32 processes per second until the value of minspareservers is full. This is the origin of prefork. This mode eliminates the need to generate new processes when requests arrive, thus 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 automatically kill unnecessary processes. This value should not be too large, but if it is 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 each sub-process can process. Each sub-process is automatically destroyed after processing "maxrequestsperchild" requests. 0 means that the sub-process will never be destroyed. Although the default value is 0, each sub-process can process more requests, setting it to a non-zero value also has two important advantages:
◆ Prevents accidental Memory leakage;
◆ When the server load drops, the number of sub-processes is automatically reduced.
Maxclients is the most important of these commands. It sets the request that Apache can process at the same time and is the parameter that has the greatest impact on Apache performance. The default value of 150 is far from enough. If the total number of requests has reached this value (you can confirm it through PS-Ef | grep HTTP | WC-l), the subsequent requests will be queued, until a processed request is completed. This is the main reason why there are still a lot of system resources and HTTP access is slow. The system administrator can dynamically adjust the value based on hardware configuration and load conditions. In theory, the larger the value, the more requests can be processed, but the default limit of Apache cannot be greater than 256. If you set this value to greater than 256
Apache cannot be started. In fact, 256 is not enough for sites with a slightly higher load. In Apache 1.3, this is a hard limitation. To increase this value, you must search for 256 in src/include/httpd. h under the source code tree manually modified before "Configure", and you will find the line "# define hard_server_limit 256. Change 256 to the value to be increased (such as 4000), and then re-compile Apache. Added the serverlimit command in Apache 2.0 to increase maxclients without re-compiling Apache. The following is the prefork configuration section of the author:
<Ifmodule prefork. c>;
Startservers 10
Minspareservers 10
Maxspareservers 15
Serverlimit 2000
Maxclients 1000
Maxrequestsperchild 10000
</Ifmodule>;
For more information about server limits, see [url] http://httpd.apache.org/docs/2.2/mod/mpm_common.html?serverlimit#/url] maxrequestsperchild: sets the total number of requests that an independent sub-process can process during its.
Open apache2 \ conf \ httpd. conf in notepad, search for maxrequestsperchild, and change maxrequestsperchild 0 to maxrequestsperchild 50. Generally, two apache.exe processes can be viewed in the "Windows Task Manager" process. One is the parent process and the other is the child process. After receiving the access request, the parent process submits the request to the child process for processing. The maxrequestsperchild command sets the number of requests that an independent sub-process can process. After the "maxrequestsperchild number" request is processed, the child process will be terminated by the parent process. At this time, the memory occupied by the child process will be released. If there are other access requests, the parent process re-generates the child process for processing. If
By default, maxrequestsperchild is set to 0 (unlimited) or a large number (for example, more than 10000), so that each sub-process can process more requests, it will not reduce the access efficiency because the sub-process is terminated and started continuously, but if maxrequestsperchild is set to 0, if it occupies 200 ~ MB of memory, even if the load is down, the occupied memory will not be reduced. A server with a large memory can be set to 0 or a large number. A server with a small memory can be set to 30, 50, or 100 to prevent memory overflow.
The definition is different in worker K and worker modes.
Working Principle and configuration of worker
Compared with prefork, worker is a brand new MPM that supports multi-thread and multi-process hybrid models in version 2.0. Because threads are used for processing, a relatively large number of requests can be processed, and the overhead of system resources is smaller than that of process-based servers. However, worker also uses multiple processes, and each process generates multiple threads to achieve stability based on the process server. This kind of MPM approach will be the development trend of Apache 2.0.
After configure-with-MPM = worker, make and install it. The following configuration segments are available in the httpd. conf generated by default:
<Ifmodule worker. c>;
Startservers 2
Maxclients 150
Minsparethreads 25
Maxsparethreads 75
Threadsperchild 25
Maxrequestsperchild 0
</Ifmodule>;
The working principle of worker is that the main control process generates a "startservers" subprocess. Each subprocess contains a fixed number of threadsperchild threads, and each thread processes Requests independently. Similarly, in order not to generate a thread when the request arrives, minsparethreads and maxsparethreads set the minimum and maximum number of Idle threads, while maxclients sets the total number of threads in all sub-processes. If the total number of threads in the existing sub-process cannot meet the load, the control process will derive a new sub-process.
The maximum default values of minsparethreads and maxsparethreads are 75 and 250, respectively. These two parameters have little impact on Apache performance and can be adjusted as needed.
Threadsperchild is the most performance-related command in worker MPM. The maximum default value of threadsperchild is 64. If the load is large, 64 is not enough. The threadlimit command must be explicitly used. The maximum default value is 20000. The preceding two values are located in the following two lines in the source code tree server/MPM/worker. C:
# Define default_thread_limit 64
# Define max_thread_limit 20000
These two rows correspond to the number of restrictions of threadsperchild and threadlimit. It is best to change 64 to the desired value before configure. Note: Do not set these two values too high to exceed the processing capability of the system, so that the system is unstable because Apache does not start.
In worker mode, the total number of requests that can be processed simultaneously is determined by the total number of sub-processes multiplied by the value of threadsperchild. The value must be greater than or equal to maxclients. If the load is large and the number of existing sub-processes cannot meet the requirements, the control process will derive a new sub-process. By default, the maximum number of sub-processes is 16. When you increase the number, you must explicitly declare serverlimit (the maximum value is 20000 ). These two values are located in the following two lines in the source code tree server/MPM/worker. C:
# Define default_server_limit 16
# Define max_server_limit 20000
Note that if serverlimit is explicitly declared, the value multiplied by threadsperchild must be greater than or equal to maxclients, and maxclients must be an integer multiple of threadsperchild, otherwise, Apache automatically adjusts to a value (which may be an unexpected value ). The following is the author's worker configuration section:
<Ifmodule worker. c>;
Startservers 3
Maxclients 2000
Serverlimit 25
Minsparethreads 50
Maxsparethread S 200
Threadlimit 200
Threadsperchild 100
Maxrequestsperchild 0
</Ifmodule>;
Restrictions on applications: the MPM and mod_ssl modules are incompatible so far. The two cannot run simultaneously.
In prefork mode, multiple sub-processes are used. Each sub-process has only one thread. Each process can maintain only one connection at a specified time. On most platforms, the prefork MPM is more efficient than the worker mpm, but the memory usage is much larger. Prefork's wireless program design will be more advantageous in some cases than worker: it can use third-party modules that do not handle thread security well, and for platforms that have difficulty in thread debugging, it is easier to debug.
In worker mode, multiple sub-processes are used. Each sub-process has multiple threads. Each thread can maintain only one connection at a specified time. Generally, on a high-traffic HTTP server, worker MPM is a good choice, because the memory usage of worker MPM is much lower than that of prefork MPM. However, the worker MPM is also imperfect. If a thread crashes, the whole process will "die" together with all its threads ". because threads share memory space, a program must be recognized by the system as "Every thread is safe" during runtime ".
The prefork method is faster than worker. However, it requires more CPU and memory resources than woker.
Note:
After you change the parameters related to the working mode, you must disable the apache service and restart the service.
Directly starting with restart will be invalid.