"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 TicketQuick 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 March 2026 · Browse all Error articles
