Apache concurrent optimization setup method

  • 2020-05-10 23:22:48
  • OfStack

Apache optimization steps:

1. First, check the operation mode of apache, and check the command:

httpd -l

Compiled in modules:
                    core.c
                    prefork.c
                    http_core.c
                    mod_so.c
              you can see here that the operating mode is prefork mode.

2. Modify the httpd.conf configuration of apache

This article is about the optimization of vps with 512M memory and apache. Please don't tell me that nginx is much better. So let's get down to business!

Operating system (linux) : CentOS 5.5 32-bit (not that I'm saying this is better, I'm just familiar with Cents)

      memory: 512M
      cpu: 1G(1000MHz, vps actually has this number, it's hard to say)
      apache: 2.2

The main program running is PHP, and the rest is irrelevant.

First of all, as mentioned in the title, the optimization mentioned in this paper is for apache version 2.2 or above, and using perfork MPM mode. perfork is the mode under which apache is installed by default under linux. If apache is used as the server, it is better to use perfork mode. worker mode does not support some functions of php. If you use the win system or neither, then you can choose not to watch or learn.

The following content is based on the blogger's own opinion, and the actual test should be handled according to your server, program and other factors.

In general, we need to optimize apache because apache takes up a lot of memory, which leads to the crash of vps. Because perfork is processed by multiple processes, each process will use a fixed amount of memory. Therefore, limit the number of httpd processes to optimize the role of apache. The memory size of a single httpd process is mainly loaded modules. At least what module is appropriate is hard for me to say, because it depends on your needs. There are some people on the Internet who say that most of the modules that you can't use are listed, and I think this is very misleading, because of the vast majority of people, maybe you are one of them. Block out useful modules, and in the worst case, restart apache will go wrong, or some of the functionality will become unusable. So I'm not going to list a minimal module loading scheme. So, I'm just going to say 1 reference.

The module loading under apache(perfork) is not the module loading list, and the missing modules are not shielded, but I don't know its actual function.

(1) for apache2.2, the module name is as useful as cache 1, because many of these templates are supported by apache above 2.1, so this kind of module will not exist for no reason.

(2) possible or required modules include:


LoadModule auth_basic_module modules/mod_auth_basic.so
  LoadModule authn_file_module modules/mod_authn_file.so
  LoadModule include_module modules/mod_include.so
  LoadModule log_config_module modules/mod_log_config.so
  LoadModule expires_module modules/mod_expires.so
  LoadModule deflate_module modules/mod_deflate.so
  LoadModule headers_module modules/mod_headers.so
  LoadModule setenvif_module modules/mod_setenvif.so
  LoadModule mime_module modules/mod_mime.so
  LoadModule autoindex_module modules/mod_autoindex.so
  LoadModule negotiation_module modules/mod_negotiation.so
  LoadModule dir_module modules/mod_dir.so
  LoadModule alias_module modules/mod_alias.so
  LoadModule rewrite_module modules/mod_rewrite.so

(3) I have roughly clicked 1, and the module in apache is about 50+. It is impossible to select the module loading list applicable to everyone. At least, I cannot use cgi module, but I do not exclude that you cannot use it.

The module load this 1 block said here, this we more than baidu, multi-purpose nature will identify 1 some useful modules and gradually shield the module is not used.

Then there is the most important perfork configuration, which has been bothering me for a long time. Even though there are only six parameters, that is enough to crash your vps. List the parameters that need to be modified. The changes are located in the httpd.conf file


Timeout 30
KeepAlive On
MaxKeepAliveRequests 80
KeepAliveTimeout 15
<IfModule prefork.c>
ServerLimit 150
StartServers      5
MinSpareServers   5
MaxSpareServers   10
MaxClients       150
MaxRequestsPerChild 4000
</IfModule>

The above code is to be modified. In fact, we need to modify the parameters in perfork. The reason why Timeout,KeepAlive and so on are also written is that this also affects the performance of apache.

The Timeout program is applicable if the parameter is set at 30-60, which is 1. At least some php programs that will take up a lot of time should be run. It is ok to adjust the php program appropriately, but please do not put it too high, otherwise it will affect the performance of apache.

MaxKeepAliveRequests is the maximum number of requests for 1 connection. For pages with more pictures and other elements, it can be adjusted to 1 point higher. For 1-like pages, 80-120 is enough, so we will set it to 100.

KeepAliveTimeout is when the user processes a connection, if there is a request within the parameter's time, the execution will continue, no need to re-create a new connection, until the maximum of MaxKeepAliveRequests is reached, will exit. For perfork mode, some people think that KeepAlive Off will be better, but for most websites, there will be no more or less picture elements, so open this item and set KeepTimeOut at 2-5 seconds, which will not only effectively improve the performance of the server, but also speed up the page opening speed.

The next step is to enter the parameters of perfork. If you don't want the server to run 1, it will take up more memory.

The first is the parameter ServerLimit, which determines the range of values that the server can set for the following parameter MaxClient. ServerLimit actually only serves as a constraint, it doesn't actually work, maybe it does, I don't know. What actually works is the MaxClient parameter, but this value is limited by ServerLimit, which we'll talk about in a second.

The other three parameters are StartServers, MinSpareServers, MaxSpareServers. Why do you call these three parameters 1? Because these three Numbers are related to 1. These three Numbers all determine the number of idle processes. StartServers should range between MinSpareServers and MaxSpareServers. Otherwise, apache will automatically restore the value to between both, so don't waste unnecessary resources. According to the default configuration of perfork, the three parameters are: 5,5,10. But for vps with only 512M memory, I think there is still too much load. Therefore, I think we should take a step back here and set the three parameters to 4,4,10. It doesn't look any different from the above 5,5,10. The maximum value is still 10, but in actual use, 4,4,10 will release memory much faster than 5,5,10.

Next is the maximum number of clients that MaxClient can support to connect to the server at the same time. For the 512M vps you don't want your station to be able to handle millions of PV either, using the default 150 is out of load. 1. You can set ServerLimit and MaxClient as 1 sample.

Finally, there is the MaxRequestsPerChild parameter. How many times does a process exit after processing? If it is set to 0, it will exit infinitely, that is to say, the httpd process will not exit. Well, just wait for your vps to crash and reboot. For this parameter, I did a rough search of 1, many people recommended 1000 times, but also 100 times. In my tests, for an vps with only 512M in memory, a value above 500 would quickly fill up memory, but not less than crash. As you can see, 1 but memory is full, the usage of cpu is almost zero. Therefore, when the machine is down, nothing can be done, so it is better to consume 1 more cpu resources to avoid and improve stability. So, my final decision was to define the value between 30 and 40. With this setting, you can see that the memory release rate is greatly improved, but you can also see that the cpu is jumping up and down frequently. This way, even if the memory is full, it can return to normal in the shortest time.

Therefore, for vps of 512MB, apache(perfork mode), the final optimal configuration is as follows:


Timeout 30
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 2
<IfModule prefork.c>
ServerLimit 150
StartServers      4
MinSpareServers   4
MaxSpareServers   10
MaxClients       150
MaxRequestsPerChild 40
</IfModule>

In this way, our configuration is basically completed and load testing can be carried out. Load testing, we use the ab test, which declares 1, is to use your local virtual machine to test a static page on the server, not on the server. Before, I did so, because the network speed 1, the effect is relatively good, but others are different network speed, so the test on the server is not accurate.

3. Now let's see how to optimize:
The connection number of           is theoretically as large as possible, but you can check the current apache connection number according to the hardware, CPU of the server, memory, bandwidth and other factors:
         

ps aux | grep httpd | wc -l

          calculates the average memory footprint of httpd:
         

ps aux | grep -v grep |awk '/httpd/{sum += $6;n++};END{print sum/n}'

          this is just a reference. Subtract the resources needed by the server system itself.
        such as memory 2G, minus 500M left for the server, and 1.5G, then you get the maximum number of connections: around 8000.
The configuration of           is as follows:
     

 
<IfModule prefork.c>
     StartServers          5
     MinSpareServers       5
     MaxSpareServers      10
     ServerLimit         5500
     MaxClients          5000
     MaxRequestsPerChild   100
</IfModule>

ServerLimit, which is the key word here, must be placed before MaxClients, which is greater than MaxClients.

4. Restart apache and open the website again to see if there is still a slow problem.

Note: the httpd.conf file can be found using the following command

find / -name httpd.conf


Related articles: