"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.
Permissions, line endings, or PHP errors
Check the error log first: cPanel > Metrics > Errors. The line immediately before the "premature end" message usually tells you the actual problem (permission denied, syntax error, etc.).
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 TicketFrequently 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
- Check the error log first - cPanel > Metrics > Errors
- CGI scripts need 755 permissions, PHP files need 644
- Fix Windows line endings - Upload in ASCII mode or run sed
- Verify the shebang line on CGI/Perl scripts
- No output before headers - Check for whitespace before <?php
Script error troubleshooting · Last updated November 2026 · Browse all Error articles
