Apache2.2prefork mode optimization

Source: Internet
Author: User
Apache2.2prefork mode optimization apache2.2 optimization configuration core Key: (1) in prefork mode (not applicable in other modes), apache needs to optimize the main parameters: ServerLimit3000StartServers750MinSpareServers5MaxSpareServers100MaxCli... apache2.2 prefork mode the key to optimizing apache2.2 configuration is as follows: (1) in prefork mode (not applicable in other modes). The main parameters for apache optimization are as follows: serverLimit 3000 StartServers 750 MinSpareServers 5 MaxSpareServers 100 MaxClients 3000 MaxRequestsPerChild 10000 first let's take a look at the meaning of each apache parameter (quotes reference the description in the official documentation): www.2cto.com (1) S The maximum number of simultaneous response requests from the erverLimit and MaxClients servers is the maximum number of concurrent responses to apache configured currently. it corresponds to the number of apache processes. The two parameters are modified at the same time, maxClients must not be greater than the ServerLimit parameter. The size of ServerLimit depends on the resources of your system. by default, each apache process occupies 2 MB of memory. you can use this formula to calculate the maximum memory size: * 80%/2 M = ServerLimit www.2cto.com (2) the default number of processes started when StartServers 750 is set to 5, because apache automatically starts a new process to increase the number of processes responding to the service. this value is fine without any adjustment, the number of processes that meet the service is increased from 5 by default, but the system will be stuck at startup. One advantage of a small startup parameter is that it increases the stress of backend tomcat after passing, rather than increasing the pressure at once. You can adjust this to the maximum number of concurrent connections of the current service, which can be obtained by monitoring the number of apache processes: ps-ef | grep httpd | wc-l. You do not need to tune it too much. Otherwise, it is unnecessary to add a connection between apache and tomcat through jk. Note: After the apache process establishes a connection with tomcat, the connection will not be released and will remain connected until timeout. if there is no timeout time, the connection will be established permanently. Timeout settings are described in later jk configurations. So do not start too many apache processes at a time, just start enough processes. For other traffic increases, apache automatically adjusts the number of processes until the range specified by the MaxClients parameter. (3) MinSpareServers 5. the MinSpareServers command sets the minimum number of idle sub-processes. Idle sub-processes are sub-processes that are not processing requests. If the number of idle sub-processes is less than MinSpareServers, Apache will generate a new sub-process at an exponential increase of one second, two second, and four third second. (4) The MaxSpareServers command sets the maximum number of idle sub-processes. Idle sub-processes are sub-processes that are not processing requests. If there are idle sub-processes that exceed the number of MaxSpareServers, the parent process will kill the redundant sub-processes. You can adjust these two parameters, but the values of these two parameters cannot be too large. otherwise, too many apache processes will lead to many tomcat processes. On the official website, there is a saying about these two parameters: "Setting this parameter too large is usually a bad idea ." MaxSpareServers is set to 2800 on a high-stress (concurrent access 200) server. The advantage of setting this value is that there will not be too many idle processes consuming resources while reducing the connection ports between apache and tomcat. When idle apache processes are disabled, jk connections are released and tomcat connections are released to reduce system resource consumption. (5) MaxRequestsPerChild 10000 "MaxRequestsPerChild command sets the maximum number of requests that each sub-process allows for the servo over its lifetime. When the limit of MaxRequestsPerChild is reached, the sub-process will end. 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. Note that for KeepAlive connections, only the first request is counted. In fact, it changes the behavior of each sub-process to limit the maximum number of links. "That is to say, in fact, the maximum number of connections of sub-processes is equal to MaxRequestsPerChild * MaxKeepAliveRequests. Therefore, after enabling KeepAlive, you must set both MaxRequestsPerChild and MaxRequestsPerChild to ensure that each apache process is disabled after a certain number, restart a new sub-process to avoid memory leakage and resource usage caused by exceptions of the apache process. (6) Keep-Alive default value: for a request sent by ON, only one request is included in MaxRequestsPerChild, regardless of the number of requests sent by the connection. (7) MaxKeepAliveRequests default: 100 "a established Keep-Alive connection allows the number of requests sent. Once a connection is established, either the number reaches the disconnected level or the KeepAliveTimeout time reaches the disconnected level. The MaxKeepAliveRequests command limits the number of requests allowed by each connection when KeepAlive is enabled. If you set this value to "0", the number of requests is not limited. We recommend that you set this value to a relatively large value to ensure optimal server performance. "To set this number, you must consider the number of requests sent by the same user to access your service within a period of time. It should be considered in combination with the KeepAliveTimeout parameter. For example, if the user needs to request 10 files consecutively at intervals not greater than the KeepAliveTimeout time, this parameter should be set to 10. if the user continuously requests access for a continuous period of time, the value must be set to more. Otherwise, the connection will be re-established for download. Once the user has made 10 consecutive requests, and this user will certainly not request again within 5 seconds after completing these requests, even after a long time, therefore, the KeepAliveTimeout time can be set very short to disconnect such users as soon as possible and allow resources to be used by other users. (8) KeepAliveTimeout default: 5 "in a established Keep-Alive connection, when the number of MaxKeepAliveRequests is not full, wait for the next request time. "If a request arrives, apache waits for the timeout time of the IO response to take effect. if the timeout time does not wait for the response, the connection is disconnected. If no request arrives within the KeepAliveTimeout time, the connection is disconnected. For specific settings, see use the MaxKeepAliveRequests parameter. At the same time, this parameter is also affected by the TimeOut parameter. in a successful connection, the connection will be closed if the response is not received within the TimeOut time. (9) TimeOut default: 300 "TimeOut command is used to set the length of time for Apache to wait for the following three events: 1. The total time it takes to receive a GET request. 2. when a POST or PUT request is sent, the time between two TCP packets is accepted. 3. the time between two ACK packets in TCP packet transmission during response. We plan to gradually make them easier to configure in the course of development. The default value of the timer before version 1.2 is 1200, but now it is set to 300, but it is still sufficient in most cases. The reason for not setting the default value smaller is that there is still a problem in the code: sometimes the timer is not reset after a packet is sent ." Performance configuration parameters that affect Apache: performance configuration parameters that affect Apache: Timeout 300 specifies the time in seconds for site response. If the data is not received or sent out after this period, the connection is closed. This parameter specifies the maximum waiting time (in seconds) before Apache receives a request or sends the requested content. If Apache does not process the request and releases the connection. The default value of this parameter is 120. we recommend that you set this parameter to 60. for websites with large traffic volumes, you can set this parameter to 30. KeepAlive On | Off enables this option to allow persistent connections so that multiple requests can be initiated for each connection. Avoid establishing a connection with the server every time a file is requested. That is, to control whether Apache allows multiple requests in a connection, which is enabled by default. However, for most forum sites, it is usually set to off to disable this support. MaxKeepAliveRequests 100 indicates the number of requests that can be submitted for each connection. if it is set to 0, the number is unlimited. the default value is 100 .. KeepAliveTimeout 15 if the time between two consecutive requests exceeds 15 seconds, the connection is interrupted. This approach can maximize the use of limited resources, which is also one of the reasons why Apache performance is so superior. MinSpareServers 5 MaxSpareServers 20 Apache automatically adjusts the number of idle sub-processes according to the weight of the load at runtime. The number of httpd processes that provide browsing services varies with the number of connections. Therefore, several idle httpd processes need to wait for new connection requests at any time. If the number of idle processes is less than 5 (default value), it indicates that there are too few idle processes and you need to increase them to 5. if there are more than 20 (default value), there are too many idle processes, reduce it to 20. When MaxClients 150 has too many connections at the same time, the system access performance will be reduced. this parameter can be set to limit the maximum number of connections at the same time. the default value is 150, which sets the request that Apache can process at the same time, maxClients and MaxRequestPerchild are the parameters that have the greatest impact on Apache performance. MaxClients specifies the maximum number of clients allowed to connect to Apache within the same time period. if MaxClients connections are exceeded, the client will get a "busy server" error page. We can see that MaxClients is set to 15 by default, which is far from enough for some medium-sized and large sites! You may need to allow 512 client connections at the same time to meet application requirements. well, let's change MaxClients to 512 and save httpd. conf and quit, restart Apache. sorry, you saw some error prompts during the restart process, and the restart of Apache failed. The error message tells you that the maximum value of MaxClients can be set to 256. we believe you will be disappointed. But don't be frustrated. Apache, as a world-class Web Server, will never be so thin! By default, MaxClients can only be set to an integer of no more than 256. However, if you want to customize it, use the ServerLimit parameter, in short, ServerLimit is like a bucket, while MaxClients is like water. you can replace a larger bucket (set ServerLimit to a larger bucket) to accommodate more water (MaxClients ), note that the value of MaxClients cannot be greater than that of ServerLimit! Next let's take a look at the MaxRequestPerChild parameter, which specifies how many threads can work in a connection process at the same time. This explanation may be too professional, so you only need to think about "multi-point simultaneous download" in "Network ant" and "Internet Express Flash get". This parameter actually limits the maximum number of "points ". The default value is 0, that is, no limit. However, if you set this value too small, it will cause access problems. if there is no special need or the traffic pressure is not great, you can keep the default value, if the traffic volume is high, we recommend that you set it to 2048. Optimization suggestion: # upgrade Apache to the latest version. The new version usually includes performance improvement and security updates. # Setting "HostNameLookups off" in httpd. conf can avoid reverse queries for the DNS domain name of each visitor. # For busy websites, set "MaxClients 230" or higher in httpd. conf. This setting allows more httpd processes to respond to requests at the same time, and avoids processor queuing. # Use another server to process image files. # Web pages and CGI pages are protected by browser buffering. For specific articles, refer to this site: use mod_gzip to accelerate Zope and Apache # to keep your Apache slim, compile the modules that are only needed, and modify the src/Configuration file before compilation, comment out unnecessary modules. # If no traffic logs are required, use httpd. the TransferLog in conf points to/dev/null/# unless you are sure to use it. the htaccess file is used to control the permissions of some directories. otherwise, "AllowOverride None" is set to avoid Apache searching in each directory. the hard work of htaccess files. # Do not run unwanted background processes. # Do not write pages or log files to network disks, such as NFS. # Do not run Apache (httpd) in inetd mode. # Do not run X Windows on your Web server. use Ctrl-Alt-Backspace to disable X. # Avoid using SSI tags. # In CGI scripts: * file I/O: The smaller the number of opened files, the better. * Shell command: use the full path to call the shell command. * If your website is mainly driven by CGI, use mod_perl. * In your Web page Directory, do not set the number of files to more than 1000. the more files, the more time they spend locating them. # The fewer images on the Web server, the better. ensure that each image runs through the image compressors. # Perform stress tests on your website. we recommend that you use the AB command that comes with Apache to test the stress. # For the best performance, it is best to unplug the network cable so that your Web server is safe and the load is immediately reduced to 0. The maximum number of connections to Apache is 256 by default. Modify the maximum number of connections of apache as follows: Step 1: First modify the./apache/conf/httpd. conf file. # Vi httpd. conf remove the # above "# Include conf/extra/httpd-mpm.conf. Save. Step 2: modify the./apache/conf/extra/httpd-mpm.conf file. # Find the vi httpd-mpm.conf This line is original: StartServers 5 MinSpareServers 5 MaxSpareServers 10 MaxClients 150 MaxRequestsPerChild 0 After modification The maximum number of connections of Apache. the default value is 256. Modify the maximum number of connections of apache as follows: Step 1: First modify the./apache/conf/httpd. conf file. # Vi httpd. conf remove the # above "# Include conf/extra/httpd-mpm.conf. Save. Step 2: modify the./apache/conf/extra/httpd-mpm.conf file. # Find the vi httpd-mpm.conf This line is original: StartServers 5 MinSpareServers 5 MaxSpareServers 10 MaxClients 150 MaxRequestsPerChild 0 After modification StartServers 5 MinSpareServers 5 MaxSpareServers 10 ServerLimit 1500 MaxClients 1000 MaxRequestsPerChild 0 Note: 1. you must add ServerLimit before MaxClients, and the value must be greater than the value of MaxClient; 2. restart apache. only/bin/apachectl restart is invalid, you must first stop apachectl and then apachectl start to understand and tune the concurrency control parameter prefork in apache. an apache has a linux-based concurrency that is not very high, about 3 K, common servers may encounter problems to varying degrees. the concurrency control of apache is mainly controlled by one of prefork and worker. we can use httpd-l to determine whether the currently used MPM is prefork. c, or Worker. c. the following describes the prefork configuration in apache. the following are my optimized parameters. # With this parameter, you do not need to modify the source code like apache1 to modify the limit of 256 customers. it takes effect only when you put the lecture at the beginning, 2000 is the maximum value of this parameter ServerLimit 2000 # specifies the number of sub-processes created when the server is started. the default value of prefork is 5. StartServers 25 # specify the 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 a maximum speed per second. Do not set this parameter too large. MinSpareServers 25 # set the maximum number of idle sub-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 you set the value of this command to a smaller value than MinSpareServers, Apache will automatically change it to "MinSpareServers + 1 ". MaxSpareServers 50 # limit the maximum number of client access requests (number of concurrent threads in a single process) 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, the request in the queue will be served. To increase this value, you must increase ServerLimit at the same time. MaxClients 2000 # the maximum number of requests allowed by the servo for each sub-process during its lifetime. the default value is 10000. after MaxRequestsPerChild is reached, the sub-process will end. If MaxRequestsPerChild is "0", the child process will never end. MaxRequestsPerChild 10000 Setting MaxRequestsPerChild to a non-zero value has two advantages: 1. it can prevent (accidental) infinite memory leakage and thus exhaust the memory. 2. a limited life cycle is provided for processes, which helps reduce the number of active processes when server load is reduced. Working method: a separate control process (parent process) is responsible for generating child processes, which are used to listen for requests and respond. Apache always tries to keep some standby (spare) or idle sub-processes to meet the upcoming requests. In this way, the client does not need to wait for the sub-process to generate before obtaining the service. In Unix systems, the parent process is usually run as the root to fix port 80, while the sub-process generated by Apache is usually run as a low-privilege user. The User and Group commands are used to set low-privilege users for sub-processes. Users running a sub-process must have the read permission on the content served by the sub-process, but must have as few permissions as possible for resources other than the service content. We often need to check the number of httpd processes (that is, the number of concurrent requests that Apache can process in prefork mode): # ps-ef | grep httpd | wc-l results, the number of concurrent requests that Apache can process. this value is automatically adjusted by Apache based on the load. view the number of concurrent requests of Apache and its TCP connection status: Status: description CLOSED: No connection is active or LISTEN is in progress: the server is waiting to enter the call SYN_RECV: a connection request has arrived, wait to confirm SYN_SENT: The application has started. open a connection to ESTABLISHED: normal data transmission status FIN_WAIT1: The application says it has completed FIN_WAIT2: the other side has agreed to release ITMED_WAIT: wait until all groups die CLOSING: both sides attempt to disable TIME_WAIT at the same time: the other side has initialized a release processing, and waits for the timeout to end LAST_ACK: The following is an example of optimization configuration for all groups to die: ServerLimit 20000 StartServers 10 MinSpareServers 10 MaxSpareServers 20 ServerLimit 2000 MaxClients 1000 MaxRequestsPerChild 10000 StartServers 3 MaxClients 2000 ServerLimit 25 MinSpareThreads 50 MaxSpareThreads 200 ThreadLimit 200 ThreadsPerChild 100 MaxRequestsPerChild 0  

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.