Tuesday, January 13, 2009

Apache Tuning

Links:

(Apache)
http://phplens.com/phpeverywhere/tuning-apache-php
http://httpd.apache.org/docs/1.3/misc/perf-tuning.html
http://perl.apache.org/docs/1.0/guide/performance.html#the_crashme_Script
http://tldp.org/LDP/LGNET/123/vishnu.html
http://www.perlcode.org/tutorials/apache/tuning.html
http://drupal.org/node/215516

(PHP)
http://phplens.com/lens/php-book/optimizing-debugging-php.php

Apache is configured using the httpd.conf file. The following parameters are particularly important in configuring child processes:

Directive

Default

Description

MaxClients

256

The maximum number of child processes to create. The default means that up to 256 HTTP requests can be handled concurrently. Any further connection requests are queued.

StartServers

5

The number of child processes to create on startup.

MinSpareServers

5

The number of idle child processes that should be created. If the number of idle child processes falls to less than this number, 1 child is created initially, then 2 after another second, then 4 after another second, and so forth till 32 children are created per second.

MaxSpareServers

10

If more than this number of child processes are alive, then these extra processes will be terminated.

MaxRequestsPerChild

0

Sets the number of HTTP requests a child can handle before terminating. Setting to 0 means never terminate. Set this to a value to between 100 to 10000 if you suspect memory leaks are occurring, or to free under-utilized resources.

For large sites, values close to the following might be better:

MinSpareServers 32

MaxSpareServers 64

Apache on Windows behaves differently. Instead of using child processes, Apache uses threads. The above parameters are not used. Instead we have one parameter: ThreadsPerChild which defaults to 50. This parameter sets the number of threads that can be spawned by Apache. As there is only one child process in the Windows version, the default setting of 50 means only 50 concurrent HTTP requests can be handled. For web servers experiencing higher traffic, increase this value to between 256 to 1024.

Other useful performance parameters you can change include:

Directive

Default

Description

SendBufferSize

Set to OS default

Determines the size of the output buffer (in bytes) used in TCP/IP connections. This is primarily useful for congested or slow networks when packets need to be buffered; you then set this parameter close to the size of the largest file normally downloaded. One TCP/IP buffer will be created per client connection.

KeepAlive [on|off]

On

In the original HTTP specification, every HTTP request had to establish a separate connection to the server. To reduce the overhead of frequent connects, the keep-alive header was developed. Keep-alives tells the server to reuse the same socket connection for multiple HTTP requests.

If a separate dedicated web server serves all images, you can disable this option. This technique can substantially improve resource utilization.

KeepAliveTimeout

15

The number of seconds to keep the socket connection alive. This time includes the generation of content by the server and acknowledgements by the client. If the client does not respond in time, it must make a new connection.

This value should be kept low as the socket will be idle for extended periods otherwise.

MaxKeepAliveRequests

100

Socket connections will be terminated when the number of requests set by MaxKeepAliveRequests is reached. Keep this to a high value below MaxClients or ThreadsPerChild.

TimeOut

300

Disconnect when idle time exceeds this value. You can set this value lower if your clients have low latencies.

LimitRequestBody

0

Maximum size of a PUT or POST. O means there is no limit.

If you do not require DNS lookups and you are not using the htaccess file to configure Apache settings for individual directories you can set:

# disable DNS lookups: PHP scripts only get the IP address

HostnameLookups off

# disable htaccess checks

AllowOverride none

If you are not worried about the directory security when accessing symbolic links, turn on FollowSymLinks and turn off SymLinksIfOwnerMatch to prevent additional lstat() system calls from being made:

Options FollowSymLinks

#Options SymLinksIfOwnerMatch


MaxClients:

You can, and should, control the MaxClients setting so that your server does not spawn so many children it starts swapping. The 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.


MaxRequestsPerChild:

when using PHP's persistent database connections: don't set MaxRequestsPerChild too high so idle resources are released quickly

Tools:

http://2bits.com/articles/tools-for-performance-tuning-and-optimization.html

HTTPERF:
ftp://ftp.hpl.hp.com/pub/httperf/

ab (Apache Benchmark)


PHP:

Enable the compression of HTML by putting in your php.ini:

output_handler = ob_gzhandler

No comments: