एरर समाधान · सर्वर एरर

Apache "Premature end of script headers": Causes and Fixes

Apache logs "Premature end of script headers" when a CGI, PHP-CGI or FastCGI script exits before sending valid headers. Check the script, shebang, permissions and limits.

Error messagePremature end of script headers

लेखक: SiteDownCheck संपादकीय टीम · अपडेट: अक्टूबर 14, 2026 · 4 मिनट पढ़ने में

Visitors get a 500 Internal Server Error, and the Apache error log shows one of these lines, depending on which module ran the script:

[cgid:error] End of script output before headers: report.cgi
[fcgid:warn] mod_fcgid: stderr: PHP Fatal error: Allowed memory size exhausted ...
Premature end of script headers: index.php

The exact wording varies by Apache version and module, but the meaning is the same. Apache started an external program (a CGI script, PHP through CGI, suEXEC or FastCGI) and expected it to begin its output with HTTP headers, at minimum a Content-Type line followed by a blank line. The program either produced nothing, crashed first, or wrote something else.

So the real error is almost always in the line above or beside this message. Read the surrounding log, not just this line.

Step 1: read the neighbouring log lines

sudo tail -n 50 /var/log/apache2/error.log

Look for PHP fatal errors, "permission denied", "bad interpreter", "command not found", "Segmentation fault" or "killed". Control-panel hosts keep a per-site error_log; PHP's own error_log in the script directory may hold the crash.

Step 2: run the script by hand

For a classic CGI script, run it as the user Apache uses and look at the first bytes of output:

sudo -u www-data /var/www/cgi-bin/report.cgi | head

You should see something like:

Content-Type: text/html

<html>...

If you see nothing, a stack trace, or text before the header, you have found it. For PHP on CGI, run php-cgi -f index.php or php -l index.php to check syntax.

Common causes

The script prints no headers

A Perl, Python or shell CGI must emit the header and a blank line before the body:

#!/usr/bin/env python3
print("Content-Type: text/html")
print()
print("<h1>Hello</h1>")

Printing debug text before the header breaks it.

Wrong or missing shebang, or Windows line endings

A script saved with CRLF line endings makes the first line #!/usr/bin/perl\r, which the kernel cannot execute; the error log may say "bad interpreter" or "No such file or directory". Convert it with dos2unix script.cgi and confirm the interpreter path with which perl.

Not executable or wrong ownership

CGI scripts need the execute bit and must live in a directory that allows CGI (ScriptAlias or Options +ExecCGI with AddHandler cgi-script .cgi):

chmod 755 report.cgi
ls -l report.cgi

With suEXEC (common on shared hosting), Apache refuses scripts that are group-writable, owned by the wrong user, or inside a directory that is world-writable. The suEXEC log (often /var/log/apache2/suexec.log) states which check failed. Typical safe values are directories at 755 and scripts at 755 or 644 for PHP files, owned by the account user.

PHP crashed or hit a limit

A PHP fatal error, a segfault from a buggy extension, or a memory or time limit can end the process before any output. Raise memory_limit for a memory problem, see PHP allowed memory size exhausted, and check max_execution_time. Under mod_fcgid, FcgidMaxRequestLen and FcgidIOTimeout can also cut a request short.

Resource limits

RLimitCPU and RLimitMEM in the Apache config, or per-account limits such as cgroups or CloudLinux LVE, can kill a script silently. If the script works from the shell but fails under Apache, check limits and see whether the OOM killer logged anything with dmesg | grep -i kill.

Stray output or a bad include

A byte order mark or whitespace before <?php or after ?> can start the output stream wrongly in some setups. A recent plugin, theme or PHP version change often introduces it, so undo the last change first.

Step 3: confirm the fix

Retry the URL and check the status code. A 200 with the expected headers means the script is responding correctly; use the HTTP Header Checker to confirm Content-Type is present and correct, and the HTTP स्टेटस चेकर for the status. Add an अपटाइम मॉनिटर on the URL, or a cron job monitor if the script runs on a schedule, so a future crash alerts you.

अगले कदम

सामान्य प्रश्न

Is Premature end of script headers a PHP error?

Not by itself. It is Apache saying the external program ended without valid headers. PHP errors are a frequent cause, but Perl, Python and shell CGI scripts trigger it too.

Why does it work from SSH but not in the browser?

The web server runs the script as a different user with a different environment, limits and working directory. Test with sudo -u www-data or the account user.

Does it appear with PHP-FPM?

Rarely. FPM failures usually appear as 502 or 503 with proxy_fcgi messages. This error is typical of CGI, suEXEC and mod_fcgid setups.

Can a plugin cause it?

Yes. A plugin that fatals early in WordPress, for example, can end the PHP process before headers are sent. Disable the newest plugin first.

Get notified when this fix updates

Plus occasional practical tips on uptime and monitoring, nothing else. Unsubscribe anytime.

यह पेज सामान्य जानकारी है, जिसे हमारी संपादकीय टीम ने लिखा है और दस्तावेज़ीकरण बदलने पर अपडेट किया जाता है। सर्वर और प्रदाता अलग-अलग होते हैं, इसलिए बदलाव पहले सुरक्षित तरीके से जाँचें। देखें: संपादकीय नीति और अस्वीकरण.