Linux-Apache-Tuning

From DevRandom

Jump to: navigation, search

Contents

Apache performance tuning

HostnameLookups off

Prior to Apache 1.3, HostnameLookups defaulted to On. This adds latency to every request because it requires a DNS lookup to complete before the request is finished. Turn this off if turned on.

FollowSymLinks and SymLinksIfOwnerMatch

This is somewhat counter intutive. Apache will have to issue extra system calls to check up on symlinks. One extra call per filename component. SymLinksIfOwnerMatch is used for better security especially is multi user or muti site systems.

LogFiles

If there are several virtual hosts and each has a separate log file for error and access then apache need to open several file handles. This may be a perfomance issue but on the other hand if many virtual hosts are writing to the same log file then it may cause a system level bottleneck.

AllowOverride

Then Apache will attempt to open /.htaccess, /www/.htaccess, and /www/htdocs/.htaccess for each directory access. For performance use AllowOverride None everywhere.

MaxRequestsPerChild

Related to process creation is process death induced by the MaxRequestsPerChild setting. By default this is 0, which means that there is no limit to the number of requests handled per child. If your configuration currently has this set to some very low number, such as 30, you may want to bump this up significantly.

MaxClients, for prefork MPM

MaxClients sets a limit on the number of simultaneous connections/requests that will be served. Set this number too low and resources will be underused, Set this too high and an influx of connections might cause performance issues.

An approximation of this number should be derived by dividing the amount of system memory (physical RAM) available by the maximum size of an apache/httpd process; with a generous amount spared for all other processes.

MaxClients ≈ (RAM - size_all_other_processes)/(size_apache_process)

With the above configuration, we start with 5-10 processes and set a top limit of 15. Anything above this number will cause serious swapping and thrashing under a load; due to the low amount of RAM available to the [virtual] Server. With a dedicated Server, the default values [ServerLimit 256] will work with 1-2GB of RAM.

When calculating MaxClients, take into consideration that the reported size of a process and the effective size are two different values. In this setup, it might be safe to use 20 or more workers... Play with different values and check your system stats.

ListenBackLog

When more connections are attempted than there are workers, the connections are placed into a queue. The default queue size value is 511 and can be adjusted with the ListenBackLog directive.

Separate Static and Dynamic Content

Use separate servers for static and dynamic content. Apache processes serving dynamic content will carry overhead and swell to the size of the content being served, never decreasing in size. Each process will incur the size of any loaded PHP or Perl libraries. For a more efficient use of system memory, either use mod_proxy to pass specific requests onto another Apache Server, or use a lightweight server to handle static requests:

Loaded Modules

Reduce memory footprint by loading only the required modules. Some also advise to statically compile in the needed modules, over building DSOs (Dynamic Shared Objects). Keep in mind that you will need to manually rebuild Apache every time a new version or security advisory for a module is put out, creating more work, more build related headaches, and more downtime.

KeepAlive

Enable HTTP persistent connections to improve latency times and reduce server load significantly [25% of original load is not uncommon].

prefork MPM: KeepAlive On KeepAliveTimeout 2 MaxKeepAliveRequests 80

worker: KeepAlive On KeepAliveTimeout 15 MaxKeepAliveRequests 80

With the prefork MPM, it is recommended to set 'KeepAlive' to 'Off'. Otherwise, a client will tie up an entire process for that span of time. Though in my experience, it is more useful to simply set the 'KeepAliveTimeout' value to something very low [2 seconds seems to be the ideal value]. This is not a problem with the worker MPM [thread-based]

With the worker MPMs, the default 15 second timeout is setup to keep the connection open for the next page request; to better handle a client going from link to link. Check logs to see how long a client remains on each page before moving on to another link. Set value appropriately [do not set higher than 60 seconds]

ExtendedStatus

If mod_status is included, make sure that directive 'ExtendedStatus' is set to 'Off'. Otherwise, Apache will issue several extra time-related system calls on every request made.

Timeout

Lower the amount of time the server will wait before failing a request.

Redirect rules

Redirect rules affect performance. For every redirect rule the client will have to make additional requests for handling the redirects


SendFile Directive

This directive controls whether httpd may use the sendfile support from the kernel to transmit file contents to the client.Apache uses sendfile to deliver the file contents without ever reading the file if the OS supports it. This sendfile mechanism avoids separate read and send operations, and buffer allocations. But on some platforms or within some filesystems, it is better to disable this feature to avoid operational problems: On Linux the use of sendfile triggers TCP-checksum offloading bugs on certain networking cards when using IPv6.

HTTP Compression & Caching

HTTP compression is completely specified in HTTP/1.1. The server uses either the gzip or the deflate encoding method to the response payload before it is sent to the client. Client then decompresses the payload. There is no need to install any additional software on the client side since all major browsers support these methods. Using compression will save bandwidth and improve response time; studies have found a mean gain of %75.2 when using compression. HTTP Compression can be enabled in Apache using the mod_deflate module. Payload is compressed only if the browser requests compression, otherwise uncompressed content is served. A compression-aware browser inform the server that it prefer compressed content through the HTTP request header

Caching

Caching servers like squid or caching appliances can also be used to improve perfomance. Caching PHP and Perl requests will also help.

Further Reading

http://virtualthreads.blogspot.com/2006/01/tuning-apache-part-1.html

http://blogcritics.org/archives/2006/01/27/175740.php

Views
Personal tools
About Me

Blog

Contact Me

Resume

Photos