Apache concurrency Optimization Setup method _linux

Source: Internet
Author: User
Tags cpu usage vps

Apache Optimization steps:

1, first look at Apache operating mode, view the command:

Httpd-l

Compiled in Modules:
Core.c
Prefork.c
Http_core.c
Mod_so.c
Here you can see that the run mode is Prefork mode.

2, modify the Apache httpd.conf configuration

This article is 512M memory and equipped with Apache VPS optimization, please do not tell me to install nginx is much better, it is true, but you say so, this article is meaningless, haha. Then let's get to the point!

Operating System (Linux): CentOS 5.5 32 bit (not that I said this is better, but cents I used more skilled)

Memory: 512M
CPU:1G (1000mhz,vps actually have this number, it's hard to say)
apache:2.2 (recommended low version, upgrade to the high version, after all, the old version has many problems, and the performance is also a good version better)

The main operation is the PHP program, the other is not much said, and this article is not very related.

First of all, the title said, the optimization is described in this article for Apache version 2.2, and the use of Perfork MPM mode. Perfork is Apache under Linux under the default installation mode, if the use of Apache as a server, or use the perfork mode is better, the worker mode of PHP some features do not support. If you are using the win system or have not made and perfork, then you can choose not to look at or hold the attitude of learning to see slightly.

The following is the blogger's own opinion, the actual test according to your server, procedures and other factors on-demand processing.

In general, we need to optimize Apache because Apache takes up a lot of memory, which leads to VPS, because perfork is processed by multiple processes, each process will point to a certain amount of memory. So limit the number of httpd processes, so as to optimize the role of Apache. The amount of memory that affects a single httpd process is mainly loading modules, at least what modules are appropriate, and I'm hard to say because this is based on your needs. Some people on the internet say that most of them are not listed in the module, which I think is very misleading because most of you may be one of them. Block out the useful modules, at worst, to reboot the Apache error, or some of the features can not be used. So I'm not going to list any of the least modular loading scenarios or anything. So, I will only say some reference.

Apache (perfork) under the module load, is not a module load list, no module is not blocked off, but I also do not know its practical role.

(a), for apache2.2, module name has the cache is generally useful, because there are many of these templates are more than 2.1 of Apache support, so this class of modules will not be missed and exist.

(b), the most likely or required modules are:

Copy Code code as follows:

LoadModule Auth_basic_module modules/mod_auth_basic.so
LoadModule Authn_file_module modules/mod_authn_file.so
LoadModule Include_module modules/mod_include.so
LoadModule Log_config_module modules/mod_log_config.so
LoadModule Expires_module modules/mod_expires.so
LoadModule Deflate_module modules/mod_deflate.so
LoadModule Headers_module modules/mod_headers.so
LoadModule Setenvif_module modules/mod_setenvif.so
LoadModule Mime_module modules/mod_mime.so
LoadModule Autoindex_module modules/mod_autoindex.so
LoadModule Negotiation_module modules/mod_negotiation.so
LoadModule Dir_module modules/mod_dir.so
LoadModule Alias_module modules/mod_alias.so
LoadModule Rewrite_module modules/mod_rewrite.so

(c), I have a rough point of the module in Apache about in 50+, to choose from all the applicable module loading list is not possible, at least the CGI module I will not use, but do not rule out you will not use.

Module loading this piece is said here, this everyone many Baidu, many use naturally will identify some useful modules and gradually shield the unused modules.

Next, is the most important perfork configuration, but also bothered me for a long time, although the parameters of the 6, it is enough to make your VPS as a machine. First, list the parameters that need to be modified, and modify the files located in httpd.conf.

Copy Code code as follows:

Timeout 30
KeepAlive on
Maxkeepaliverequests 80
KeepAliveTimeout 15
<ifmodule prefork.c>
Serverlimit 150
Startservers 5
Minspareservers 5
Maxspareservers 10
MaxClients 150
Maxrequestsperchild 4000
</IfModule>

The above code is to be modified, in fact, we are to modify the parameters in the Perfork, the reason for the timeout,keepalive and so also write out, because this is also affecting the performance of Apache.

Timeout is a connection how much time after the disconnect, this parameter set in 30-60 is the general PHP program is applicable, at least to run some to occupy a lot of time in the PHP program, then the appropriate height can also be, but please do not too high, otherwise it will affect the Apache performance, This optimization we use 30 is enough.

Maxkeepaliverequests is a connection to the largest number of requests, for the page has more pictures and other elements, can be appropriately adjusted up a bit, for the general Web page set at 80-120 is enough, we set to 100, If the setting is too high it can cause httpd to be unable to exit the freed memory for a long time.

KeepAliveTimeout is when a user processes a connection, and if there is a request within the time of the parameter, the execution continues, and no new connections need to be recreated until the maximum value of maxkeepaliverequests is reached. For Perfork mode, some people think that the keepalive off will be better, but for the vast majority of the site will not be a lot of some picture elements, so the item open, and the keeptimeout set in 2-5 seconds, not only effectively improve server performance, You can also speed up page opening.

Next, is formally entered the Perfork parameter set, do not want the server to run for a while the memory is full to see OH.

The first is the parameter serverlimit, which is the server's maximum support for simultaneous connections, which determines the range of values that the following parameters maxclient can set. Serverlimit actually only play a limiting role, and there is no practical effect, perhaps, I do not know. What actually works is the maxclient parameter, but the value is limited by the serverlimit, and so on.

The other three parameters are startservers,minspareservers,maxspareservers, why the three parameters are said together, because these three numbers are linked together. These three numbers are determined by the number of idle processes, Startservers should range is between Minspareservers and Maxspareservers. Otherwise, Apache automatically restores the value to both, so don't waste unnecessary resources. According to the perfork default configuration, these three parameters are: 5,5,10. But for a VPS with only 512M of memory, I think there is too much load. So I think it's time to step back and set these three parameters to 4,4,10, which looks nothing different than the 5,5,10 above, with a maximum of 10, but in actual use, 4,4,10 releases much faster memory than 5,5,10.

Next is the Maxclient maximum support how many clients at the same time to connect the server, simply said that the maximum concurrency number support, this is not particularly necessary to say. For 512M VPS You also don't think your station can withstand million PV, using the default 150 is already out of load. Serverlimit and maxclient are generally set to the same value.

The last is the Maxrequestsperchild parameter, a process after how many times to exit, set to 0 is infinite, that is, will not quit, then the httpd process will not exit. Well, you'll be waiting for your VPS to reboot. For this parameter, I have a rough search, many people recommend 1000 times, there are 100 times. According to my test, for a VPS with only 512M of memory, the value set above 500 will quickly fill up the memory, but not at least at the machine. And the observation that, but the memory is full, the CPU usage rate has almost become 0. So, when the machine that nothing can do, but also consume a little more CPU resources to avoid, improve stability. So, my final decision is to define the value between 30-40. This setting, you can see that the memory release speed has been greatly improved, but also can see the CPU is also frequent jumping up and down. This setting allows you to revert to normal in the shortest possible time, even if the memory is full.

So for the 512MB Vps,apache (perfork mode), the final excellent configuration is:

Copy Code code as follows:

Timeout 30
KeepAlive on
Maxkeepaliverequests 100
KeepAliveTimeout 2
<ifmodule prefork.c>
Serverlimit 150
Startservers 4
Minspareservers 4
Maxspareservers 10
MaxClients 150
Maxrequestsperchild 40
</IfModule>

In this way, our configuration is basically complete, load test can be done. In the load test, we use the AB test to declare that you are using your local virtual machine to test a static page on the server instead of testing it on the server. Before, I was doing this, because the same speed, the effect is relatively good, but others are different speed, so the server test is not accurate.

3, now see how to optimize:
The number of connections is theoretically greater, but it depends on the hardware, CPU, memory, bandwidth, and so on, to see the current number of Apache connections:

PS aux | grep httpd | Wc-l

Calculates the average amount of memory consumed by httpd:

PS aux | grep-v grep |awk '/httpd/{sum + = $6;n++}; End{print sum/n} '

This is just a reference. After the calculation, subtract the resources required by the server system itself.
For example, memory 2G, minus 500M to the server, and 1.5G, then you can get the maximum number of connections: in about 8000.
After the modified httpd.conf's prefork configuration is:

Copy Code code as follows:

<ifmodule prefork.c>
Startservers 5
Minspareservers 5
Maxspareservers 10
Serverlimit 5500
MaxClients 5000
Maxrequestsperchild 100
</IfModule>

Here the emphasis is on serverlimit, which must be placed before maxclients, the value is greater than maxclients.

4 Restart Apache, and then open the website to see if there will be a slow problem.

Note: You can use the following command to locate the httpd.conf file

Find/-name httpd.conf

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.