GoAdmio

keepalivetimeout

Read or set Apache's KeepAliveTimeout directive and reload the web server

On this page: Overview · Quick start · Syntax · Arguments · Output · Examples · Troubleshooting · Details · See also


Overview

/go/keepalivetimeout changes the Apache setting KeepAliveTimeout in Apache's main configuration file. It is the number of seconds Apache keeps an idle connection open, waiting for the visitor's next request. The setting matters only while keepalive is On.

After changing the file, the command reloads PHP-FPM and the web server.

The file it edits depends on the system:

System File
AlmaLinux, Rocky, CentOS, Red Hat, Fedora /etc/httpd/conf/httpd.conf
Debian, Ubuntu /etc/apache2/apache2.conf

The setting applies to the whole server. It has no effect on Nginx.


Quick start

# Check the current value
/go/keepalivetimeout return

# Set it to 3 seconds
/go/keepalivetimeout 3

# Go back to the default of 5 seconds
/go/keepalivetimeout

Syntax

/go/keepalivetimeout [seconds]
/go/keepalivetimeout return
Form Behavior
/go/keepalivetimeout Sets KeepAliveTimeout 5 and reloads.
/go/keepalivetimeout <value> Sets KeepAliveTimeout <value> and reloads.
/go/keepalivetimeout return Prints the current value, then exits with code 1.
/go/keepalivetimeout --noreload Meant to set 5 without reloading. The reload still happens. See Notes for maintainers.

[!WARNING] The value is not checked, and every argument you pass becomes part of the value. /go/keepalivetimeout 3 --noreload writes KeepAliveTimeout 3 --noreload, which makes the Apache configuration invalid.


Arguments

Argument Required Default Notes
[seconds] No 5 A whole number of seconds. Apache also accepts a number with an ms suffix. All arguments are joined with spaces.
return No — Prints the current value. Not case-sensitive.
--noreload No — Only recognized as the sole argument, and currently has no effect on the reload.

The command has no other options.


Output

Outcome What you get
return The current value, such as 5. Empty if it can't be read. Exit code 1.
Value set KeepAliveTimeout updated successfully., then the output of reload.
Unsupported operating system Unknown operating system. The exit code is 0.

The confirmation line is printed even when the file wasn't changed.

Logging

Every run is appended to /var/log/go/keepalivetimeout.log.


Examples

# Read the current value
/go/keepalivetimeout return
# Close idle connections after 2 seconds
/go/keepalivetimeout 2
# Back to the default
/go/keepalivetimeout
# Check the line in the configuration file (Debian and Ubuntu)
grep -n "KeepAliveTimeout" /etc/apache2/apache2.conf

Troubleshooting

Symptom Likely cause Fix
Apache doesn't reload, with an error about KeepAliveTimeout An invalid value was written, for example 3 --noreload. Fix the line in the file so that it holds a number, check it with apachectl configtest, then run reload.
--noreload did not stop the reload The check for the flag has a typo. Expected for now. See Notes for maintainers.
return prints an empty line The file has no KeepAliveTimeout line (Apache then uses 5), or the line is indented or uses a tab. Check the file by hand.
No effect on a server that uses Nginx The setting is for Apache only. Change Nginx's keepalive_timeout instead.
The value has no effect keepalive is Off. Turn KeepAlive on.
The server runs out of connections or memory under load The timeout is long. Set a lower value, such as 2 to 5.
Permission denied when adding the line The command wasn't run as root. Run it as root.
/etc/apache2/vhost/port.conf disappeared on Debian The script deletes that file on Debian on every run. See Notes for maintainers.

Details

What it does

  1. Logs the call, and finds Apache's main configuration file for the operating system. On Debian it also deletes /etc/apache2/vhost/port.conf. For an unknown system it prints Unknown operating system. and stops.
  2. Reads the value from the first line that contains KeepAliveTimeout followed by a space.
  3. For return: prints the value and stops.
  4. Chooses the new value: 5 if there is no argument or the only argument is --noreload, otherwise all arguments joined together.
  5. If the file has an active KeepAliveTimeout line, replaces the value on every such line. Otherwise adds KeepAliveTimeout <value> to the end of the file.
  6. Prints KeepAliveTimeout updated successfully.
  7. Reloads PHP-FPM and the web server with reload. This step is always run.
flowchart TD
    A["keepalivetimeout value"] --> B["Find Apache config file"]
    B --> C{"Known system?"}
    C -- "No" --> X["Unknown operating system, stop"]
    C -- "Yes" --> D{"return?"}
    D -- "Yes" --> E["Print current value, exit 1"]
    D -- "No" --> F["Set value: 5 if none, else the arguments"]
    F --> G{"KeepAliveTimeout line exists?"}
    G -- "Yes" --> H["Replace its value"]
    G -- "No" --> I["Append the line"]
    H --> J["reload"]
    I --> J

Notes for maintainers

This command shares its template with keepalive. The problems below apply to both, apart from the ones noted.

Issue Why it matters Suggested fix
The check for --noreload tests the text "@" instead of "$@" The pattern never matches, so the reload always runs. Test "$@", and parse the flag properly.
--noreload is recognized only as the sole argument, and otherwise becomes part of the value 3 --noreload writes an invalid line and can stop Apache from reloading. Remove flags from the arguments before building the value.
The value is not validated, and goes straight into sed A non-number breaks Apache's configuration, and characters such as / or & break the sed command. Accept a whole number (optionally with ms), and run apachectl configtest before the reload.
The value is read with cut -d' ' -f2 An indented line or one with a tab returns an empty value. Use awk '$1=="KeepAliveTimeout" {print $2}'.
The read pattern is not anchored Commented-out lines can be matched. Use ^[[:space:]]*KeepAliveTimeout[[:space:]], as the write pattern does.
On Debian the script deletes /etc/apache2/vhost/port.conf on every run An unrelated, unexplained deletion, and only on Debian, not Ubuntu. It looks left over from another script. Remove the line, or explain it.
Unknown operating system exits with 0 A failure looks like success. (Unlike keepalive, this command does stop.) Use exit 1.
The line is appended with >> without sudo, and there is no root check Fails when not run as root. Re-run through sudo at the top, like most other commands.
The command edits Apache's main file Package upgrades can replace it. Write a separate file in conf.d (RPM) or conf-available (Debian).
The success message always prints, and return exits with 1 A run that changed nothing still looks successful, and return looks like a failure. Report what changed, and use exit 0.
maxkeepaliverequests probably uses the same template The same problems can exist there. Check it and fix all three together.

See also

Command Description
keepalive Read or set Apache's KeepAlive directive
maxkeepaliverequests Read or set Apache's MaxKeepAliveRequests
reload Reload PHP-FPM and the active web server
web Restart or reload the active web server
checkapache Validate the Apache configuration
apache Convert a GoAdmio server from Nginx to Apache