Apache Working Mode

Source: Internet
Author: User
Tags echo date

What is the working mode of apache?
 
My personal understanding: apache's working mode is the memory allocation and usage of processes and threads during apache runtime. For example, if a user accesses apache, what if apache enables a process to process user requests? Or is a thread enabled in an existing process to process the user's request? This option is determined by the apache working mode. If a working mode such as prefork is specified, each user's request must be processed by a process.
 
How many working modes does apache have? How can I view and modify the working mode of apache?
 
Apache has the following working modes: beos, event, worker, prefork, and mpmt_os2.
 
Use the # http-l command in linux (centos) to view the current working mode. You can also use the # apachectl-l command.
 
# Http-l
 
 
 
# Apachectl-l
 
 
 
If you see prefork. c, the prefork working mode is used.
 
You can use the working mode name corresponding to #-with-mpm = prefork to modify the working mode during compilation.
 
Beos working mode (not related to linux, or temporarily unavailable)
 
In the Beos operating mode, a separate control thread is used to create and control the request processing thread.
 
Event Mode (not stable, or temporarily unavailable)
 
The event Mode separates service processes from links. When KeepAlive is enabled, the server load is higher than the worker mode. Event Mode is a variant mode developed by worker. The configuration and instructions are identical to those of worker. However, the event mode does not support https access, and sometimes there are a series of problems.
 
Worker working mode (not good with php, or temporarily unavailable)
 
Because the worker mode uses threads to process requests, it can process massive requests, and the overhead of system resources is smaller than that of process-based servers. At the same time, the worker mode also uses multiple processes. Each process has multiple threads to obtain the stability of the process server.
 
Mpmt_os2 working mode (rarely used, or temporarily unavailable)
 
Mpmt_os2 is a hybrid multi-process multi-thread multi-path processing module (MPM) specially designed for OS/2 optimization ).
 
Prefork working mode (the main character of this article, the most used and most stable working mode)
 
The prefork working mode is the default working mode when installing apache in linux. It is the most common working mode. In order to be able to understand his working principles, the following is a hypothesis:
 
There is A running apache server. When user A accesses apache, apache creates A new process 1 to process user A's requests.
 
At this time, another user B accesses apache, and apache creates a new process 2 to process user B's requests.
 
Later, users C, D, and E access apache, and apache creates three processes (3, 4, and 5) to process their requests.
 
If apache creates a new process to process user requests every time a new user accesses apache, is it too slow?
 
Therefore, in apache prefork mode, five processes are established when apache is started for the first time, waiting for a user's connection request. When one user accesses the service, one process processes the request.
 
If five users access apache at the same time, all the five processes created by apache are used up for the first time. Therefore, apache creates five new processes and waits for requests from the next batch of users.
 
The prefork mode sets up to 256 processes at the same time for apache based on the server hardware. More requests can only be processed after the previous process is completed.
 
The assumption is complete!
 
The above assumption is the working principle of the prefork mode. However, the specific number in the preceding assumptions is not fixed, but set through the prefork mode configuration. The configuration information in http. conf is as follows.
 
 
 
* Comments section with a well number
 
Several important parameters of the prefork mode:
 
Name
Default Value
Description
 
StartServers
5
Default number of processes started during apache startup
 
MinSpareServers
5
Minimum number of idle Processes
 
MaxSpareServers
10
Maximum number of idle Processes
 
ServerLimit
256
The maximum number of processes (see MaxClients for details)
 
MaxClients
256
Maximum process count
 
MaxRequestsPerChild
4000
Maximum number of requests processed by each process
 
StartServers: You can see this.
 
MinSpareServers: Let's take an example.
 
Apache has five idle processes when no user accesses the website. Then there are only four idle processes. This value is smaller than MinSpareServers, so apache takes the first second as one process and the second as two processes, create idle processes at the speed of four processes in the third second. It will not end until MinSpareServers idle processes are greater than or equal.
 
MaxSpareServers: Let's take an example.
 
Apache has five idle processes when there are no users. If there are five users accessing the website at the same time. Then there are only 0 idle processes, and the value is smaller than MinSpareServers. Therefore, apache takes one process in the first second and two processes in the second, create idle processes at the speed of four processes in the third second. It will not end until MinSpareServers idle processes are greater than or equal. In this example, 1 + 2 + 4 processes are generated in the third second to meet the requirements of MinSpareServers. Then the five users visited apache and closed the browser. Therefore, apache has 5 + 7 idle processes. At this time, there are many idle processes, and apache will start to shut down some processes until the idle processes smaller than MaxSpareServers are completed. If the value is smaller than MinSpareServers, apache sets this value to MinSpareServers + 1 by default.
 
ServerLimit: this parameter controls the total number of apache processes. Why do two parameters control the total number of apache processes? This parameter is not available in the apache1 era, because a server with MB of memory was very powerful at that time. Later, the era of apache2 came, and the server hardware was also upgraded. Many servers use 4G memory, and many servers are larger than 4G memory. In the era of apache1, only one MaxClients parameter can control the total number of processes, and the maximum value of this parameter is 256. However, in the era of apache2, you must adjust the ServerLimit value to be greater than 256 to make MaxClient support a value greater than 256.
 
MaxClients: the maximum number of processes in apache. In the era of apache1, only one MaxClients parameter can control the total number of processes, and the maximum value of this parameter is 256. However, in the era of apache2, you must adjust the ServerLimit value to be greater than 256 to make MaxClient support a value greater than 256.
 
MaxRequestsPerChild: The following is an example.
 
Apache has five idle processes without user access. When a user accesses the website, the user leaves after the visit. The first process of apache processes a request and enters the idle state again. Another user visits the website and leaves. Then, the first apache process processes one or more requests. In this way, the process will continue to access 3998 users, and the process will be automatically shut down after 4000 requests are processed. At this time, apache has only four restricted processes, which are smaller than the MinSpareServers value. Therefore, apache creates an idle process from now on. Why should we close this process after processing 4000 requests? One answer: to prevent memory leakage.
 
The following figure shows the parameters. (To have a deep understanding of these parameters, you must experience them yourself)
 
Test Environment Parameters
Description
 
Operating System
Centos4.5 (Virtual Machine)
 
Server
Apache2.2.6
 
Memory
1G
 
Server address
192.168.212.128
 
Client address
192.168.212.1
 
Test File index. php
 
<? Php
For ($ I = 0; $ I <= 10; $ I ++ ){
Echo date ('H: I: s', time ());
Echo '<br/> ';
Sleep (10 );
}
?> Test the prefork mode setting of http. conf: (The setting is a bit extreme, but it can be well understood)
 
 
 
First, check the idle process when apache is started for the first time:
 
In linux (centos), you can run the # ps-ef | grep httpd command to view the memory usage of apache processes. You can run the # ps-U apache-u command to view the memory usage of apache processes.
 
# Service httpd restrat: restart apache and initialize the process
 
# Ps-ef | grep httpd: apache processes are used to process user requests.
 
# Ps-U apache-u apache u: view the process whose username is apache (that is, the process details created by apache to process user requests)
 
 
 
Currently, apache creates StartServers = 1 process when no user accesses it.
 
When there is a user access (access the apache server mode on the VM through the IE browser on the local machine)
 
 
 
We can see that the number of apache processes has increased to 2. Why? When the only idle process is complete and the idle process is smaller than MinSpareServers, apache creates a new process. Note: Check the idle process whose RSS value (the memory usage of this process is KB) is 4424.
 
Check the number of apache processes after the access is completed.
 
 
 
We can see that there are only two processes, and both are idle.
 
When there are two users to access (access the apache server on the virtual host through two ie views on the local machine to simulate this scenario)
 
 
 
We can see that the number of apache processes increases to three when two users access apache. Why? After two idle processes are completed, the idle process is smaller than MinSpareServers, and apache creates a new process. Note: Check the idle process whose RSS value (the memory usage of this process is KB) is 4424.
 
Check the number of apache processes after the access is completed.
 
 
 
The number of idle processes is reduced to two. Why? After two users access the service, both processes will be shut down. At this time, there will be three idle processes. The value is greater than the value of MaxSpareServers = 2. Therefore, apache will automatically turn off a process that meets the value of MaxSpareServers = 2.
 
When three users access the server at the same time (access the apache server on the VM through three ie views on the local machine to simulate this scenario)
 
 
 
We can see that there are three apache processes, all of which are processing user requests, and no redundant idle processes are generated. Why? Because the value of MaxClients is set to 3 above. Therefore, apache generates up to three processes.
 
Check the number of apache processes after the three users access the service.
 
 
 
The number of idle processes is reduced to two. Why? After three users access the service, all the processes of the three users will be shut down. At this time, there will be three idle processes. The value is greater than the value of MaxSpareServers = 2. Therefore, apache will automatically turn off a process that meets the value of MaxSpareServers = 2.
 
When four users access the service at the same time (access the apache server on the virtual host through four ie views on the local machine to simulate this scenario)
 
 
 
We can see that, although four users access the service at the same time, there are only three processes created by apache. Apache only processes the preceding three user requests. 4th users can only wait.
 
Check the TCP connection of apache. (If you are not familiar with TCP connections, click here)
 
 
 
We can see that all four users have established connections. However, apache only processes the first three requests. After three requests are processed, the last request is processed. This can be seen from the printed content of the file. Almost all four users access the service at the same time.
 
 
 
We can see that the last user's request time starts after the first three users have completed the access.
 
And so on, what happens when there are seven users simultaneously? Just paste a few images and you will understand it.
 
Conclusion
 
If you read this article in detail from start to end, the following conclusions will be drawn:
 
1. apache allocates and manages processes strictly according to the configuration parameters in prefork mode. Unlike some articles on the Internet that modify a value, apache does not work. You can only say that you have not changed the correct content, or that you have not understood the content above.
 
2. The MaxClients value is the maximum number of processes in apache. Unlike some articles on the Internet, the larger the value is, the better (some articles recommend this value as 4000). You can see the % MEM (memory usage percentage) of each apache process in the process diagram above) the value is about 0.5%. Therefore, the maximum number of digits set for this value is 100/0. 5 = 200. This is only when the server has only one apache. If other programs on the server need to occupy memory (such as mysql), the value is smaller than 200. You cannot allocate all the memory of the operating system to apache?
 
3. Each browser is a user, and each user is a process. Do you understand the meaning? The concurrency of my server is only 200. That is to say, my server can only support access by 200 users at the same time, and more users can only wait for access. Or my server only supports access from 200 browsers. I will explain the server concurrency in detail in the following article.
 
Is the concurrency of 4,200 small? You must know that apache processes data quite quickly. Access to a normal homepage may be completed in one second. So in the hypothetical state, my server can access 60*200 = 12000 users every minute. You can access 12000*60*24 = 17280000 users every day. This is of course the hypothetical data in the completely saturated access status. In the next article, I will explain in detail website concurrency, requests, and even attacks.
 
5. If the number of accessed users is greater than the number of MaxClients, more users will not immediately disconnect the connection and establish a TCP connection. However, it will wait until the previous user completes processing and get the corresponding result. In php. ini, http. conf, the connection will be disconnected only when no corresponding timeout value is set by the operating system.
 
Supplement
 
This article is my personal opinion on apache's working mode, connection requests, concurrency, and other content. Some errors are inevitable.
 
Address: http://www.qindamoni.net/139
 

Related Article

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.