Apache Two modes of operation: Prefork, worker

Source: Internet
Author: User

Objective

1 · Recent articles are all about Apache security and optimization, these are important to the server, mastering these not only improves the security of the server, but also makes the performance of the server greatly improved. This will allow the customer to have a good sense of experience.
2 · This article is still about Apache today. However, it is speaking of Apache optimization in depth. Apache's default configuration guarantees basic operating services, but in actual production environments, it is sometimes necessary to maximize server performance.

Two modes of operation of Apache: (Prefork, worker)

Many friends know that Apache is now the most widely used Web server is one of the most stable open source software, there are many modes of work, the main use of the two working modes are: prefork mode, worker mode. The two modes are described below.

Prefork Working mode:

1 · First we need to know what is the prefork mode of operation, the second need to know the advantages and disadvantages of this mode of work, and finally we need to configure the Prefork mode of operation. This way in the production environment will be conducive to our flexible application according to their own needs!
2 Prefork Working mode implements a non-threaded, pre-distributed Web server, which is the best mpm (multi-processing module) that requires each request to be independent of each other. Each process only processes a single request at a certain time.
The 3 prefork mode is responsible for generating child processes by a separate control process (the parent process), which is used to listen for requests and respond, so there will always be idle child processes in response to new requests, which can speed up response times.

Advantages and disadvantages of prefork:

Advantages:
1 · With a strong self-regulation ability, high efficiency, only a small number of configuration instructions to adjust;
2 · The most important thing is that the value of the maxclients can be a large enough value to handle the potential request peaks;
3 · For systems that do not have a thread-safe library and need to avoid threading compatibility issues;
4 · It is easier to debug some of the difficult platforms for thread debugging.
Disadvantages:
1 · The value of maxclients should not be too large to avoid the need to use more memory than the size of physical memory;
2 · Because it will often spare idle child processes in response to new requests, it will consume more memory;

Prefork Configuration method:

1 · First you need to know if your Apache has specified a working mode
Cd/usr/local/httpd/bin
./httpd-l (Execute this command in the bin directory to see the current mode of operation)
2 · If you do not specify a mode of operation, you need to add parameters to the Apache configuration
./configure--with-mpm=prefork
3 · Open the MPM (multi-path module) path in the master configuration file:
Vim/etc/httpd.conf
Include conf/extra/httpd-mpm.conf (remove annotations)
4 • Modify Prefork working module parameters
Vim/usr/local/httpd/conf/extra/httpd-mpm.conf
Modify the value slightly adjusted, the actual situation needs according to their own needs, the content is as follows:
<ifmodule mpm_prefork_module> (prefork module)
Startservers 10
Minspareservers 10
Maxspareservers 30
Maxrequestworkers 1000
Maxconnectionsperchild 5000
</IfModule>

Prefork Configuration parameter table:
Parameters Description
Startservers Number of processes created at startup
Minspareservers Least idle Process
Maxspareservers Most idle processes
Maxrequestworkers Create up to how many child processes to process requests
Maxconnectionsperchild The maximum number of requests processed per process, if the number of requests is reached, the process is destroyed, and if set 0, the child process never ends
Worker Working mode:

1 · Worker mode enables a WEB server to support mixed multithreaded multi-process.
2 · Worker module the number of threads each process can have is fixed, and the server will increase or decrease the number of processes depending on the load situation
3 • A separate control process (parent process) is built on the load subprocess, and each child process can establish a certain number of service localities and a listener thread, listen to the access request and pass it to the service thread for processing and answering

Advantages and disadvantages of Worker:

Advantages:
1 · Many requests can be processed because the thread is used to process the request;
2 · The cost of system resources is less than the process-based MPM;
3 · It uses multi-process, each process has multiple threads to obtain stability based on the process of MPM;
4 • Threads are lighter than processes because the thread is sharing the memory space of the parent process, so memory consumption decreases
Disadvantages:
1 · If there is a problem with one thread, it will cause a problem with the thread under the same process.
2 · Due to the use of multi-process multithreading, it is necessary to take into account the security of the thread, when using keep-alive long connection, a thread will always be occupied, even if there is no request in the middle, it will need to wait until the timeout is released (the problem is also present in Prefork mode).

How the Worker is configured:

1 · First you need to know if your Apache has specified a working mode
Cd/usr/local/httpd/bin
./httpd-l (Execute this command in the bin directory to see the current mode of operation)
2 · If you do not specify a mode of operation, you need to add parameters to the Apache configuration
./configure--with-mpm=worker
3 · Open the MPM (multi-path module) path in the master configuration file:
Vim/etc/httpd.conf
Include conf/extra/httpd-mpm.conf (remove annotations)
4 • Modify Worker work module parameters
Vim/usr/local/httpd/conf/extra/httpd-mpm.conf
Modify the value slightly adjusted, the actual situation needs according to their own needs, the content is as follows:
<ifmodule mpm_worker_module>
Startservers 2
Minsparethreads 25
Maxsparethreads 75
Threadsperchild 25
Maxrequestworkers 150
Maxconnectionsperchild 0
</IfModule>

Worker Configuration parameter table:
Parameters Description
Startservers Number of child processes established at server startup
Minsparethreads Least idle Process
Maxsparethreads Most idle processes
Threadsperchild Number of resident execution threads per child process
Maxconnectionsperchild The maximum number of requests processed per process, if the number of requests is reached, the process is destroyed, and if set 0, the child process never ends
Maxrequestworkers Limit the number of requests for maximum client access for the server at the same time
Summarize:

1 · Prefork mode uses multiple child processes, with only one thread per child process. Each process can only maintain one link at a certain time. But it has more memory footprint, but it's also easier to debug!
2 · Worker mode uses multiple child processes, each of which has multiple threads. Each thread can only maintain one link at a certain time. Its memory usage is much lower than prefork.

Induction:

Overall, the prefork is slightly faster than the worker, but it requires more CPU and memory resources than the worker! The Prefork method is more recommended in practical applications.

Apache Two modes of operation: Prefork, worker

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.