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
u forgot to add proper content-type header in the response which is a must have http header when hosting on apache2
use
print "Content-type:text/html\r\n\r\n"
or
header('Content-Type: text/html');
Error can be caused by various issues. for more info check suexec or fcgi logs. For example if suexec has wrong user and permssion it can cause the error to occur to solve try
chgrp WEBGROUP /usr/local/apache2/bin/suexec
chmod 4750 /usr/local/apache2/bin/suexec
In my case I had to change
#!/usr/bin/php
to
#!/usr/bin/php-cgi
It was a file permission issue.
All files on my website were set to a permission level of '644.' Once I changed the permission level to 705 (chmod 705) everything worked. Note that I changed it to 705, but 755 will also work. I also changed the folder it was in to 701 (to hide it, but still be executable by the server).
Aside: I still don't understand why my .PHP file worked on the other server when it was probably set to 644?? How could Apache execute the script without world permission?? Does Apache not need world permission?? Just a few questions I still have...
It is to do with file permissions and it happens on systems that have the suphp(usually cpanel/whost servers) installed.
Removing the write permission from anyone else but the owner(644|600) for the php files will fix the issue. That is how i got it fixed.
I hope this helps.
I experienced this problem today, but unfortunately none of the suggestions here helped. The only problem was that I didn't see ANY errors.. I literally had to do an strace -p <process_id>
on the Apache thread to spot the headers being written and Apache crashing on the next line; Somewhere in my PHP code I was setting a header with over 12KB of data.
The lesson here is that in some cases, Apache crashing with a HTTP error 500 - Premature end of script
-failure can be the result of having too long or overflowing HTTP headers.
Debug your headers for length if you have this same problems because most (if not all) web servers have HTTP header limits.
PS: This reply has some info on header sizes.