MPM -- Apache Working Mode
The performance improvement of Apache 2.0 is the most attractive. In SupportPOSIX threadOfUNIX SystemApache can run different MPM in a multi-process and multi-thread hybrid mode to enhance the scalability of some configurations.
Mpm(Multi-processing modules, multi-channel processing module)Apache2.0 is the core feature that affects performance.
The server can pressSiteCustomized.
At the user level, mpm looks very similar to other Apache modules. The main difference is that only one MPM can be loaded to the server at any time.
The following shows how to specify MPM in Apache 2.0.
# Tar-xvzf httpd-2.0.59.tar.gz
# Cd httpd-2.0.59
#./Configure -- help | grep mpm
-- With-MPM = mpmChoose the process model for Apache to use.
MPM = {BEOs | worker | prefork | mpmt_os2 | perchild | leader | threadpool}
The preceding operation is used to select the MPM module of the process model to be used. BEOs and mpmt_os2 are the default MPM on BEOs and OS/2, respectively,
PerchildIt is designed to run different sub-processes with different identities of users and groups.. This is especially useful when running multiple virtual hosts that require CGI, compared with suexec in version 1.3.
The Mechanism is better. The leader and threadpool are both worker-based variants and are still in the experimental stage. In some cases, they do not work as expected. Therefore, Apache is not officially recommended. Therefore, we mainly discussPrefork and workerThese two types of product-level MPM have the greatest relationship with performance.
Working Principle and configuration of prefork
If you do not need "-- with-MPM" to explicitly specify an MPM,Prefork is the default MPM on UNIX platforms.. It uses the Apache pre-dispatching process.
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-lTo determine the currently used mpm, you will seePrefork. c(If you seeWorker. cWorker is used.
MPM, and so on ). Check the default httpd. conf configuration file, which contains the following configuration sections:
Startservers 5
Minspareservers 5
Maxspareservers 10
Maxclients 150
Maxrequestsperchild 0
PreforkThe working principle is:After the "startservers" sub-process is initially created, the control process creates a process to meet the needs of the minspareservers settings. Wait for one second to create two more processes. Wait for another second to create four more processes ...... In this way, the number of Created processes is increased exponentially, up to 32 processes per second until the value set by minspareservers is met. 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.
Therefore, you can adjust this value based on the server load. I think about 10000 is suitable.
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 150 is far from enough. If the total number of requests has reached this value (
PS-Ef | grep HTTP | WC-L to confirm), then 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 start. In fact, 256 is not enough for sites with a slightly higher load. In Apache
In 1.3, this is a hard limit. To increase this value, you must manually modify it before "Configure ".Source code treeIn src/include/httpd. H, find 256, and you will find "# 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:
Startservers 10
Minspareservers 10
Maxspareservers 15
Serverlimit 2000
Maxclients 1000
Maxrequestsperchild 10000
In the preceding configuration, the maximum serverlimit value is 2000, which is sufficient for most sites. If you must increase the value, modify the following two lines in server/MPM/prefork. C under the source code tree:
# Define default_server_limit 256
# Define max_server_limit 20000
Working Principle and configuration of worker
Compared with prefork, worker is 2.0
The new version of MPM that supports multi-threaded and multi-process hybrid models. 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 MPM will work in Apache
2.0 development trend.
After configure-with-MPM = worker, make and install it. In the default generated
Httpd. conf has the following configuration segments:
Startservers 2
Maxclients 150
Minsparethreads 25
Maxsparethreads 75
Threadsperchild 25
Maxrequestsperchild 0
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:
Startservers 3
Maxclients 2000
Serverlimit 25
Minsparethreads 50
Maxsparethread S 200
Threadlimit 200
Threadsperchild 100
Maxrequestsperchild 0
Through the above description, we can understand the working principles of the two important mpm, prefork and worker in Apache 2.0, and can configure APACHE-related core parameters according to the actual situation, to achieve maximum performance and stability.
Next I will compile and install it in worker Mode
#./Configure -- prefix =/opt/Apache -- With-MPM = worker -- enable-module = so