Skip to content

Apache KeepAlive and timeout tuning on cPanel

KeepAlive lets a single TCP connection serve multiple HTTP requests, cutting round-trips. Tuned wrong, it ties up Apache workers serving idle connections. Here is the balance.

Apache KeepAlive is the feature that lets one TCP connection serve multiple HTTP requests in sequence — useful because TCP handshakes are expensive and TLS handshakes are very expensive. The default cPanel config either turns KeepAlive off (saving worker slots, losing the benefit) or sets it on with long timeouts (winning the benefit, tying up workers serving nothing). Both extremes are wrong on a busy host. The right tuning depends on what is in front of Apache.

What’s happening

When KeepAlive is on with KeepAliveTimeout 5, Apache holds the connection open for 5 seconds after each response. If the client (browser) sends another request in that window, it reuses the connection — no TCP handshake, no TLS handshake. If not, Apache closes the connection. That 5-second hold ties up one Apache worker per idle connection.

On a busy host with MaxRequestWorkers 400 and many simultaneous visitors, 100 of those workers might be holding idle connections waiting for the next request. That is 25% of capacity gone to nothing. Solutions: reduce the timeout, reduce the max keepalive requests, or — better — put nginx or a load balancer in front that handles keepalive on the client side and uses a connection pool to Apache.

How to verify

Current settings:

grep -E '^KeepAlive|^MaxKeepAlive|^KeepAliveTimeout' /etc/apache2/conf/httpd.conf

Live state via mod_status:

curl -s http://127.0.0.1/server-status | grep -E 'KeepAlive|busy'

The Apache scoreboard in mod_status shows workers in state K (keepalive idle). If many are in K and few in R (reading) or W (writing), KeepAlive is over-tuned.

For HTTP/2, the calculus is different — multiplexing handles many requests per connection by design.

The fix

  1. Confirm KeepAlive is on:

    KeepAlive On
  2. Tune timeout to 2-5 seconds for a typical cPanel host:

    KeepAliveTimeout 3
    MaxKeepAliveRequests 100

    A 3-second timeout is enough for a browser parsing a page to issue follow-up requests; longer than that and the connection is genuinely idle.

  3. For high-concurrency hosts, prefer HTTP/2 + low KeepAlive:

    HTTP/2 multiplexes, so the original “reuse the connection” benefit is implicit. With HTTP/2 plus a short timeout (1-2s), Apache spends fewer worker-slots on idle KeepAlive.

  4. Tune the Apache Timeout directive separately:

    Timeout 60

    This is the maximum time Apache waits for any single request. Default 300 is far too long — a stuck request can hold a worker for 5 minutes. 60s is generous; for purely HTTP API workloads, 30s.

  5. Consider a reverse proxy in front. nginx in front of Apache with proxy_http_version 1.1 and proxy_set_header Connection "" terminates KeepAlive at nginx and uses a connection pool to Apache. Apache workers no longer wait on idle clients.

  6. For LiteSpeed instead of Apache, KeepAlive is handled in the event-driven core; the equivalent setting is Keep-Alive Timeout in WebAdmin > Tuning. The same logic applies, but LiteSpeed handles many more idle connections per worker than Apache prefork ever could.

After tuning, re-check mod_status an hour later: more workers in W/R states, fewer in K. Stack Harbor configures KeepAlive per server config as part of managed cPanel operations.