Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Apache returns 500 with Premature end of script headers

ErrorPremature end of script headers

Short answer

Apache ran your script, but the script printed no valid HTTP header before its output. The browser shows Internal Server Error. Run the script by hand to see what it prints first, then check the header and blank line, Windows line endings in the shebang line, and whether suexec rejected the run.

  • Applies to Apache 2.4, PHP 8.3
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Read the newest Apache error log lines. The script's own error message usually sits next to the Premature end line.

    sudo tail -n 30 /var/log/apache2/error.log
  2. 2Match what you see, then jump to its fix

    1. 1Running the script on the command line shows text or an error before any Content-Type lineThe script printed output before a header and blank lineFix
    2. 2The script was edited on Windows and the log also shows a 'command not found' style messageWindows line endings break the interpreter pathFix
    3. 3The error appears only on a shared host or a server where suexec is installedsuexec refused to run the programFix

What does the error look like?

  • Premature end of script headers
  • 500 Internal Server Error

Cause 1 of 3

The script printed output before a header and blank line

1Check

A CGI response must start with a header, then a blank line. Run the script directly and look at the first lines.

./script.cgi | head -n 5

2Fix

Print the header and a blank line before everything else, and remove any debug output or crash that comes first.

#!/usr/bin/env python3
print("Content-type: text/html\n")
print("Hello, World.")

3Verify

The command line run starts with the header, then a blank line.

./script.cgi | head -n 3

Confirmed by httpd.apache.org

Did this fix it?

Cause 2 of 3

Windows line endings break the interpreter path

1Check

A carriage return can be appended to the interpreter path in the first line. Look for it.

head -n 1 script.cgi | od -c | head -n 3

2Fix

Convert the file to Unix line endings, and upload scripts in text mode.

sed -i 's/\r$//' script.cgi

3Verify

Run the script again from the command line, then in the browser.

./script.cgi | head -n 3

Confirmed by httpd.apache.org

Did this fix it?

Cause 3 of 3

suexec refused to run the program

1Check

Any failed suexec check produces this same message. See whether Apache was built with suexec, then read its own log.

apachectl -V | grep -i suexec
suexec -V

2Fix

Follow the suexec log to the rule that was broken (owner, directory or file permissions) and correct it. Disable suexec only if you understand what it protects.

# suexec -V prints the path of its log file; read the newest lines of that file
suexec -V

3Verify

Request the script again and check that no new line appears in the suexec log named by suexec -V.

curl -I https://example.com/cgi-bin/script.cgi

Confirmed by httpd.apache.org

Did this fix it?

How do I stop it coming back?

  • Always run a CGI script from the command line before testing it through Apache.
  • Keep scripts in Unix line endings and save them as LF in your editor.
  • Log debug output to a file, never to the response before the header.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

Apache returns 403 when a URL contains %3F

a request like /page%3Fname=test is refused while /page?name=test works

Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.