Home >php教程 >php手册 >How Apache increases the number of concurrent connection requests

How Apache increases the number of concurrent connection requests

WBOY
WBOYOriginal
2016-08-30 09:20:321226browse

Sites that use Apache as an HTTP server will inevitably encounter this problem: apache has a very low load, but accessing the web server is very slow. After excluding the cause of the script program, it is basically a problem of apache settings.

The following content may help resolve this issue.
1. First, you must understand the MPM (Multi-Processing Modules) used by Apache

MPM is the core of Apache. Its role is to manage network connections and schedule requests.
MPM in Apache2.0 is divided into three types (perfork, worker, event). perfork is inherited from Apache1.3. It adopts process management method, so it can provide more reliable performance and better compatibility; worker is a new method added in Apache2.0, which adopts thread control method. , it can save system overhead and process more data than perfork, but at the same time, the compatibility is not very good, and many old programs cannot work under the worker; event is still in the experimental stage, and it allocates different processes to each task. Pool, should not be used at this time.
You can get which MPM is currently used by Apache through the command httpd -l
This article only discusses the perfork method

2. Understand how perfork works

Check the configuration file httpd.conf of Apache2.0 and 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 will automatically create StartServers processes and try its best to keep the number of idle processes between MinSpareServers and MaxSpareServers.
If the idle processes are smaller than MinSpareServers, Apache will create new processes at a rate of approximately 1 per second.
If the idle processes are smaller than MaxSpareServers, Apache will delete excess idle processes and release server resources.
The maximum number of processes is controlled by MaxClients. In Apache1.3, the maximum can only be set to 256. However, in Apache2.0, you can break the limit of 256 by adding the ServerLimit item at the beginning of the configuration. In this case, MaxClients ≤ ServerLimit ≤ 20000
MaxRequestsPerChild is used to control how many requests each process will be automatically destroyed after processing. This parameter can be set to 0 to indicate unlimited (that is, the process will not be destroyed).



3. Optimize perfork

First of all, for a website with a relatively high load, the process limit of 256 is not enough. If the server has reached the limit of 256, then the next access needs to be queued. This is why the load of some servers is not high, but One of the reasons why access is so slow. So first you should understand the number of processes when the server is busy.
You can know the number of Apache processes in the current system through the command ps -ef|grep httpd|wc -l, and achieve the purpose of soft expansion of the server by setting ServerLimit and MaxClients.

Then, during peak traffic times, what often happens is that all of a sudden there are a lot of concurrent connections, and then suddenly there's a lot less access. If Apache does not prepare a sufficient number of preparation processes, then access can only wait for Apache to add one new process per second, and then delete the redundant processes. Then Apache can only be busy creating and destroying processes, which greatly reduces access. speed. You can appropriately increase StartServers, MinSpareServers, and MaxSpareServers so that Apache does not have to be busy doing useless work.

Finally, it is strongly recommended not to set MaxRequestsPerChild to 0, but to set it to non-0, which can protect the Apache process from memory leaks, because you don't know when the application running on Apache will go wrong and cause memory leaks.

After setting it up, it looks like this:

ServerLimit 1000
StartServers 30
MinSpareServers 30
MaxSpareServers 45
MaxClients 1000
MaxRequestsPerChild 4000

Detailed steps http://www.liaoxiansheng.cn/?p=496

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn