Errors & Troubleshooting

Premature end of script headers

Updated November 2026 8,757 views 3 min read
Errors & Troubleshooting | Updated November 2026

"Premature end of script headers" is an Apache error that appears in your error log when a CGI or PHP script crashes before it can output the required HTTP headers. The browser typically sees a 500 Internal Server Error. Here are the common causes and fixes.

01. File Permissions

CGI scripts (.cgi, .pl) must be executable. Set permissions to 755:

chmod 755 script.cgi

PHP files should be 644 (not executable). If a PHP file is set to 755, some server configurations reject it.

Directories should be 755. Do not use 777 for anything.

02. Line Endings (Windows vs Linux)

If you wrote or edited a CGI script on Windows and uploaded it via FTP in binary mode, the file may have Windows-style line endings (\r\n) instead of Unix-style (\n). The extra carriage return character makes the shebang line (first line) invalid.

Fix: Re-upload the script in ASCII mode, or convert it on the server:

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

For more on transfer modes, see ASCII vs Binary.

03. Missing or Incorrect Shebang Line

CGI scripts must start with a shebang line pointing to the correct interpreter:

#!/usr/bin/perl

or for Python:

#!/usr/bin/python3

This must be the very first line of the file with no spaces or blank lines before it. Check Server Paths for the correct paths on our servers.

04. PHP Errors

A PHP fatal error (syntax error, missing file, undefined function) causes the script to crash before outputting headers. Check your error log for the specific PHP error:

  • cPanel > Metrics > Errors
  • Or check ~/public_html/error_log

See Error 500 - Internal Server Error for PHP debugging steps.

05. Output Before Headers

If your script outputs any text (even a blank line or whitespace) before the Content-type header, you get this error. In PHP, this means any content before <?php or after ?> in included files. A common culprit is a UTF-8 BOM (byte order mark) at the start of the file.

Fix: Make sure there is no whitespace before <?php, and consider omitting the closing ?> tag (which is actually recommended practice in PHP files that contain only PHP code).

Cannot Find the Cause?

If the error log does not give you enough information, open a ticket with the script path and we will check the server logs for more detail.

Open a Support Ticket

Frequently Asked Questions

What does "Premature end of script headers" mean?

It means a CGI or PHP script crashed before it could send the required HTTP headers, so Apache logs this message and the browser usually sees a 500 Internal Server Error. The script started but failed before producing valid output.

Where do I find the real cause of the error?

Check the error log at cPanel, Metrics, Errors, or ~/public_html/error_log. The line immediately before the "premature end" message almost always names the actual problem, such as a permission denied or a PHP syntax error.

My Perl or CGI script started failing after I uploaded it from Windows. Why?

Windows line endings (\r\n) break the shebang line. Re-upload the file in ASCII mode, or run sed -i 's/\r$//' script.cgi on the server. Also confirm the script is chmod 755 and starts with a correct shebang like #!/usr/bin/perl.

It happens on a PHP page, not a CGI script. What causes that?

A PHP fatal error, or output sent before the headers. Any whitespace or a UTF-8 BOM before the opening <?php tag counts as early output. Remove any leading whitespace and omit the closing ?> tag in PHP-only files.

Quick Recap

  1. Check the error log first - cPanel > Metrics > Errors
  2. CGI scripts need 755 permissions, PHP files need 644
  3. Fix Windows line endings - Upload in ASCII mode or run sed
  4. Verify the shebang line on CGI/Perl scripts
  5. No output before headers - Check for whitespace before <?php

Script error troubleshooting · Last updated November 2026 · Browse all Error articles

Share: X in Reddit f Email
  • 131 Users Found This Useful

Was this answer helpful?

Related Articles

Unable to Include Potential Exe - ModSecurity Error

Errors & Troubleshooting | Updated November 2026 The error "Unable to include potential...

Cloudflare Error 522: Connection Timed Out

Errors & Troubleshooting | Updated November 2026 Cloudflare Error 522: Connection Timed...

Error: SoftException in Application(dot)cpp:303 UID of script is small than min_uid

Errors & Troubleshooting | Updated November 2026 The "SoftException in Application.cpp:...

Gmail Send Mail As Error: 535 Incorrect Authentication Data

Email and Webmail | Updated September 2026 If Gmail bounces your message with "535 Incorrect...

How to Clean Malware from a Hacked Website (Non-WordPress)

Errors & Troubleshooting | Updated July 2026 If your Joomla, Drupal, custom PHP, or plain...



Save 30% on web hosting - Use coupon code Hosting30