How to enable KeepAlive in Apache

Enabling KeepAlive in Apache lets browsers, reverse proxies, and API clients reuse one HTTP connection for more than one request instead of negotiating a new TCP or TLS session each time. That reduces connection churn and usually improves response time on sites that serve many small assets, redirects, or follow-up requests to the same host.

Persistent connection behavior in Apache is controlled by the core KeepAlive, MaxKeepAliveRequests, and KeepAliveTimeout directives. Current Apache 2.4 documentation still lists defaults of KeepAlive On, MaxKeepAliveRequests 100, and KeepAliveTimeout 5, and Debian-family packages ship those defaults in /etc/apache2/apache2.conf unless a later drop-in overrides them.

Examples below use the Debian and Ubuntu layout with /etc/apache2/conf-available, a2enconf, and the apache2 systemd unit. Many hosts already have keepalive enabled, so the safer job is often to restore or pin the directive in a small drop-in instead of editing the vendor file directly, and KeepAliveTimeout should stay conservative on busy mpm_prefork servers because idle clients can hold workers open; on RHEL-family systems, place the equivalent directive in /etc/httpd/conf/httpd.conf or a file under /etc/httpd/conf.d and reload httpd instead.

Steps to enable KeepAlive in Apache:

  1. Inspect the current KeepAlive directives in the active Apache config tree.
    $ 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

    Debian and Ubuntu already enable KeepAlive by default, so a dedicated drop-in is mainly useful after an older hardening change turned it off or when the directive should be managed outside the vendor file.

  2. Create a dedicated KeepAlive override in /etc/apache2/conf-available/keepalive.conf.
    $ sudo tee /etc/apache2/conf-available/keepalive.conf >/dev/null <<'EOF'
    KeepAlive On
    EOF

    Leave MaxKeepAliveRequests and KeepAliveTimeout at their current values unless the workload needs separate tuning.

  3. Enable the keepalive configuration snippet.
    $ sudo a2enconf keepalive

    If the snippet is already enabled, a2enconf reports that state and leaves the existing symlink under /etc/apache2/conf-enabled unchanged.

  4. Validate the Apache configuration before reloading the service.
    $ sudo apache2ctl configtest
    Syntax OK

    An AH00558 warning about a missing global ServerName does not block a successful syntax test.

  5. Reload the running apache2 service to apply the updated configuration.
    $ sudo systemctl reload apache2

    A reload is enough for a KeepAlive directive change because the running parent process only needs the updated configuration.

  6. Verify that Apache now advertises a persistent connection when a client requests it.
    $ curl --http1.0 -I -sS -H 'Connection: keep-alive' http://127.0.0.1/
    HTTP/1.1 200 OK
    Server: Apache/2.4.58 (Ubuntu)
    Keep-Alive: timeout=5, max=100
    Connection: Keep-Alive
    Content-Type: text/html

    Use the site hostname instead of 127.0.0.1 when the intended virtual host is not served by the default local listener, and expect the timeout and max numbers to match the active server settings on that host.