sfw/fix
Premature end of script headers high

Apache 500: End of script output before headers

A CGI or FastCGI script exited or printed body content before sending a valid HTTP header line, so Apache returns a 500.

What you see

[cgi:error] [pid 12345] End of script output before headers: index.php
... 500 Internal Server Error

What’s actually happening

The browser gets a plain 500 Internal Server Error with no detail. The Apache error_log holds the real message: "End of script output before headers" or "Premature end of script headers" naming the script. It often hits one specific page (the one doing heavy work) rather than the whole site, and may be intermittent under load when scripts time out.

Common causes

  • The script printed output — a PHP warning, a stray echo, a UTF-8 BOM, or a blank line before <?php — ahead of the HTTP headers.
  • A PHP fatal error or segfault killed the process before any headers were sent (check for "child pid X exit signal Segmentation fault").
  • The script ran past mod_fcgid's FcgidIOTimeout / FcgidBusyTimeout or Apache's Timeout and was killed mid-response.
  • A memory limit (PHP memory_limit or RLimitMEM) or the OPcache crashing tore the worker down.
  • The CGI script isn't executable, has the wrong shebang, or has Windows CRLF line endings so the interpreter never starts cleanly.

How to fix it

  1. Read the error log firsttail -f /var/log/apache2/error.log (or the vhost's ErrorLog) and reproduce the request. The line right before "End of script output before headers" — a PHP fatal, a signal, a timeout — is the actual fault. Everything else here is guided by what that line says.
  2. Hunt for output sent before headersFor PHP, check that no file has whitespace or a BOM before <?php and that display_errors isn't dumping warnings into the response. `head -c3 file.php | xxd` reveals a stray EF BB BF BOM. Set log_errors=On and display_errors=Off so notices go to the log, not the stream.
  3. Catch crashes and signalsIf the log shows "exit signal Segmentation fault (11)", a PHP extension is crashing — disable extensions one at a time (often a bad OPcache, ioncube, or xdebug build) and restart Apache. Raise memory_limit if you see "Allowed memory size exhausted" just before the premature-end line.
  4. Raise the relevant timeoutFor mod_fcgid, bump FcgidIOTimeout 120 and FcgidBusyTimeout 120 in the config; for plain CGI raise Apache's `Timeout`. For PHP-FPM behind Apache, also raise request_terminate_timeout in the pool and max_execution_time in php.ini. Restart Apache after.
  5. Fix script execendabilitychmod +x the CGI script, verify the shebang points at a real interpreter (`head -1 script.cgi`), and strip CRLF endings with `sed -i 's/\r$//' script.cgi` or dos2unix. A #!/usr/bin/perl^M will fail silently.

Stop it recurring

Keep display_errors off in production and log to a file, so a PHP notice can never leak into the response stream ahead of the headers.

Related errors