Enabling KeepAlive in Apache keeps HTTP connections open so browsers and API clients can reuse a single TCP/TLS session for multiple requests, reducing handshake overhead and improving responsiveness on asset-heavy sites.

KeepAlive behavior is controlled by KeepAlive, MaxKeepAliveRequests, and KeepAliveTimeout, which determine whether persistent connections are allowed, how many requests a single connection may carry, and how long an idle connection is held open before the server closes it.

Keeping connections open consumes worker capacity and memory, so timeouts that are too long can reduce throughput during traffic spikes (especially with mpm_prefork) and should be tuned alongside concurrency limits like MaxRequestWorkers. The steps below use the Debian-family layout (/etc/apache2, apache2 service, a2enconf); RHEL-family systems typically use /etc/httpd/conf/httpd.conf and the httpd service.

Steps to enable KeepAlive in Apache:

  1. Show the current KeepAlive settings defined in the Apache configuration files.
    $ sudo grep -RIn --include='*.conf' -E '^[[:space:]]*(KeepAlive|MaxKeepAliveRequests|KeepAliveTimeout)' /etc/apache2
    /etc/apache2/apache2.conf:98:KeepAlive On
    /etc/apache2/apache2.conf:105:MaxKeepAliveRequests 100
    /etc/apache2/apache2.conf:111:KeepAliveTimeout 5
  2. Create a dedicated KeepAlive config snippet under /etc/apache2/conf-available.
    $ sudo tee /etc/apache2/conf-available/keepalive.conf >/dev/null <<'EOF'
    KeepAlive On
    MaxKeepAliveRequests 100
    KeepAliveTimeout 2
    EOF

    A KeepAliveTimeout that is too high can consume MaxRequestWorkers and cause slow responses or 503 Service Unavailable during traffic spikes.

  3. Enable the configuration snippet.
    $ sudo a2enconf keepalive
    Enabling conf keepalive.
    To activate the new configuration, you need to run:
      systemctl reload apache2
  4. Validate configuration syntax before reload.
    $ sudo apache2ctl configtest
    Syntax OK
  5. Reload Apache to apply the change.
    $ sudo systemctl reload apache2
  6. Confirm the KeepAlive snippet values that are now loaded.
    $ sudo grep -n -E '^(KeepAlive|MaxKeepAliveRequests|KeepAliveTimeout)' /etc/apache2/conf-enabled/keepalive.conf
    1:KeepAlive On
    2:MaxKeepAliveRequests 100
    3:KeepAliveTimeout 2

    Setting MaxKeepAliveRequests to 0 removes the request limit, but keeping a finite value can reduce the impact of long-lived clients on busy servers.