1. Introduction to multi-channel processing module mpm
The Apache HTTP server is designed as a powerful and flexible web server that can work on many platforms and environments. Different platforms and environments often require different features, or may achieve the same features in different ways, the most efficient. Apache uses modular design to adapt to various environments. This design allows the website administrator to select the server features by selecting which modules will be loaded on the server during compilation or running. Apache 2.0 extends this modular design to the most basic web server features. It provides an optional Multi-processing module (MPM) to bind to a network port, accept requests, and schedule sub-processes to process requests.
In the user's opinion, mpm is similar to other Apache modules. The main difference is that there must be one at any time and only one MPM is loaded to the server.
2. Select mpm
MPM must be specified during configuration before compilation and then compiled into the server program. The compiler can optimize many functions only when it knows that a thread is used.
To use the specified mpm, runconfigure
Use Parameters--with-mpm=NAME
.NameIs the specified MPM name. We can only specify one of the MPM modules.
After compilation, you can use./httpd -l
To determine the selected MPM. This command lists all modules compiled into the server program, including MPM. Unix/Linux supports three types of MPM, prefork, worker, and event. The event model is a test version in apache2.2 and is not recommended in the production environment. apache2.4 and the event model is a stable version.
Availablehttpd -l
To determine the selected MPM. This command lists all modules compiled into the server program, including MPM.
[[Email protected] ~] # Httpd-lcompiled in modules: core. C # core prefork. C # compiled MPM module http_core.c mod_so.c # support for loading DSO dynamic modules
Prefork
Prefork is a multi-module MPM that implements a process model and pre-derived web server. Prefork is applicable to systems that do not have thread security libraries and need to avoid thread compatibility issues. It is the best MPM that requires each request to be independent from each other. Such a request has a problem and will not affect other requests.
The prefork mode uses multiple sub-processes. Each sub-process can maintain only one connection at a specified time. On most platforms, the prefork MPM is more efficient than the worker mpm, but the memory usage is much larger. Prefork works in Linux and is more advantageous than worker.
A separate control process (parent process) is responsible for producing child processes, which are used to listen for requests and respond. Apache always tries to keep a standby (spare) or idle sub-process to meet the upcoming requests. In this way, the client does not have to wait for the sub-process to generate before obtaining the service. In Unix systems, the parent process is usually run as root to bind port 80, while sub-processes produced by Apache are usually run by a low-privilege user. The user and group commands are used to configure low-privilege users for sub-processes. A user running a sub-process must have the permission to read the content of the sub-process, but must have as few permissions as possible for resources other than the service content.
After the HTTPd service is started, a master process is started, which listens to user requests and derives and destroys sub-processes. It does not process user requests. Once the request comes, it schedules its child processes to respond, and then continues to listen to other requests.
Consider how many idle processes are created at the startup? In order to quickly respond to user requests, several idle processes will be created. When the request comes, you can quickly respond. If there are too many requests, more processes will be derived to respond to the request. However, if at night there is not much traffic, we should reclaim unnecessary idle processes to avoid occupying resources.
Master process:
1. Bind a privileged port when starting the service
2. Distribute and recycle sub-Processes
3. Read the analysis master configuration file
4. Listen to user requests and dispatch sub-processes to respond
Configuration: edit the main configuration file/etc/httpd/CONF/httpd. conf.
# Prefork MPM <ifmodule prefork. c> # determine whether prefork exists. c. This module takes effect immediately, otherwise, startservers 8 is invalid # Number of worker processes started by default minspareservers 5 # minimum number of idle processes maintained maxspareservers 20 # maximum number of idle processes maintained serverlimit 256 # maximum number of active processes maintained maxclients 256 # maximum number of concurrent connections maxrequestsperchild 4000 # maximum number of requests that each sub-process can provide during the lifecycle </ifmodule>
The ifmodule command is used to determine whether a module exists. If a module exists, the parameter takes effect. If the module does not exist, the parameter does not take effect.
Startserver 5
Specifies the number of sub-processes created when the server starts. The default value of prefork is 5. To meet the needs of minspareservers settings, create a process. Wait for one second to create two more processes. Wait for another second to create four more processes ...... In this way, the number of Created processes is increased exponentially, up to 32 processes per second until the value set by minspareservers is met. This is the origin of prefork. This mode eliminates the need to generate new processes when requests arrive, thus reducing system overhead and increasing performance.
Minspareservers 5
Minimum number of idle sub-processes. The default value is 5. If the number of idle sub-processes is less than minspareservers, Apache will generate a new sub-process at one time per second. Do not set this parameter too large
Maxspareservers
Set the maximum number of idle processes. The default value is 10. If there are idle sub-processes that exceed the number of maxspareservers, the parent process will kill the redundant sub-processes. Do not set this parameter too large. If this parameter is set less than minspareservers, Apache automatically changes it to "minspareservers + 1 ". If the site load is large, consider increasing minspareservers and maxspareservers at the same time.
Maxclients 256
Limit the maximum number of concurrent threads of a single process connected to the client at the same time ). The default value is 256. Any request that exceeds the limit of maxclients will enter the waiting queue. Once a link is released, requests in the queue will be served. To increase this value, you must also increase serverlimit. Theoretically, the larger the value, the better the performance.
Serverlimit 256
By default, maxclient has a maximum of 256 threads. To set a larger value, you need to add serverlimit. 200000 is the maximum value of serverlimit. If you need more, you must compile Apache without re-compiling Apache. The premise is that it must be placed before other instructions.
Maxrequestsperchild
Each sub-process allows the maximum number of requests of the servo within its lifetime. By default, when 10000 reaches the limit of maxrequestsperchild, the child process ends.
If maxrequestsperchild is 0, the child process will never end. Setting maxrequestsperchild to a non-zero value has two advantages:
It can prevent (accidental) unlimited memory leakage and thus exhaust the memory.
A limited life cycle is provided for processes, which helps reduce the number of active processes when server load is reduced.
For keepalive links, only the first request is counted. In fact, it changes the behavior of each sub-process to limit the maximum number of links.
Let's look at the HTTPd process:
[[Email protected] ~] # Ps-Ef | grep httpdroot 1812 1 0 10:00? 00:00:01/usr/sbin/httpd # Master processapache 1814 1812 0? 00:00:00/usr/sbin/httpd # The following are Apache 1815 1812 0? 00:00:00/usr/sbin/httpd # Work processapache 1816 1812 0? 00:00:00/usr/sbin/httpdapache 1817 1812 0? 00:00:00/usr/sbin/httpdapache 1818 1812 0? 00:00:00/usr/sbin/httpdapache 1819 1812 0? 00:00:00/usr/sbin/httpdapache 1820 1812 0? 00:00:00/usr/sbin/httpdapache 1821 1812 0? 00:00:00/usr/sbin/httpd
Prefork is a process that processes a request. When starting httpd, if this mode is selected, a master process (Control Process) and startservers sub-process will be created first, the number of startservers is 8. because the process and the process have exclusive memory, a process crash will not affect other processes, so the stability of the prefork mode is better, but the process consumes more memory.
After the main process creates the startservers sub-process, to meet the minspareserver setting requirements, it will first create a process, wait one second, create two processes, and then wait one second, create four processes .... add a process created with a geometric number, and create up to 32 processes per second until the minspareserver settings are met (we can see that the minspareserver below is set to 5). This is the origin of prefork, in this way, you do not have to wait until a request arrives to create a new process, which increases the system response speed to increase performance.
Worker
Worker MPM uses multiple sub-processes, each of which has multiple threads. Each thread can maintain only one connection at a specified time. Generally, on a high-traffic HTTP server, worker MPM is a good choice, because the memory usage of worker MPM is much lower than that of prefork MPM. However, worker MPM also has defects. If a thread crashes, the whole process will "die" together with any of its threads, because the thread shares the memory space, therefore, a program must be recognized by the system as "Every thread is safe" during running ".
When the first user request arrives, the thread finds that the file needs to be copied and copies the file to the process through the kernel. Therefore, the second user requests the same file, for the second thread, this file already exists, because it shares the same space, so the speed is improved;
Disadvantages: threads are likely to generate resource contention, rather than full-state parallelism. Therefore, a process cannot start too many threads. Therefore, multiple processes can be started from the process to start multiple threads, however, every thread is an independent execution entity (the execution must be given to CPU resources)
Change Apache MPM to woker Mode
We use the httpd installed through yum. By default, it has compiled the prefork and worker mpm, so we can switch between them. For manual compilation, we can specify only one -- With-MPM = Name.
Edit the configuration file/etc/sysconfig/httpd:
# Vi/etc/sysconfig/HTTD # Remove the comments in the following line from httpd =/usr/sbin/httpd. Worker # restart the service when switching mode # service httpd restart
Default worker Configuration:
<Ifmodule worker. c> if this module is available, start startservers 4 # Number of sub-processes started maxclients 300 # maximum number of concurrent requests minsparethreads 25 # minimum number of Idle threads totalmaxsparethreads 75 # maximum number of Idle threads totalthreadsperchild 25 # the number of threads that each sub-process can generate. maxrequestsperchild 0 # the maximum number of requests that each sub-process can provide, 0 indicates no restriction
[[email protected] ~]# ps -ef | grep httpdroot 2395 1 0 15:36 ? 00:00:00 /usr/sbin/httpd.worker apache 2397 2395 0 15:36 ? 00:00:00 /usr/sbin/httpd.workerapache 2398 2395 0 15:36 ? 00:00:00 /usr/sbin/httpd.workerapache 2399 2395 0 15:36 ? 00:00:00 /usr/sbin/httpd.worker
The working principle of worker is that the main control process generates a "startservers" sub-process. Each sub-process contains a fixed number of threadsperchild threads, and each thread processes a request, the thread is shared memory space, so a thread crash will cause all threads in this process to crash, so its stability is not as good as prefork, but its memory usage is lower than prefork. Similarly, in order not to generate a thread when the request arrives, minsparethreads and maxsparethreads set the minimum and maximum number of Idle threads, while maxclients sets the total number of threads in all sub-processes. If the total number of threads in the existing sub-process cannot meet the load, the control process will derive a new sub-process.
The maximum default values of minsparethreads and maxsparethreads are 25 and 75, respectively. These two parameters have little impact on Apache performance and can be adjusted as needed.
Threadsperchild is one of the most important indicators that affect performance in worker. The total number of requests that can be processed simultaneously in worker mode is determined by the total number of startservers multiplied by the value of threadsperchild, and should be greater than or equal to maxclients.
Refer:
Http://blog.chinaunix.net/uid-20441206-id-3360689.html
Http://blog.chinaunix.net/uid-17238776-id-4353851.html
Http://www.cnblogs.com/ghj1976/archive/2011/01/11/1932764.html
This article is from the share your knowledge blog, please be sure to keep this source http://skypegnu1.blog.51cto.com/8991766/1532333