Differences
This shows you the differences between two versions of the page.
— | apache_tuning [2015/06/02 03:05] (current) – created - external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Apache Performance Tuning ====== | ||
+ | * [[Apache]] | ||
+ | * [[Apache Benchmarks]] | ||
+ | * [[Apache Security]] | ||
+ | * [[Linux Memory and Processes]] | ||
+ | |||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | |||
+ | Apache Documentation: | ||
+ | |||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | |||
+ | From Apache: **The single biggest hardware issue affecting webserver performance is RAM.** A webserver should never ever have to swap, as swapping increases the latency of each request beyond a point that users consider "fast enough" | ||
+ | |||
+ | ==== Apache Tuning 101 ==== | ||
+ | |||
+ | These are the best places to start tuning Apache' | ||
+ | |||
+ | * Add more RAM | ||
+ | * For prefork worker, lower the MaxRequestsPerChild so it flushes processes reasonably, matching system load so it does not use too much memory | ||
+ | * Switch to '' | ||
+ | * Calculate memory usage and set the '' | ||
+ | * Find the average number of concurrent connections (see CGP graphs) and set the MaxClients close to that so it can handle loads. | ||
+ | * Run benchmark testing | ||
+ | * Disable or lower KeepAlive setting | ||
+ | * Lower Timeout setting | ||
+ | |||
+ | |||
+ | ==== Apache Directives ==== | ||
+ | |||
+ | === MaxClients === | ||
+ | |||
+ | From Apache: "You can, and should, control the MaxClients setting so that your server does not spawn so many children it starts swapping. This procedure for doing this is simple: determine the size of your average Apache process, by looking at your process list via a tool such as top, and divide this into your total available memory, leaving some room for other processes." | ||
+ | |||
+ | Run [[Apache Benchmarks|benchmark testing]] to see how the webserver performs with actual and theoretical numbers of concurrent connections. | ||
+ | |||
+ | == Tuning MaxClients Example == | ||
+ | |||
+ | Let's say your amount of RAM is 992 MB. See how much free memory is available: | ||
+ | |||
+ | < | ||
+ | free -m | ||
+ | | ||
+ | Mem: | ||
+ | -/+ buffers/ | ||
+ | Swap: | ||
+ | </ | ||
+ | |||
+ | |||
+ | Determine the maximum amount of memory allotted to Apache threads: | ||
+ | |||
+ | < | ||
+ | ps a -ylC apache2 --sort rss | awk ' | ||
+ | </ | ||
+ | |||
+ | This will return the number in MB. | ||
+ | |||
+ | Also, here's a nice script that outputs the values cleanly: | ||
+ | |||
+ | < | ||
+ | ps -ylC apache2 | awk '{x += $8;y += 1} END {print " | ||
+ | </ | ||
+ | |||
+ | Find out how much memory the operating system is using with all running daemons on a regular basis. | ||
+ | |||
+ | Once you have that, and the highest number of memory that an Apache thread is using, divide the two numbers. | ||
+ | |||
+ | < | ||
+ | echo <MB of memory from Apache> / < | ||
+ | </ | ||
+ | |||
+ | The output of that should be what MaxClients should be set to. | ||
+ | |||
+ | === MaxRequestsPerChild === | ||
+ | |||
+ | For the '' | ||
+ | |||
+ | If a site is not quickly receiving new requests (high load server), it's safe to lower this value so the threads start creating after less requests. | ||
+ | ==== mod_deflate ==== | ||
+ | |||
+ | mod_deflate is enabled by default on Gentoo Apache installs, but it won't compress anything with gzip unless enabled. | ||
+ | |||
+ | < | ||
+ | < | ||
+ | AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml application/ | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | You can verify that it is working properly if you can see this line in the Network tab of Inspect Element in Chrome: | ||
+ | |||
+ | < | ||
+ | ==== prefork mpm ==== | ||
+ | |||
+ | prefork is the default MPM worker with our Apache installations. | ||
+ | |||
+ | == Default Configuration == | ||
+ | |||
+ | < | ||
+ | StartServers 5 | ||
+ | MinSpareServers 5 | ||
+ | MaxSpareServers 10 | ||
+ | MaxClients 256 | ||
+ | MaxRequestsPerChild 10000 | ||
+ | </ | ||
+ | |||
+ | == Recommendations == | ||
+ | |||
+ | Apache documentation for prefork recommends that '' | ||
+ | |||
+ | '' | ||
+ | |||
+ | **Tweaking the MaxClients value to a reasonable number is the best way to prevent your webserver from grinding to a halt!** | ||
+ | |||
+ | Also see [[http:// | ||
+ | |||
+ | ==== Modules ==== | ||
+ | |||
+ | Apache 2.2 ships with a lot of modules, many of which are not needed by most clients. | ||
+ | |||
+ | Here is a list of basic modules that are needed or generally used: actions alias auth_basic authn_default auth_digest authn_file authz_default authz_host authz_user autoindex cache cgi cgid deflate dir disk_cache env expires file_cache filter headers include info log_config logio mem_cache mime mime_magic negotiation rewrite setenvif status unique_id userdir vhost_alias | ||
+ | |||
+ | ==== mod_core ==== | ||
+ | |||
+ | == Lower default timeout == | ||
+ | |||
+ | * [[http:// | ||
+ | |||
+ | This is the amount of time the server will wait for certain events before failing a request. | ||
+ | |||
+ | < | ||
+ | Timeout 20 | ||
+ | </ | ||
+ | |||
+ | == KeepAlive == | ||
+ | |||
+ | * [[http:// | ||
+ | |||
+ | Apache default is to enable the setting. | ||
+ | |||
+ | The KeepAlive directive sets how long Apache will wait for another request once the initial one has been fulfilled. | ||
+ | |||
+ | For a normal site, enabling the setting is fine, and keeping a low KeepAliveTimeout value (say, 2 seconds). | ||
+ | |||
+ | < | ||
+ | KeepAlive On | ||
+ | KeepAliveTimeout 2 | ||
+ | </ | ||
+ | |||
+ | For a high-traffic site, disable it so that it does not delay creating new connections. | ||
+ | |||
+ | < | ||
+ | KeepAlive Off | ||
+ | </ | ||
+ | |||
+ | == Disable reverse DNS lookups == | ||
+ | |||
+ | < | ||
+ | # HostnameLookups: | ||
+ | # e.g., www.apache.org (on) or 204.62.129.132 (off). | ||
+ | # The default is off because it'd be overall better for the net if people | ||
+ | # had to knowingly turn this feature on, since enabling it means that | ||
+ | # each client request will result in AT LEAST one lookup request to the | ||
+ | # nameserver. | ||
+ | HostnameLookups Off | ||
+ | </ | ||
+ | |||
+ | == Memory Mapping and Sendfile == | ||
+ | |||
+ | < | ||
+ | # EnableMMAP and EnableSendfile: | ||
+ | # memory-mapping or the sendfile syscall is used to deliver | ||
+ | # files. | ||
+ | # be turned off when serving from networked-mounted | ||
+ | # filesystems or if support for these functions is otherwise | ||
+ | # broken on your system. | ||
+ | EnableMMAP On | ||
+ | EnableSendfile On | ||
+ | </ |