Adjust Apache2 performance optimization

xiaoxiao2021-03-06  41

MPM has a variety of ways such as Prefork and Worker.

HTTPD -L can see which way the current Apache is used.

MPM's WORKER mode is a combination of multi-process and multi-threaded.

First of all, use

./configure --enable-mods-shared = all --enable-modules = so --with-mpm = worker

Make

Make Install

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 MinserveServers 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. Here is the author's prefork.c> StartServers 10 Minservers 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 child process is 16, and it is also necessary to explicitly declare the ServerLimit when it is increased. The maximum 20000). 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-86113.html

New Post(0)