Apache returns 500 with Premature end of script headers
Premature end of script headersShort 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.
Fix it fast
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.log2Match what you see, then jump to its fix
- 1
Running 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
The script was edited on Windows and the log also shows a 'command not found' style messageWindows line endings break the interpreter pathFix - 3
The error appears only on a shared host or a server where suexec is installedsuexec refused to run the programFix
- 1
What does the error look like?
Premature end of script headers500 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 52Fix
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 3Confirmed by httpd.apache.org
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 32Fix
Convert the file to Unix line endings, and upload scripts in text mode.
sed -i 's/\r$//' script.cgi3Verify
Run the script again from the command line, then in the browser.
./script.cgi | head -n 3Confirmed by httpd.apache.org
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 -V2Fix
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 -V3Verify
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.cgiConfirmed by httpd.apache.org
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
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack 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 fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fixBack end
AWS SDK for PHP rejects requests: the signature is from the future
Signature not yet current: {date} is still later than {date}
AWS compares the timestamp in your signed request with its own clock.
2 causes, each with a fixBack end
You switched PHP versions with Homebrew, but php -v still shows the old one
brew link php@8.3 prints an error or does nothing
Homebrew can have several PHP versions installed, but only one is linked into the prefix, and your shell uses the first php on PATH.
3 causes, each with a fix