Apache as the most widely used Web server and the most stable open source server software, its working mode has many, there are two main modes: Prefork mode and worker mode one, two modes Prefork mode: Prefork is the default (default) MPM on UNIX platforms, using multiple child processes, with only one thread per child process. Each process can only maintain one connection at a certain time, and is efficient, but has a large memory footprint. This multi-path processing module (MPM) implements a non-threaded, pre-derived Web server that works in a similar way to Apache 1.3. It is suitable for systems that do not have a thread-safe library and need to avoid threading compatibility issues. It is the best mpm to require each request to be independent of each other, so that if a request has a problem it will not affect the other request. Worker mode: Workers use multiple child processes, each with multiple threads, each with a certain amount of time to maintain only one connection and a smaller memory footprint for high-traffic HTTP servers. The downside is that if a thread crashes, the entire process will "die" with any of its threads, so ensure that a program must be recognized by the system as "every thread is safe" at run time. This multi-path processing module (MPM) enables a network server to support mixed multithreaded multi-process. Because a thread is used to process requests, a large amount of requests can be processed, while the overhead of system resources is less than the process-based MPM. But it also uses multiple processes, each with multiple threads, to get the stability of the process-based MPM. Second, the Apache mode of viewing and installation 1, often see the current mode if Apache is already installed, we can use the "httpd-l" command to view the current mode. If PREFORK.C is found, it is currently working in Prefork mode, and WORKER.C is working in worker mode. If Apache is not yet installed, we can join the--with-pem= (prefork|worker) option when compiling to determine what mode is enabled. 2, switch mode A. The current prefork mode startup file is renamed MV HTTPd Httpd.preforkb. Rename the startup file for worker mode to MV Httpd.worker HTTPDC. Modify Apache configuration file vi/usr/local/apache2/conf/extra/httpd-mpm.conf Find the following paragraph inside, you can modify the load parameters such as: Copy code <ifmodule mpm_worker_ Module>startservers 2MaxClients 150MinSpareTHreads 25MaxSpareThreads 75ThreadsPerChild 25MaxRequestsPerChild 0</ifmodule> copy code D. Restart Service/usr/local/apache2/bin/apachectl Restart is in stability and security considerations, it is not recommended to replace the apache2 operation mode, using the system default Prefork. In addition, many PHP modules do not work in worker mode, for example, Redhat Linux comes with PHP that does not support thread safety. Therefore, it is best not to switch to working mode. Iii. comparison of prefork and worker patterns Prefork mode uses multiple child processes, with only one thread per child process. Each process can only maintain one connection at a certain time. On most platforms, the Prefork MPM is more efficient than the worker mpm, but the memory usage is much larger. Prefork's wireless path design in some cases will be more advantageous than a worker: it can use third-party modules that do not handle thread safety, and it is easier to debug for platforms that have difficulty debugging threads. Worker mode uses multiple child processes, each of which has multiple threads. Each thread can only maintain one connection at a certain time. In general, worker MPM is a good choice on a high-traffic HTTP server because the worker MPM uses much less memory than Prefork MPM. But the worker mpm is also in the imperfect place, and if a thread crashes, the entire process will "die" along with all of its threads. Because threads share memory space, a program must be recognized by the system as "every thread is safe" at run time. Overall, the prefork is slightly faster than the worker, but it requires slightly more CPU and memory resources than Woker. Iv. prefork Mode configuration detailed copy code <ifmodule mpm_prefork_module>serverlimit 256StartServers 5MinSpareServers 5MaxSpareServers 10MaxClients 256MaxRequestsPerChild 0</ifmodule> Copy code serverlimit The default maxclient maximum is 256 threads, If you want to set a larger value, add the Serverlimit parameter. 20000 is the maximum value of the Serverlimit parameter. If you need to be larger, you must compile the ApachE, previously there was no need to recompile Apache. Effective premise: Must be placed in front of other directives startservers specifies the number of child processes that are established when the server is started, and Prefork defaults to 5. minspareservers Specifies the minimum number of idle child processes, which defaults to 5. If the number of currently idle child processes is less than minspareservers, then Apache will produce a new subprocess at a maximum speed of one second. Do not set this parameter too large. Maxspareservers sets the maximum number of idle child processes, which defaults to 10. If there are currently more than maxspareservers of idle child processes, the parent process kills the extra child processes. Do not set this parameter too large. If you set the value of this directive to be smaller than Minspareservers, Apache will automatically modify it to "minspareservers+1". MaxClients limits the number of client maximum access requests at the same time (number of concurrent threads per process), which defaults to 256. Any request exceeding the maxclients limit will enter the waiting queue, and once a link is freed, the request in the queue will be serviced. To increase this value, you must increase the serverlimit at the same time. Maxrequestsperchild the maximum number of requests that each child process allows for a servo during its lifetime, by default 10000. When the Maxrequestsperchild limit is reached, the child process ends. If Maxrequestsperchild is "0", the child process will never end. Setting Maxrequestsperchild to a value other than 0 has two benefits: 1. can prevent (accidental) memory leaks from running indefinitely, thus exhausting memory. 2. Give the process a limited lifespan, thus helping to reduce the number of active processes when the server load is reduced. Five, the worker mode configuration detailed copy code <ifmodule mpm_worker_module>startservers 2MaxClients 150MinSpareThreads 25MaxSpareThreads 75ThreadsPerChild 25MaxRequestsPerChild 0</ifmodule> Copy Code startservers the number of child processes established at server startup, the default value is "3". MaxClients maximum number of Access requests (maximum number of threads) that allow simultaneous servo. Any requests exceeding the maxclients limit will enter the waiting queue. The default value is "Serverlimit", multiplied by the result of (Threadsperchild)。 Therefore, to increase maxclients, you must increase the value of serverlimit at the same time. Minsparethreads the minimum number of idle threads, the default value is "75". This MPM will monitor the number of idle threads based on the entire server. If the total number of idle threads in the server is too small, the child process will produce a new idle thread. Maxsparethreads sets the maximum number of idle threads. The default value is "250". This MPM will monitor the number of idle threads based on the entire server. If the total number of idle threads in the server is too many, the child process kills the extra idle threads. The value range of the maxsparethreads is limited. Apache will automatically fix the values you set as follows: The worker requires that it is greater than or equal to Minsparethreads plus threadsperchild. Threadsperchild the number of resident execution threads established per child process. The default value is 25. When a child process establishes these threads at startup, no new threads are established. Maxrequestworkers = maxclients Maximum number of connections Maxrequestsperchild sets the maximum number of requests per child process to allow the servo during its lifetime. When the Maxrequestsperchild limit is reached, the child process will end. If Maxrequestsperchild is "0", the child process will never end. Setting Maxrequestsperchild to a value other than 0 has two benefits: 1. can prevent (accidental) memory leaks from running indefinitely, thus exhausting memory. 2. Give the process a limited lifespan, thus helping to reduce the number of active processes when the server load is reduced. Note for keepalive links, only the first request is counted. In fact, it changes the behavior of each child process to limit the maximum number of links. Six, summed up the previous Apache mainstream model for Prefork, now the worker mode has begun to rise, the difference is that the worker mode can handle high traffic, but the security is not very good, prefork mode security is better, but performance will be nearly, You can choose different mode according to your own server's category, better use Apache.
Two working modes commonly used by Apache Prefork and worker