Apache's working mode

Source: Internet
Author: User
Tags echo date

Personal understanding: Apache's mode of operation is the memory allocation of Apache at runtime, the way processes and threads are used. For example: an Apache running server, if there is a user access to this Apache, then Apache is to enable a process to process the user's request? Or are you enabling a thread in an existing process to process the user's request? This choice is an Apache work model to determine. If a working mode such as Prefork mode is specified, then each user's request is enabled by a process to process.

How many working modes does Apache have? How do I view and modify Apache's working mode?

Apache's working modes are: Beos,event,worker,prefork,mpmt_os2.

Use the #http–l command under Linux (CentOS) to view the working mode you are currently using. You can also use the #apachectl-l command.

#http –l

#apachectl –l

See the PREFORK.C that illustrates the use of the Prefork working mode.

You can modify the working mode by using the #–with-mpm=prefork corresponding work mode name at compile time.

BeOS working mode (not related to Linux, or temporarily unused)

Working mode on the BeOS system, a separate control thread is used to create and control the worker thread that processes the request.

Event mode of operation (not very stable, or temporarily unused)

Because the event mode separates the service process from the link, the higher load can be sustained by the relative worker mode in the open keepalive situation. The event mode is a variant pattern developed by the worker, and the configuration and instructions are exactly the same as the worker. However, the event mode does not support HTTPS access very well, and sometimes a series of problems arise.

Worker working mode (not good with PHP, or temporarily unused)

Worker mode is able to handle a huge amount of requests because it uses threads to process requests, and system resources are less expensive than process-based servers. The worker pattern also uses multiple processes, each of which has multiple threads to achieve stability based on the process server.

Mpmt_os2 working mode (rarely used, or temporarily unused)

The MPMT_OS2 is a hybrid multi-process multithreaded multi-processing module (MPM) specifically optimized for OS/2.

Prefork working mode (main character of this article, most used and most stable mode of operation)

Prefork operating mode is the default operating mode for Linux under Apache installation, and is the most common mode of operation. To be able to simply understand how he works, here's a hypothesis:

There is a running Apache server, and when user a accesses the Apache, Apache establishes a new Process 1 processing User A's request.

Then another user B accesses the Apache,apache and establishes a new process 2 to process User B's request.

Later, users c,d,e access to the Apache,apache and set up three processes 3,4,5 process their requests.

If every time a new user accesses the Apache,apache and then establishes a new process to handle the user's request, is it too slow?

So Apache's Prefork mode establishes 5 processes at the first launch of Apache, waits for a user's connection request, and has a user access that processes his request.

So if there are 5 users accessing apache,apache at the same time, the first 5 processes are all used up, so Apache is again building 5 processes, waiting for the next batch of user requests.

Depending on the hardware of the server, the Prefork mode can set up up to 256 processes at a time. A few more requests can only wait for the previous process to finish processing.

Let's say it's finished!

The above hypothesis is how the Prefork mode works. However, the above hypothesis is not a dead number, but is set by the configuration of the prefork mode. The following is the configuration information in http.conf.

* Annotated section with a pound sign

Several important parameters of the Prefork model:

Name Default Value Description

Startservers 5 The number of processes started by default when Apache starts

Minspareservers 5 Minimum number of idle processes

Maxspareservers 10 maximum number of idle processes

Serverlimit 256 Maximum process totals (reference, actual see maxclients)

MaxClients 256 maximum number of processes

Maxrequestsperchild 4000 The maximum number of requests processed per process

startservers: This will clear the eyes.

minspareservers: For example, I understand.

Apache has 5 idle processes when there is no user access, if there is a user visiting the site. The idle process is only 4, which is less than minspareservers, so Apache creates a new idle process at the speed of 1 processes in the first second, 2 processes in the second second, and 4 processes in the third second. The idle process does not end until it is greater than or equal to minspareservers.

maxspareservers: Let's just give an example.

Apache has 5 idle processes without user access, if 5 users visit the site at the same time. The idle process is only 0, which is less than minspareservers, so Apache creates a new idle process at the speed of 1 processes in the first second, 2 processes in the second second, and 4 processes in the third second. The idle process does not end until it is greater than or equal to minspareservers. In this example, until the third second, a total of 1+2+4 processes are generated to meet the requirements greater than or equal to minspareservers. Later, these 5 users finished accessing Apache, the end of the visit, close the browser. So Apache has 5+7 a free process. At this point the idle process is more, and Apache begins to shut down some processes until the completion of less than maxspareservers idle processes. If the value is less than Minspareservers, Apache defaults to set the value to Minspareservers+1.

serverlimit: This parameter controls the total number of Apache processes, so why do you have two parameters to control the total number of Apache processes? This parameter is not in the era of apache1, because there is a 256M memory server is very powerful. Later the Apache2 era came and the server hardware was upgraded. Many servers are 4G of memory, and many servers that are larger than 4G memory appear. Apache1 era only one maxclients parameter control the total number of processes is enough, and the maximum value of this parameter is 256 dead. But in the age of apache2, you must adjust the Serverlimit value greater than 256 to enable maxclient to support values greater than 256.

maxclients: The largest number of Apache processes. Apache1 era only one maxclients parameter control the total number of processes is enough, and the maximum value of this parameter is 256 dead. But in the age of apache2, you must adjust the Serverlimit value greater than 256 to enable maxclient to support values greater than 256.

Maxrequestsperchild: For example, I understand.

Apache has 5 idle processes without user access. When a user visits a website, it is finished and left. Then Apache's first process processes a request and enters the idle state from the new one. Another user visits the website and leaves after the visit. Then Apache's first process processed the request. This continues to access 3,998 users, the process processes 4,000 requests, and then shuts down the process automatically. At this time, Apache has only 4 restricted processes, less than the Minspareservers value so Apache builds an idle process from now on. Why would you close the process when you're done with 4,000 requests? One of the answers: to prevent memory leaks.

The following diagram describes these parameters (to understand a deep understanding of a knowledge to personally experience)

Test Environment Parameter 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 <=; $i + +) {
echo Date (' H:i:s ', Time ());
Echo ' <br/> ';
Sleep (10);
}
?>

Test the Prefork mode setting for http.conf: (the setting is a bit extreme, but you can understand the explanation well)

First, look at the idle process for the first time Apache starts:

The process of viewing Apache under Linux (CentOS) can use the #ps-ef|grep httpd command to view the memory usage of the Apache process can use the #ps–u apache–u Apache U command

#service httpd Restrat: Restart Apache, initialize process

#ps-ef|grep httpd: The user named Apache is the process that Apache uses to process user requests

#ps –U apache–u Apache U: View the process that the user is named Apache (that is, the process details that Apache established to process user requests)

Apache is currently establishing Startservers=1 processes for no user access

When there is a user access (through Internet Explorer on this computer to access the virtual host on the Apache server mode that scenario)

You can see that the Apache process is increased to 2, why? When the only idle process is finished, the idle process is less than minspareservers,apache to create a new process. Note the idle process that looks at the RSS value (KB of memory usage for the process) is 4424.

Then look at the number of Apache processes after the visit is complete.

You can see that there are only two processes, and both processes are idle.

Impersonate the scenario when there are two user accesses (via the two IE browsing on this computer to access the Apache server on the virtual host)

You can see that the number of Apache processes increased to 3 when two users visited. Why is it? When the idle 2 process is finished, the idle process is less than minspareservers,apache to create a new process. Note the idle process that looks at the RSS value (KB of memory usage for the process) is 4424.

Then look at the number of Apache processes after the visit is complete.

You can see that the number of idle processes is reduced to 2. Why is it? When the two user access is completed, two users of the process will be closed, this time there will be 3 idle processes, this value is greater than the value of maxspareservers=2. So Apache will automatically turn off a process that satisfies the maxspareservers=2 value.

Simulate the scenario when there are three simultaneous users accessing the Apache server on the virtual host via the three IE browsing on this computer

You can see that the Apache process has 3, and that all three processes are processing the user's request and no extra idle processes are generated. Why is it? Because in the above we set the value of MaxClients to 3. So Apache only generates 3 processes at most.

Take a look at the number of Apache processes after these three users have completed their visit.

You can see that the number of idle processes is reduced to 2. Why is it? When the three user access is completed, three users of the process will be closed, this time there will be 3 idle processes, this value is greater than the value of maxspareservers=2. So Apache will automatically turn off a process that satisfies the maxspareservers=2 value.

Simulate the scenario when there are four simultaneous users accessing the Apache server on the virtual host via the four IE browsing on this computer

You can see that although there are four simultaneous users, the number of processes created by Apache is only 3. This means that Apache only handles the previous 3 user requests. A 4th user can only wait.

This is the time to view the Apache TCP connection situation. (You can click to see if the TCP connection is not quite understood)

You can see that all 4 users have already established a connection. But Apache only processed the previous 3 requests. After processing 3 requests, the last request is processed, which may be seen from the printed contents of the file. Four users are almost simultaneously accessed.

You can see that the last user's request time starts after the previous three user access is complete.

And so on, what would it be like to have 7 users accessing it at the same time? Just stick a few pictures, and you'll see.

Conclusion

If you read this article from beginning to end, the following conclusions will be drawn:

1,apache is assigned and managed in strict accordance with the configuration parameters of the Prefork mode. Unlike some articles on the web that modify a certain value, Apache does not work. You can only say that you have changed the wrong, or that you do not understand the above content.

The 2,maxclients value is the maximum number of processes for Apache. Unlike some articles on the internet, the bigger the better (some articles recommend this value is 4000), you can see from the process diagram above that Apache's%mem (percent of memory consumption) value is about 0.5%. So the maximum number of specific settings for this value is: 100/0.5 = 200. And this is only in the case of this server only one Apache. If the server has other programs that require memory (such as MySQL) This value is less than 200. You can't always give Apache all the memory of the operating system?

3, each browser is a user, each user is a process. You know what that means? The concurrency of my server is only 200. That is, my server can only support 200 user access, and then more users can only wait. Or, I have this server that only supports 200 browser access. About the concurrency of the server I will explain in detail in the following article.

Is the concurrency of 4,200 very small? Be aware that Apache is processing data at a fairly fast rate. A normal home access may be processed in one second. So in the hypothetical state, my server, every minute can access 60*200=12000 users. Access to 12000*60*24=17280000 users is accessible daily. This is certainly the hypothetical data in the fully saturated access state. Next article I will explain the website in detail, request, combo and other content.

5, if the number of users accessed is greater than the number of maxclients, the more users will not immediately disconnect, or establish a TCP connection. Just wait for the previous user to finish working on getting the corresponding. In the php.ini,http.conf, the operating system is set to the timeout period will not be appropriate to disconnect the connection.

Apache's working mode

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.