Apache2 in linux

Source: Internet
Author: User

Apache2 in linux
Working Principle and configuration of prefork
If "-- with-mpm" is not used to explicitly specify an MPM, prefork is the default MPM on Unix platforms. It adopts the pre-school child process mode, which is also used in Apache 1.3. Prefork itself does not use threads. Version 2.0 uses it to maintain compatibility with version 1.3. On the other hand, prefork uses a separate sub-process to process different requests, processes are independent from each other, which makes them one of the most stable MPM.

If prefork is used, after make compilation and make install, "httpd-l" is used to determine the currently used MPM. c (if you see worker. c indicates that worker MPM is used, and so on ). Check the default httpd. conf configuration file, which contains the following configuration sections:

<IfModule prefork. c>
StartServers 5
MinSpareServers 5
MaxSpareServers 10
MaxClients 150
MaxRequestsPerChild 0
</IfModule>

The working principle of prefork is that after the control process initially creates a "StartServers" sub-process, it creates a process to meet the needs of the MinSpareServers settings, waits for one second, creates two more, and then waits for one second, create four more ...... 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.

MaxSpareServers sets the maximum number of idle processes. If the number of idle processes is greater than this value, Apache automatically kill unnecessary processes. This value should not be too large, but if it is smaller than MinSpareServers, Apache will automatically adjust it to MinSpareServers + 1. If the site load is large, consider increasing MinSpareServers and MaxSpareServers at the same time.

MaxRequestsPerChild sets the number of requests that each sub-process can process. Each sub-process is automatically destroyed after processing "MaxRequestsPerChild" requests. 0 means that the sub-process will never be destroyed. Although the default value is 0, each sub-process can process more requests, setting it to a non-zero value also has two important advantages:

◆ Prevents accidental Memory leakage;

◆ When the server load drops, the number of sub-processes is automatically reduced.

Therefore, you can adjust this value based on the server load. I personally think about 10000 is more appropriate.

MaxClients is the most important of these commands. It sets the request that Apache can process at the same time and is the parameter that has the greatest impact on Apache performance. The default value of 150 is far from enough. If the total number of requests has reached this value (you can confirm it through ps-ef | grep http | wc-l), the subsequent requests will be queued, until a processed request is completed. This is the main reason why there are still a lot of system resources and HTTP access is slow. The system administrator can dynamically adjust the value based on hardware configuration and load conditions. In theory, the larger the value, the more requests can be processed, but the default limit of Apache cannot be greater than 256. If you set this value to greater than 256, Apache cannot start. In fact, 256 is not enough for sites with a slightly higher load. In Apache 1.3, this is a hard limitation. To increase this value, you must search for 256 in src/include/httpd. h under the source code tree manually modified before "configure", and you will find the line "# define HARD_SERVER_LIMIT 256. Change 256 to the value to be increased (such as 4000), and then re-compile Apache. Added the ServerLimit command in Apache 2.0 to increase MaxClients without re-compiling Apache. The following is the prefork configuration segment:

<IfModule prefork. c>
StartServers 10
MinSpareServers 10
MaxSpareServers 15
ServerLimit 2000
MaxClients 1000
MaxRequestsPerChild 10000
</IfModule>

In the preceding configuration, the maximum ServerLimit value is 2000, which is sufficient for most sites. If you must increase the value, modify the following two lines in server/mpm/prefork. c under the source code tree:

# Define DEFAULT_SERVER_LIMIT 256
# Define MAX_SERVER_LIMIT 2000

Working Principle and configuration of worker

Compared with prefork, worker is a brand new MPM that supports multi-thread and multi-process hybrid models in version 2.0. Because threads are used for processing, a relatively large number of requests can be processed, and the overhead of system resources is smaller than that of process-based servers. However, worker also uses multiple processes, and each process generates multiple threads to achieve stability based on the process server. This kind of MPM will be the trend of Apache 2.0.

After configure-with-mpm = worker, make and install it. The following configuration segments are available in the httpd. conf generated by default:

<IfModule worker. c>
StartServers 2
MaxClients 150
MinSpareThreads 25
MaxSpareThreads 75
ThreadsPerChild 25
MaxRequestsPerChild 0
</IfModule>

The working principle of worker is that the main control process generates a "StartServers" subprocess. Each subprocess contains a fixed number of ThreadsPerChild threads, and each thread processes Requests independently. 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 75 and 250, respectively. These two parameters have little impact on Apache performance and can be adjusted as needed.

ThreadsPerChild is the most performance-related command in worker MPM. The maximum default value of ThreadsPerChild is 64. If the load is large, 64 is not enough. The ThreadLimit command must be explicitly used. The maximum default value is 20000. The preceding two values are located in the following two lines in the source code tree server/mpm/worker. c:

# Define DEFAULT_THREAD_LIMIT 64
# Define MAX_THREAD_LIMIT 20000

These two rows correspond to the number of restrictions of ThreadsPerChild and ThreadLimit. It is best to change 64 to the desired value before configure. Note: Do not set these two values too high to exceed the processing capability of the system, so that the system is unstable because Apache does not start.

In Worker mode, the total number of requests that can be processed simultaneously is determined by the total number of sub-processes multiplied by the value of ThreadsPerChild. The value must be greater than or equal to MaxClients. If the load is large and the number of existing sub-processes cannot meet the requirements, the control process will derive a new sub-process. By default, the maximum number of sub-processes is 16. When you increase the number, you must explicitly declare ServerLimit (the maximum value is 20000 ). These two values are located in the following two lines in the source code tree server/mpm/worker. c:

# Define DEFAULT_SERVER_LIMIT 16
# Define MAX_SERVER_LIMIT 20000

Note that if ServerLimit is explicitly declared, the value multiplied by ThreadsPerChild must be greater than or equal to MaxClients, and MaxClients must be an integer multiple of ThreadsPerChild, otherwise, Apache automatically adjusts to a value (which may be an unexpected value ). The following is the worker configuration section:

<IfModule worker. c>
StartServers 3
MaxClients 2000
ServerLimit 25
MinSpareThreads 50
Maxsparethread S 200
ThreadLimit 200
ThreadsPerChild 100
MaxRequestsPerChild 0
</IfModule>

Through the above description, we can understand the working principles of the two important MPM, prefork and worker in Apache 2.0, and can configure Apache-related core parameters according to the actual situation, to achieve maximum performance and stability.
 
How to switch the prefork and worker modes of apache2
 
Problem
On Prima/Plesk/Virtuozzo, apache uses the rpm package of the system's built-in httpd-2.0.5x
In redhat linux, The prefork mode is used by default, instead of the worker mode.
How do I switch between them?
Solution
The Apache HTTP server is designed as a powerful and flexible server that can work on multiple platforms and in different environments.
Different platforms and environments often have different requirements, or adopt different methods to achieve the same effect.
Apache is well adapted to a large number of different environments with its modular design.
This design allows the website administrator to load different modules during compilation and runtime to determine different additional functions of the server.
Apache2.0 extends this modular design to the basic functions of web servers.
This version has the selection of multi-path processing module (MPM) to process network port binding,
Accept requests and assign sub-processes to process these requests.
For example, for better scalability, you can choose a thread-based MPM like worker or event,
However, we need better stability and compatibility to adapt to the use of prefork for some old software.
On Redhat Linux's major version as4, apache is a httpd-2.0.5x,
The default value is the prefork mode, mainly because of stability.
To switch to worker mode, You need to log on to linux and perform the following operations:
Go to the/usr/sbin directory
Cd/usr/sbin
Rename the current prefork Startup File
Mv httpd. prefork
Rename the Startup File in worker Mode
Mv httpd. worker httpd
Modify the configuration file vi/etc/httpd/conf/httpd. conf
Find the following section, and modify the parameters such as load:
  
StartServers 5
ThreadLimit 200
ServerLimit 50
MaxClients 2000
MinSpareThreads 25
Maxsparethread S 200
ThreadsPerChild 100
MaxRequestsPerChild 50
  
Restart the service
/Etc/init. d/httpd restart
Start apache2 in worker mode.

 

This article is from "technology source interest"

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.