1 Three modes and versions of Apache
Apache currently has three stable MPM (Multi-Processing Module) modes, which are prefork , worker and event .
We can use the httpd -V command to check the version and mode of apache. If your server allows apache, you can try it with httpd -V first. If the prompt is as follows
-bash: httpd: command not found
Then first find the running httpd process, ps -ef | grep httpd, and then directly use the full path of this process for -V operation
/**/**/apache_portal/bin/httpd -V
AH00318: WARNING: MaxRequestWorkers of 20000 would require 100 servers and
would exceed ServerLimit of 60, decreasing to 12000.
To increase, please see the ServerLimit directive.
Server version: Server/2.4.18 (Unix)
Server built: Jul 21 2017 08:43:23
Server's Module Magic Number: 20120211:52
Server loaded: APR 1.5.2, APR-UTIL 1.5.4
Compiled using: APR 1.5.2, APR-UTIL 1.5.4
Architecture: 64-bit
Server MPM: worker
Here we can know that the apache mode is worker mode, and the version is 2.4.18
1) prefork mode
Advantages of Prefork MPM: Prefork some child processes first, and then wait for the connection; it can reduce the overhead of frequently creating and destroying processes. Each child process has only one thread. At a time point, only one request can be processed without worrying about thread safety.
Disadvantages: A process relatively occupies resources, consumes a lot of memory, and is not good at handling high concurrency
2) Worker mode
The worker uses a mixed mode of multi-process and multi-threading . The worker mode also pre-spawns some sub-processes, and then each sub-process creates some threads, and includes a listening thread. Each request will be assigned to a thread to serve. . The thread is lighter than the process, because the thread shares the memory space of the parent process. Therefore, the memory footprint will be reduced . In high concurrency scenarios, there will be more threads available than prefork, and the performance will be better ; In addition, if there is a problem with one thread , it will also cause problems with threads in the same process . If there are problems with multiple threads, it will only affect a part of Apache, but not all. Due to the use of multi-process and multi-threading, thread safety needs to be considered. When using a keep-alive long connection, a thread will always be occupied. Even if there is no request in the middle, it will not be released until the timeout is reached (the problem is also exists in prefork mode)
3) event mode
It is very similar to the worker mode. The difference is that it solves the problem of wasting thread resources during the keep-alive long connection . In the event working mode, there will be some special threads to manage these keep-alive types. The thread, when a real request comes, passes the request to the server thread, and after the execution is completed, it is allowed to be released. This enhances request processing in high concurrency scenarios.
4) Let's look at the average memory occupied by httpd
ps aux|grep -v grep|awk '/httpd/{sum+=$6;n++};END{print sum/n}'
11792.7
Almost 11M
2 Apache configuration and performance optimization
The configuration method of Apache configuration file httpd.conf is as follows. Different mode labels are different, but the parameters inside are similar
The prefork mode part is configured as follows
<IfModule mpm_prefork_module>
StartServers 5
MinSpareServers 5
MaxSpareServers 10
MaxRequestWorkers 250
MaxConnectionsPerChild 1000
</IfModule>
The worker mode part is configured as follows
<IfModule mpm_worker_module>
StartServers 3
ServerLimit 16
MinSpareThreads 75
MaxSpareThreads 250
ThreadsPerChild 25
MaxRequestWorkers 400
MaxConnectionsPerChild 1000
</IfModule>
The event mode part is configured as follows
<IfModule mpm_event_module>
StartServers 3
ServerLimit 16
MinSpareThreads 75
MaxSpareThreads 250
ThreadsPerChild 25
MaxRequestWorkers 400
MaxConnectionsPerChild 1000
</IfModule>
1) StartServers
The number of child processes created when the server starts
2) ServerLimit
Maximum number of processes configured in the system
3) MinSpareServers
The minimum number of idle child processes, such as the default 5; if the current number of idle child processes is less than MinSpareServers, then Apache will spawn new child processes at a maximum rate of one per second, this parameter should not be set too large.
4)MaxSpareServers
The maximum number of idle child processes, the default is 10; if there are currently more idle child processes than MaxSpareServers, then the parent process will kill the extra child processes. This parameter does not need to be set too large . If you set it to be smaller than MinSpareServers, Apache will automatically modify it to the number of MinSpareServers +1.
5)MinSpareThreads
The minimum number of idle threads , the default here is 75
6)MaxSpareThreads
The maximum number of idle threads , the default here is 250
7) ThreadsPerChild
The fixed number of threads generated by each child process.
8)MaxRequestWorkers/MaxClients
Limit the maximum number of client access requests of the server at the same time . MaxRequestWorkers/MaxClients sets the total number of threads in all child processes. If the total number of threads in the existing child process cannot meet the load, the control process will spawn a new child process.
Before Apache 2.3.1, this parameter MaxRequestWorkers was called MaxClients.
9)MaxConnectionsPerChild/MaxRequestsPerChild
Each child process allows the maximum number of requests in its life cycle. If the total number of requests has reached this value, the child process will end. If it is set to 0, the child process will never end.
MaxConnectionsPerChild was called MaxRequestsPerChild before Apache 2.3.9. We can see the explanation as follows
The working principle of worker is that the main control process generates "StartServers" child processes, each child process contains a fixed number of ThreadsPerChild threads, and each thread independently processes requests. Similarly, in order not to spawn threads when the request comes, MinSpareThreads and MaxSpareThreads set the minimum and maximum number of idle threads; and MaxClients sets the total number of threads in all child processes. If the total number of threads in the existing child process cannot meet the load, the control process will spawn a new child process.
The maximum default values for MinSpareThreads and MaxSpareThreads are 75 and 250, respectively. These two parameters have little effect on the performance of Apache, and can be adjusted accordingly according to the actual situation