Apache2.0 performance optimization-MPM selection and configuration

Source: Internet
Author: User
Apache & nbsp; 2.0 is the most attractive in terms of performance improvement. On Unix systems that support POSIX threads, Apache can use different MPM to run in a mix of multi-process and multi-thread modes to enhance the scalability of some configurations. Compared with Apache & nbsp; 1.3, version 2.0 has made a lot of optimizations to improve processing capability and scalability, and most of the improvements will take effect by default. However

Apache? 2.0 performance improvement is the most attractive. On Unix systems that support POSIX threads, Apache can use different MPM to run in a mix of multi-process and multi-thread modes to enhance the scalability of some configurations. Compared with Apache? 1.3 and 2.0 have been optimized to improve processing capability and scalability, and most of the improvements will take effect by default. However, at the time of compilation and running, 2.0 also has many options that can significantly improve the performance. This article does not want to describe commands that exchange functions for speed, such as HostnameLookups, but just shows the core feature that affects performance in 2.0: MPM (Multi? -Processing? Modules, multi-channel processing module.

It is no exaggeration to say that the introduction of MPM is Apache? 2.0 most important changes. We all know that Apache is based on modular design, while Apache? 2.0 extends the most basic functions of modular design to Web servers. The server is loaded with a multi-channel processing module, which is responsible for binding the network port of the local machine, accepting requests, and scheduling sub-processes to process requests. Extended modular design has two important benefits:

◆? Apache supports multiple operating systems in a more concise and effective manner;

◆? Servers can be customized according to the special needs of the site.

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.

Specifies the MPM method.

The following uses Red? What? Linux? 9 is the platform. what is the description in Apache? In 2.0, how do I specify MPM? (Apache uses 2.0.45 ). First decompress the source code package httpd-2.0.45.tar.gz, generate the httpd-2.0.45 Directory (Apache? The naming rule of the 1.3source code package is apache_1.3.nn.tar.gz, and the version is httpd-2.0.nn.tar.gz, where NN is the minor version number ).

Go to the httpd-2.0.45 directory and run the following code:

$?./configure?--help|grep?mpm

Shown as follows:

--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 ,? Perchild 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? 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. so? Apache is not officially recommended. Therefore, we mainly describe the prefork and worker products that have the greatest performance relationship with MPM? (? For more MPM details, see Apache official documentation: http://httpd.apache.org/docs-2.0/mod ).

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. Which of the following methods does it adopt the pre-school child process? 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, compile and make in make? After installing install, use "httpd? -L "to determine the currently used MPM, you should see prefork. c (if you see worker. c, the worker 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
 

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 number of created processes is met? The value set by MinSpareServers. 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. When each sub-process processes "MaxRequestsPerChild "? Requests will be destroyed automatically. 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:

◆? This 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. Its default value? 150 is far from enough. if the total number of requests has reached this value (via 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 we set this value to greater than 256, then? Apache cannot be started. In fact, 256 is not enough for sites with a slightly higher load. In Apache? In 1.3, this is a hard limit. If you want to increase this value, you must find it in src/include/httpd. h under the source code tree that was manually modified before "configure? 256, you will find "# define? HARD_SERVER_LIMIT? 256. Change 256 to the value to be increased (such as 4000), and then re-compile Apache. In Apache? The ServerLimit command is added in 2.0, so that MaxClients can be increased without recompiling 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 20000, 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, is the worker 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. How does this MPM work? 2.0 development trend.

In configure? -With-mpm = worker, compile and make? Install. The following configuration segments are available in the httpd. conf generated by default:

 
      StartServers?2    MaxClients?150    MinSpareThreads?25    MaxSpareThreads?75    ThreadsPerChild?25    MaxRequestsPerChild?0
 

The working principle of worker is that a "StartServers" sub-process is generated by the main control process. each sub-process contains a fixed ThreadsPerChild? Number of threads. 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.

Is ThreadsPerChild a worker? The most performance-related command in MPM. The maximum default value of ThreadsPerChild is 64. if the load is large, 64 is not enough. What is the explicit usage? ThreadLimit command. its 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 ). What is the author's? Worker configuration section:

 
      StartServers?3    MaxClients?2000    ServerLimit?25    MinSpareThreads?50    MaxSpareThreads?200    ThreadLimit?200    ThreadsPerChild?100    MaxRequestsPerChild?0
 

Through the above description, you can understand Apache? In 2.0, prefork and worker are two important MPM operating principles. Apache-related core parameters can be configured according to the actual situation to achieve maximum performance and stability.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.