Apache Optimization: Modify the maximum number of concurrent connections

Source: Internet
Author: User

Apache is a cross-platform Web server, because of its simple and efficient, stable security features, is widely used in computer technology in all areas. Now, with its huge number of users, Apache has become the number one Web server for users.

However, in a real production environment, it is still not possible to use the default configuration of Apache directly to act as a server. After all, in order to use Apache server more fully and rationally, we should make some necessary adjustments to Apache's default configuration according to our actual needs. While optimizing the configuration for Apache, it is particularly important to modify the maximum number of concurrent connections for Apache.

Before we modify the maximum number of concurrent connections to Apache, we need to know some of the knowledge of Apache in advance.

As we all know, Apache is a cross-platform, modular design of the server. In order to meet the different needs of various platforms and different environments, and to achieve the best results in a specific platform or environment, Apache has also adopted a modular design for the basic functions of the Web server (port bindings, receiving requests, etc.). This Apache core module is called the Multi-Path processing module (multi-processing module, or MPM).

Apache provides several different mpm modules for different operating systems, such as: Mpm_beos , mpm_event , mpm_netware , mpmt_os2 , mpm_prefork , mpm_winnt , mpm_worker . If conditions permit, we can compile the specified MPM module into our own Apache based on actual requirements (Apache's The source code is open, allowing the user to compile themselves). However, if we do not have a choice at compile time, Apache will choose the corresponding MPM module according to the different operating system, which is the MPM module recommended by Apache for different platforms.

The default MPM module on different operating systems
Operating System MPM Module Description
Windows Mpm_winnt Don't introduce it:)
Unix/linux Mpm_prefork Don't introduce it:)
BeOS Mpm_beos A multimedia operating system developed by be company, the official version has stopped updating.
Netware Mpm_netware A network operating system launched by Novell.
Os/2 Mpmt_os2 An operating system that was originally developed by Microsoft and IBM and is now developed separately by IBM (Microsoft abandons OS/2, and instead develops windows)

The Mpm_event module can be regarded as a variant of the Mpm_worker module, but it has experimental properties and is generally not recommended for use.

Of course, Apache also provides a finished Apache on its official website that corresponds to the MPM modules that have been compiled for different operating systems. You can click here to go to the official Apache website to download.

In addition, if we want to know what kind of MPM module is used inside an Apache, we can enter it as a command line and Apache安装目录\bin then type the command to httpd -l see what MPM module is currently in use within Apache.

To view the compilation module using the Httpd-l command

Because BeOS, NetWare, OS/2 and other operating systems are not common in the usual development work, here we mainly explain the MPM modules on the windows and Unix/linux operating systems. In the Windows and Unix/linux operating systems, the MPM module mainly has mpm_winnt , mpm_prefork , mpm_worker three kinds.

Mpm_prefork Module

mpm_preforkThe module mainly applies to the Apache server of the Unix/linux platform, which mainly works as follows: When the Apache server is started, the mpm_prefork module will pre-create several sub-processes (the default is 5), when the client's request is received, the mpm_prefork module then forwards the request to the child process processing, And each child process can only be used to process a single request at the same time. If the current number of requests exceeds the number of pre-created child processes, the mpm_prefork module creates a new child process to handle the additional requests. Apache always tries to keep some spare or idle child processes in the process of meeting incoming requests. This way the client's request does not need to wait for the child process to be generated after receiving it.

Because in mpm_prefork a module, each request corresponds to a child process, it consumes more system resources than the other two modules. The advantage of the module, however mpm_prefork , is that each of its child processes handles the corresponding individual request independently, so that if one of the requests fails, it does not affect other requests. At the same time, mpm_prefork modules can be applied to third-party modules that do not have thread safety (e.g., non-thread-safe versions of PHP) and are easy to debug on platforms that do not support thread debugging. In addition, the mpm_prefork module has a higher mpm_prefork stability than the module.

Mpm_worker Module

mpm_workerThe module is also mainly used in the Apache server of the Unix/linux platform , which can be seen as an mpm_prefork improved version of the module. mpm_workermodules work in a similar way to mpm_prefork modules. However, because process-based processes (for example mpm_prefork ) consume more system resources than thread-based processing, because of processing the same request. Therefore, unlike mpm_prefork modules, the mpm_worker module causes each child process to create a fixed number of service threads and a listener thread, and let each service thread handle requests from the client, which listens to the access request and passes it to the service thread for processing and answering. Apache always tries to maintain a pool of standby or idle service threads. This way, the client does not have to wait for new threads or new processes to be established to be processed.

mpm_prefork mpm_worker modules can further reduce the overhead of system resources compared to modules. Plus it also uses multiple processes, each with multiple threads, so it adds a certain amount of stability to a completely thread-based approach.

MPM_WINNT Module

mpm_winntModules are MPM modules designed specifically for the Windows operating system. It creates only a single child process, and in this subprocess takes turns producing multiple threads to process the request.

Modifying the MPM module configuration

With a certain understanding of the Apache MPM module, we can modify the maximum number of concurrent connections in Apache for different MPM modules.

1. Enable MPM Module configuration file

Apace安装目录/conf/extraThere is a configuration file named in the directory httpd-mpm.conf . This file is mainly used for the configuration of the MPM module. However, by default, the MPM module configuration file for Apache is not enabled. Therefore, we need to httpd.conf enable the configuration file in the file as follows:

(Remove the comment symbol "#" in front of the line)

2. Modify the relevant configuration in the MPM module configuration file

After starting the MPM module configuration file, we can open the configuration file with a text editor, and we can see that there are many configuration nodes in the configuration file, <IfModule> as shown in:

The corresponding configuration will only take effect if Apache uses the corresponding MPM module.

At this point, we need to modify the parameter configuration under the corresponding node according to the MPM module used by the current Apache server <IfModule> . First, let's look at mpm_winnt the default configuration under the module:

#由于mpm_winnt模块只会创建1个子进程, the parameter setting for a single subprocess here is equivalent to the entire Apache parameter setting. <ifmodule mpm_winnt_module>threadsperchild      #推荐设置: Small website =1000 medium Web site =1000~2000 Large web site =2000~ 3500MaxRequestsPerChild    #推荐设置: Small =10000 Medium or large =20000~100000</ifmodule> 

The corresponding configuration parameters function as follows:

Threadsperchild
The maximum number of concurrent threads per child process.
Maxrequestsperchild
The total number of requests that each child process is allowed to process. If the accumulated number of requests exceeds this value, the child process ends (and then determines whether to create a new child process as needed), which is set to 0 to not limit the total number of requests (the child process never ends).

This parameter is recommended to be set to a value other than zero, which provides the following two benefits:

  1. You can run out of memory by preventing any memory leaks that may be present in the program from going on indefinitely.
  2. Gives the process a limited lifespan, which helps reduce the number of active processes when the server load is reduced.

Note: in the above parameters that relate to the number of statistics requests, only the first request is counted for keepalive connections.

Next, let's look at mpm_perfork mpm_worker the default configuration under modules and modules:

 #mpm_perfork模块 <ifmodule mpm_prefork_module>startservers 5  #推荐设置: small = default =20~50 large =50~100minspareservers 5  #推荐设置: Consistent with startservers Maxspareservers 1 0  #推荐设置: =20 =30~80 =80~120 maxclients,  #推荐设置: small =10000 Medium or large =10000~500000       
#mpm_worker模块 <ifmodule mpm_worker_module>startservers          #推荐设置: small = =3~5 big =5~10maxclients in default          # Recommended settings: Small =500 =500~1500 large =1500~3000minsparethreads      #推荐设置: small = default in =50~100 big =100~200maxsparethreads      # Recommended settings: small = default in =80~160 large =200~400 threadsperchild      #推荐设置: Small =10000 in or large =10000~50000(also, if maxclients/ Threadsperchild is greater than 16 and additional serverlimit parameters are required, serverlimit must be greater than or equal to Maxclients/threadsperchild value. ) </IfModule>     

The corresponding configuration parameters function as follows:

Startservers
number of child processes created when Apache was started.
Minspareservers
The number of the least-kid processes in the idle state.

The so-called idle child process refers to a child process that is not processing the request. 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. This parameter needs to be adjusted only on very busy machines. This value shouldn't be too large.

Maxspareservers
the maximum number of child processes in an idle state.

This parameter needs to be adjusted only on very busy machines . This value shouldn't be too large. If you set the value of the directive to be MinSpareServers smaller, Apache will automatically change it to MinSpareServers+1 .

MaxClients
The maximum number of requests allowed to connect at the same time.

Any MaxClients request exceeding the limit will enter the waiting queue until ListenBacklog the maximum of the instruction limit is reached.

For non-threaded MPM (that mpm_prefork is), the MaxClients maximum number of child processes that can be used to process client requests, the default value is 256. To increase this value, you must increase it at the same time ServerLimit .

For a threaded or mixed mpm (that is mpm_beos , or mpm_worker ), the MaxClients maximum number of threads that can be used to process client requests. The default value for the thread type mpm_beos is 50. The default value for the hybrid MPM is the ServerLimit result of a () multiplied by ( ThreadsPerChild ). So MaxClients you have to increase the value at the same time to increase to more than 16 processes to provide ServerLimit .

Minsparethreads
The minimum number of threads in an idle state.

Different MPM handles this instruction differently:

mpm_workerThe 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. mpm_netwareThe default value is 10. Since this MPM only runs a single sub-process, this MPM will of course monitor the number of idle threads based on the entire server . The default value is mpm_beos mpmt_os2 1, and the mpm_netware mpm_beos mpmt_os2 default value is 5.

Maxsparethreads
the maximum number of threads in an idle state.

Different MPM handles this instruction differently:

mpm_workerThe 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. mpm_netwareThe default value is 100. Since this MPM only runs a single sub-process, this MPM will of course monitor the number of idle threads based on the entire server. The default value is 10, which is the mpm_beos mpmt_os2 same as the working mode mpm_netware mpm_beos mpmt_os2 .

Note : ServerLimit represents the maximum number of processes allowed to be created by Apache. It is important to note that Apache has a hard limit (for modules) inside the compile time ServerLimit 20000 mpm_prefork ServerLimit 200000 . You can't go beyond that limit.
Be especially careful when using this command. If it is ServerLimit set to a higher value than is actually required by many values, there will be too much shared memory being allocated. If ServerLimit set to MaxClients exceed the system's processing power, Apache may not boot or the system will become unstable.

Note : When configuring the relevant parameters, first ensure that the server has sufficient hardware performance (for example: CPU, memory, etc.). If you notice that the server's memory footprint increases as the server 's running time increases, and perhaps a memory leak occurs in the program, adjust the value of the parameter downward MaxRequestsPerChild to reduce the impact of the memory leak, and then find out where the problem is in the program as quickly as possible.

Apache Optimization: Modify the maximum number of concurrent connections

Related Article

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.