I get a \"Premature end of script headers: contactform.cgi\" error message when running the below script. What frustrates me is that I ran this as a .php on another server
After many diff's, this was what was missing from the httpd.conf
file on the server in question:
AddHandler php5-script .php
Solved the issue.
I tried all of the above but found out it was a missing windows compiler.
Downloading and installing this fixed the issue. To see if this is your problem, try to run PHP from command line.
msvcr110.dll is missing from computer error while installing PHP
Check your line endings! If you see an error about the file not being found, followed by this "premature of end headers" error in your Apache log - it may be that you have Windows line endings in your script in instead of Unix style. I ran into that problem / solution.
I bumped into this problem using PHP-FPM and Apache after increasing Apache's default LimitRequestFieldSize and LimitRequestLine values.
The only reason I did this (apache says don't mess) is because Yii2 has some pjax problems with POST requests. As a workaround, I decided to increase these limits and use gigantic GET headers.
php-fpm barfed up the 500 error though.
In my case it was a cache directory that wasn't so big, only 17MB but duo to the file structure it took forever to be accessed by the website and was producing that error after max_execution_time was reached.
I renamed the directory to cache-BK and created a new cache directory with the same permissions and that solved the problem.
The "Premature end of script headers" error message is probably the most loathed and common error message you'll find. What the error actually means, is that the script stopped for whatever reason before it returned any output to the web server. A common cause of this for script writers is to fail to set a content type before printing output code. In Perl for example, before printing any HTML it is necessary to tell the Perl script to set the content type to text/html, this is done by sending a header, like so:
print "Content-type: text/html\n\n";
(source; http://htmlfixit.com/cgi-tutes/tutorial_Common_Web_dev_error_messages_and_what_they_mean.php#premature