Repost: Apache 2.0 Performance Optimization

xiaoxiao2021-03-06  143

http://tech.ccidnet.com/pub/Article/c737_a47607_p1.html

Apache 2.0 Performance Optimization Author: ◇ issued Zhenguang Qi Xing flying time: 2003.05.27

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. The introduction of MPM brings performance to improve Apache 2.0 in performance. Improvement. 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 of extending modular design: ◆ Apache can be more concise and effectively support multiple operating systems; ◆ 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, which shows how to specify 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 is shown below:

--with-mpm = mpmchoose 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 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 Working principle and configuration If you do not need "--with-MPM" explicitly specify some 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 5MinSpareServers 5MaxSpareServers 10MaxClients 150MaxRequestsPerChild 0 works prefork that the control process after the initial establishment of "StartServers" sub-process, in order to meet the need to create a process MinSpareServers set, wait a second, continued Create two, wait a second, continue to create four ... The number of processes created in 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 handle more requests, if there is a non-zero value, there are two important benefits: ◆ Prevent unexpected memory leaks; ◆ When the server load has dropped, the other will be automatically reduced The number of child processes. 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 10minsPareServers 10MaxSpareServers 15 ServerLimit 2000MaxClients 1000MaxRequestSperChild 10000 The maximum value of ServerLimit is 20000, which is already enough for most sites. If you must increase this value, you can modify the following two rows in Server / MPM / Prefork / Prefork.c under the source tree: #define default_server_limit 256 # define max_server_limit 20000 Worker's working principle and configuration relative 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 2MaxClients 150MinSpareThreads 25MaxSpareThreads 75ThreadsPerChild 25MaxRequestsPerChild 0 worker working principle is to generate "StartServers" sub-process by the main control process, each child process contains a fixed number of ThreadsPerChild threads, each thread processed independently 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 rows correspond to the limit of ThreadPerchild 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 code tree server / mpm / worker / worker.c: #define default_server_limit 16 # define max_server_limit 20000 Need to note, if it explicitly declares ServerLimit, then it must be multiplied by ThreadsperChild It is equal to MaxClients, and MaxClients must be an integer multiple of ThreadsperChild, otherwise Apache will automatically adjust to a corresponding value (probably a non-desired value). Below is the author's Worker configuration segment:

StartServers 3MaxClients 2000ServerLimit 25MinSpareThreads 50MaxSpareThreads 200ThreadLimit 200ThreadsPerChild 100MaxRequestsPerChild 0 Through the above description, the principle can be understood in Apache 2.0 prefork MPM and worker these two important, and can be configured according to the actual situation Apache Related core parameters to achieve maximum performance and stability.

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

New Post(0)