Apache Real-Combat optimization

Source: Internet
Author: User

Apache in Linux and Windows combat optimization

Under Linux:

1. First understand the MPM (multi-processing Modules, multi-channel processing module) used by Apache
MPM is the core of Apache, its role is to manage network connections, scheduling requests.

We all know that in Apache2.0, MPM is divided into 3 kinds: perfork, worker, event.

Perfork inherited from Apache 1.3, it uses a process management approach, so it can provide more reliable performance and better compatibility;worker Is the new way of Apache2.0, it adopts the thread control method, can save the system overhead and processing more data than perfork, but the compatibility is not very good, many old programs can't work under the worker; The event is still in the experimental phase. It allocates a different process pool for each task and should not be used at this time.


Command Httpd-l to get the current type of MPM used by Apache
This article is for discussion only on Perfork mode


2. Learn how Perfork works:
Looking at the configuration file for Apache2.0 httpd.conf, you can see the configuration section of Perfork, which is roughly as follows:

Serverlimit 256

Startservers 5

Minspareservers 10

Maxspareservers 15

MaxClients 256

Maxrequestsperchild 4000


When Apache is started, Apache automatically creates startservers processes and tries to keep the number of idle processes between Minspareservers and maxspareservers.
If the idle process is less than minspareservers,apache, the process will be created at a rate of approximately 1 per second.
If the idle process is less than maxspareservers,apache, the extra idle process is removed and the server resource is freed.
The maximum number of processes is controlled by maxclients and can only be set to 256 in Apache1.3, but in Apache2.0, you may exceed the 256 limit by adding serverlimit items at the beginning of the configuration, at which point you must maxclients≤ serverlimit≤20000
Maxrequestsperchild is used to control how many requests each process is automatically destroyed, and this parameter can be set to 0 for infinity (that is, no process is destroyed).


3. Optimize Perfork
First, for a relatively high-load site, 256 of the process limit is not enough, if the server has reached the limit of 256, then the next access will need to queue, which is why some server load is not high, but access is very slow one of the reasons. So the first thing you should know is the number of processes that the server is busy.


By command Ps-ef|grep Httpd|wc-l can understand the current number of Apache processes in the system, by setting Serverlimit and maxclients to achieve the purpose of soft expansion of the server.


Then, during peak traffic times, a lot of concurrent connections suddenly occur, and then suddenly there is a significant reduction in access. If Apache does not prepare a sufficient number of preparatory processes, the access can only wait for Apache to 1 new processes per second, and then remove the redundant processes, Apache can only be busy with new and destroyed processes, greatly reducing the access speed. Startservers, Minspareservers, and maxspareservers can be properly added to make Apache do not have to be busy working hard.

Finally, it is strongly recommended that the Maxrequestsperchild not be set to 0 and set to non-0 to protect the Apache process from memory leaks because you do not know when an application running on Apache will cause a memory leak.

This is roughly the case after Setup:

Serverlimit 1000

Startservers 30

Minspareservers 30

Maxspareservers 45

MaxClients 1000

Maxrequestsperchild 4000


Under Windows:

(1) Preferred to view Apache working mode
View Apache's working mode command under Windows: Httpd-l
If MOD_WIN32.C is listed, it means that win32.c works. The full list of contents is as follows:
Core.c
Mod_win32.c
Mpm_winnt.c
Http_core.c
Mod_so.c

MPM_WINNT.C is dedicated to Windows NT-optimized MPM (multi-processing module), which uses a separate parent process to produce a separate subprocess that, in turn, produces multiple threads in this subprocess to process requests. This means that mpm_winnt can only start a parent-child two process and not start multiple processes at the same time as Linux.
Mpm_winnt mainly through the Threadsperchild and maxrequestsperchild two parameters to optimize Apache, the following detailed to explain.

(2) mod_win32.c working mode, the corresponding optimization content as follows
In the httpd.conf file, add the following:
<ifmodule mpm_winnt.c>
Threadsperchild 250
Maxrequestsperchild 5000
</IfModule>

Threadsperchild
This parameter sets the number of threads per process, and the child processes are no longer establishing new threads when they are established at startup. On the one hand, because mpm_winnt cannot start multiple processes, this value should be large enough to handle possible peaks of requests; On the other hand, this parameter is based on the response speed of the server, and the number is too large to slow down. Therefore, it is necessary to synthesize a reasonable numerical balance.

The default value on Mpm_winnt is 64, and the maximum value is 1920. This is recommended to be set to 100-500, the server performance is higher, the value is larger, the inverse value is smaller.

Maxrequestsperchild
This parameter represents the maximum number of requests that each subprocess can handle, that is, the number of child processes at the same time. Set to zero for unlimited, the default value on Mpm_winnt is 0.
The official reference manual is not recommended to be set to 0, mainly based on two points of consideration:
(1) can prevent (accidental) memory leaks indefinitely, thus exhausting memory;
(2) Give the process a limited lifespan, thus helping to reduce the number of active processes when the server load is reduced.

The value of this parameter depends largely on the memory of the server and can be set to 0 or a large number if the memory size is large, otherwise a small amount is set. It is necessary to note that if this value is set too small will cause the Apache to restart frequently, in the log file will see the following text:
Process exiting because it reached Maxrequestsperchild. Signaling the parent

This reduces the overall performance of Apache.

Alternatively, you can verify that the currently set value is reasonable by looking at the Server-status (status report) provided by Apache and open it in the httpd.conf file as follows:

* # First need to load the Mod_status module
LoadModule Status_module modules/mod_status.so
* # Then set the access address
<Location/server-status>
SetHandler Server-status
Order Deny,allow
Deny from all
# If an IP access limit is set to allow from 192.168.0.1
Allow from all
</Location>


This article from "Lu Chunning" blog, declined reprint!

Apache Real-Combat optimization

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.