APACHE 2.0 Performance Optimization - Selection and Configuration of MPM

xiaoxiao2021-03-06  46

Talking about Apache, most system administrators are very impressed with their stable version 1.3. Although Apache 2.0 has long developed from Alpha, Beta to the current GA (General Availability), some people think that development is not available to the stable version of the production environment. In particular, the version 1.3 version of the API is not compatible with version 2.0, making a large number of modules must be rewritten to use it on version 2.0. The internal change between Apache 1.3 and 2.0 is indeed, with Apache founder Brian Behlendorf's own words: "This version includes hundreds of new features, so this product should have a product number such as 3.1 or 8i, and Not 2.0. "Apache 2.0 joins a lot of core improvements and new features, such as UNIX threads, multi-protocol support, new build systems, better support for non-Unix platforms, IPv6 support, new Apache API, filter, Multi-language error response, native Windows NT Unicode support, simpler configuration, and upgrade regular expression library, etc. Of course, it also includes important improvements to many modules, and has also joined some new modules.

In order to make Apache to upgrade from version 2.3 to version 2.0, the Apache development team has done a lot of work. Many important modules are now able to support version 2.0, such as PHP, Fastcgi, MOD_PERL, MOD_PYTHON, etc. On HTTPD.conf instruction configuration syntax, the current version 2.0.45) and version 1.3 have been pretty good. For example, for the previous version 2.0, if you want to use PHP, it is generally implemented with a filter; the current version of the LoadModule statement in version 1.3 is used as a recommended way to load PHP in the PHP official document. As long as you understand the new features of Apache 2.0, upgrade from version 1.3 to version 2.0 will be a very easy thing. Using Apache 2.0 is a general trend, because Apache's development team has transferred the development center to version 2.0. The 1.3 version has been released from 1.3.27 since October 2002, and the 2.0 version was released in January this year after 2.0.43 in January this year after 2.0.43 in January this year. 2.0.45 and contain many improvements and corrections.

MPM introduction brings performance improvement

Apache 2.0 improves the most attractive person in performance. On UNIX systems that support POSIX threads, Apache can run in a multi-process and multi-threaded phase mixed mode, enhanced the expandable performance of the partial configuration with a different MPM. A large amount of optimization is made compared to the Apache 1.3, 2.0, and most improvements can take effect in the default state. But in compilation and running, there are many options that can significantly improve performance. This article does not want to describe those instructions such as Hostnamelookups, etc., but only explain the core characteristics of the surfactity in 2.0: the basic working principle and configuration instructions of MPM (Multi-Processing Modules, Multiple Division).

Not exaggerated, the introduction of MPM is the most important change in Apache 2.0. Everyone knows that Apache is based on modular design, and Apache 2.0 has expanded 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 native network port, accepting requests, and dispatching processes to handle requests. There are two important benefits in expanding modular design:

◆ Apache can be more concise and effectively support multiple operating systems;

◆ The server can customize the special needs of the site.

At user-level, MPM looks very similar to other Apache modules. The main difference is that only one MPM can be loaded into the server at any time.

Method for specifying MPM

The following is a platform with Red Hat Linux 9, explaining how MPM in Apache 2.0 (Apache is 2.0.45). First decompress the source code package httpd-2.0.45.tar.gz, generate httpd-2.0.45 directory (Apache 1.3 source code package naming rules are apache_1.3.nn.tar.gz, and version 2.0 is httpd- 2.0.nn.tar.gz, where NN is the secondary version number). Enter the HTTPD-2.0.45 directory, run the following code:

$ ./Configure --help | grep mpm

The display is as follows:

--with-mpm = mpm

Choose the process model for apache to use.

MPM = {BEOS | Worker | Prefork | MPMT_OS2 | Perchild | Leader | threadpool}

The above operation is used to select the process model to use, which is the MPM module. BEOS, MPMT_OS2 is the default MPM, which is the default MPM, and OS / 2, respectively. The primary design of Perchild is to run different sub-processs with different users and groups. This is especially useful when running multiple virtual hosts that require CGI, making it better than the SUEXEC mechanism in version 1.3. LEADER and THREADPOOL are all based on Worker, but also in experimental phases. In some cases, it does not work as expected, so Apache official is not recommended. Therefore, we mainly describe the two-kind and performance relationship of Prefork and Worker (for details on other MPMs, see Apache official document: http://httpd.apache.org/docs-2.0/mod/) .

PREFORK work principle and configuration

If you don't have to specify a "--with-mpm", Prefork is the default MPM on the UNIX platform. The pre-paused child process it use is also the model used in Apache 1.3. Prefork itself does not use threads, version 2.0 is used to maintain compatibility with version 1.3; on the other hand, PREFORK has handled different requests with separate child processes, and between the processes are independent of each other, which makes it become One of the most stable MPMs.

If you use Prefork, after Make Compilation and Make Install installation, use "httpd -l" to determine the current Used MPM, you should see prefork.c (if you see Worker MPM, you can push ). Then check the default httpd.conf profile, which contains the following configuration segment:

StartServers 5

MinSpareServers 5

MaxSpareServers 10

MaxClients 150

MaxRequestSperChild 0

Prefork works, after the control process originally creates a "startservers" process, in order to meet the needs of MinsPareServers settings, wait for a second, continue to create two, wait a second, continue to create four ... In this way, the number of processes created according to the exponential level, up to 32 per second until the value of Minservers is satisfied. This is the origin of prefork. This mode can not have to generate new processes when requested, thereby reducing system overhead to increase performance.

MaxSpareServers set the maximum number of idle processes, if the number of idle processes is greater than this value, Apache will automatically kill some extra processes. This value does not set too much, but if the value set is smaller than the MinsPareServers, Apache will automatically adjust to minSpareServers 1. If the site load is large, consider increasing MinSpareServers and MaxSpareServers. MaxRequestSperChild Sets the number of requests that can be handab to each child process. Each child process will be automatically destroyed after processing the "maxRequestsperchild" request. 0 means infinity, that is, the child process will never be destroyed. Although the default set is 0 to make each child process to process more requests, but if there is a non-zero value, there are two important benefits:

◆ Prevent unexpected memory leaks;

◆ Hou will automatically reduce the number of child processes when the server load has dropped.

Therefore, this value can be adjusted according to the load of the server. The author believes that 10,000 is relatively appropriate.

MaxClients is the most important one in these instructions, setting Apache's request at the same time, is the maximum influence on Apache performance. Its default 150 is far less than enough. If the total number of requests has reached this value (can be confirmed by PS-EF | GREP HTTP | WC -L), then the following request is to queue until a certain request is completed. . This is the main reason why the system resources remain a lot and HTTP access is slow. System administrators can dynamically adjust this value based on hardware configuration and load conditions. Although this value is theoretically, the more requests can be handled, but the Apache default restriction cannot be greater than 256. If this value is set to be greater than 256, then Apache will not be able to start. In fact, 256 is not enough for the site for load. In Apache 1.3, this is a hard limit. If you want to increase this value, you must find 256 in the SRC / Include / httpd.h of the source code tree in front of "configure", will find the "#define hard_server_limit 256". Change 256 to an increase value (such as 4000), then recompile Apache. The ServerLimit directive is added to Apache 2.0 so that you can increase MaxClients without compiling Apache. Below is the author's prefork configuration paragraph:

STARTSERVERS 10

MinspareServers 10

MaxSpareServers 15

ServerLimit 2000

MaxClients 1000

MaxRequestsperChild 10000

In the above configuration, the maximum value of ServerLimit is 20000, which is already sufficient for most sites. If you must increase this value, you can modify the following two lines in Server / MPM / prefork / prefork.c under the source tree:

#define default_server_limit 256

#define max_server_limit 20000

Worker's work principle and configuration

Compared to Prefork, Worker is a new version of MPM that supports multi-threaded and multi-process mixed models in version 2.0. Since the thread is used, the relative massive request can be handled, and the overhead of the system resource is less than the process-based server. However, Worker also uses a multi-process, each process generating multiple threads to obtain the stability of the process server. This MPM's way to work will be the development trend of Apache 2.0.

After configure -with-mpm = worker, Make Compilation, Make Install installation. There is the following configuration segment in the default httpd.conf:

STARTSERVERS 2

MaxClients 150

Minsparethreads 25

Maxsparethreads 75

ThreadsperChild 25

MaxRequestSperChild 0

The WORKER's work is that the "StartServers" process is generated by the main control process, and each sub-process contains a fixed ThreadsperChild thread number, each thread handles the request. Similarly, in order not to regenerate the thread, MinSparethreads and MaxSparethreads set the minimum and maximum number of idle threads; and MaxClient 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 derpt the new sub-process.

The largest default values ​​of MinSparethreads and MaxSparethreads are 75 and 250, respectively. These two parameters have little impact on Apache's performance, and can be adjusted accordingly according to the actual situation.

Threadsperchild is the most closely related instruction related to performance in Worker MPM. Threadsperchild's maximum default is 64. If the load is large, 64 is not enough. At this time, it is necessary to explicitly use the ThreadLimit directive, and its maximum default is 20000. The above two values ​​are located in the source tree server / mpm / worker / worker.c:

#define default_thread_limit 64

#define max_thread_limit 20000

These two lines correspond to the limit of Threadsperchild and Threadlimit. It is best to change 64 to the desired value before configure. Note that do not set these two values ​​too high, exceeding the system's processing power, so that the system is very unstable due to Apache.

The total number of requests that can be handled at the same time in Worker mode is determined by the total number of sub-process by threadsperchild values, which should be greater than equal to MaxClients. If the load is large, the existing child process cannot meet, the control process will derive a new child process. The default maximum sub-process is 16, and it is also necessary to explicitly declare ServerLimit when it is increased. These two values ​​are located in the source tree server / mpm / worker / worker.c:

#DEFINE DEFAULT_SERVER_LIMIT 16

#define max_server_limit 20000

It should be noted that if ServerLimit is explicitly declared, it must be greater than equal to MaxClients with ThreadsperChild, and MaxClients must be an integer multiple of ThreadsperChild, otherwise Apache will automatically adjust to a corresponding value (possibly a non-desired value). Below is the author's Worker configuration segment:

STARTSERVERS 3

MaxClients 2000

ServerLimit 25

MinSparethreads 50

Maxsparethreads 200

Threadlimit 200

ThreadsperChild 100

MaxRequestSperChild 0

转载请注明原文地址:https://www.9cbs.com/read-71292.html

New Post(0)