Sunday, 2 August 2009

Basic Apache optimization

Apache – Intro

* A public-domain open source Web server developed by a loosely-knit group of programmers

* The first version of Apache, based on the NCSA httpd Web server, was developed in 1995

* Core development of the Apache Web server is performed by a group of about 20 volunteer programmers, called the Apache Group

* It was developed from existing NCSA code plus various patches, hence the name “a patchy” server, or “Apache” server

* Another reason was , The name ‘Apache’ was chosen from respect for the Native American Indian tribe of Apache, well-known for their superior skills

MPM (Multi-Processing Modules)

* The event Multi-Processing Module (MPM) is designed to allow more requests to be served simultaneously by passing off some processing work to supporting threads, freeing up the main threads to work on new requests.

Working :

1. MPM tries to fix the ‘keep alive problem’ in HTTP. After a client completes the first request, the client can keep the connection open, and send further requests using the same socket

2. This can save signifigant overhead in creating TCP connections

3. However, Apache traditionally keeps an entire child process/thread waiting for data from the client, which brings its own disadvantages

4. To solve this problem, this MPM uses a dedicated thread to handle both the Listening sockets, and all sockets that are in a Keep Alive state

Select MPM (Multi-Processing Modules)

prefork [default MPM for Apache 2.0 and 1.3]

* Apache 1.3-based

* Multiple processes, 1 thread per process, processes handle requests

* Used for security and stability

* Has higher memory consumption and lower performance over the newer Apache 2.0-based threaded MPMs

* A single control process is responsible for launching child processes which listen for connections and serve them when they arrive

* Apache always tries to maintain several spare or idle server processes, which stand ready to serve incoming requests

* In this way, clients do not need to wait for a new child processes to be forked before their requests can be served worker

* Apache 2.0-based

* Multiple processes, many threads per process, threads handle requests

* Used for lower memory consumption and higher performance

* Does not provide the same level of isolation request-to-request, as a process-based MPM does

winnt

* The only MPM choice under Windows

* 1 parent process, exactly 1 child process with many threads, threads handle requests

* Best solution under Windows, as on this platform, threads are always “cheaper” to use over processes

Optimize the following directives:

Timeout

* The number of seconds before receives and sends time out

* By default the Timeout directive is set to 300 seconds.

* We can decrease it to 15 seconds to mitigate the potential effects of a denial of service attack/server load

KeepAlive

* The KeepAlive directive allows multiple requests to be sent over the same TCP connection

* This is particularly useful while serving HTML pages with lot of images

* If KeepAlive is set to Off, then for each images, a separate TCP connection has to be made

* Overhead due to establishing TCP connection can be eliminated by turning On KeepAlive

MaxKeepAliveRequests

* The maximum number of requests to allow during a persistent connection

* Set to 0 to allow an unlimited amount; leave this number high, for maximum performance

KeepAliveTimeout

* KeepAliveTimeout determines how long to wait for the next request

* Set this to a low value, perhaps between two to five seconds

StartServers

* StartServers controls the number of child-processes that Apache forks before starting to accept connections

* It’s best to set StartServers and MinSpareServers to high numbers,so that if you get a high load just after the server has been restarted, the fresh servers will be ready to serve requests immediately

MaxSpareServers and MinSpareServers

* Determine how many child processes to keep while waiting for requests

* If the MinSpareServers is too low and a bunch of requests come in, then Apache will have to spawn additional child processes to serve the requests

MaxClients

* The MaxClients sets the limit on maximum simultaneous requests that can be supported by the server

* No more than this much number of child processes are spawned. It shouldn’t be set too low such that new connections are put in queue, which eventually time-out and the server resources are left unused

* Setting this too high will cause the server to start swapping and the response time will degrade drastically

* Appropriate value for MaxClients can be calculated as:

MaxClients = Total RAM dedicated to the web server / Max child process size

* To configure more than 256 clients, you must edit the HARD_SERVER_LIMIT entry in httpd.h and recompile Apache

Sample values for each directives

Timeout 15
KeepAlive On
maxKeepAliveRequests 100
KeepAliveTimeout 3
StartServers 8
MinSpareServers 10
MaxSpareServers 20
MaxClients 250
MaxRequestsPerChild 0






Default Configuration of MPM

<IfModule prefork.c>

StartServers 8

MinSpareServers 5

MaxSpareServers 20

MaxClients150

MaxRequestsPerChild 1000

</IfModule>

<IfModule worker.c>

StartServers 2

MaxClients 150

MinSpareThreads 25

MaxSpareThreads 75

ThreadsPerChild 25

MaxRequestsPerChild 0

</IfModule>

<IfModule mpm_winnt.c>

ThreadsPerChild 250

MaxRequestsPerChild 0

</IfModule>

No comments:

Post a Comment