ok... i've compiled php4 as a cgi on red hat 6.2 and I'm attempting to use it with apache.
during the install I configured the binary with:

--enable-discard-path
--enable-force-cgi-redirect

everything compiled fine and at the shell my test cgis work w/o a problem. via apache however I get an error to the effect of "premature end of script headers."

I've just combed through the archives and it seems like 20 different people have reported this error yet there is no solution.

other cgi's work w/ apache w/o any problems and the following lines are in my httpd.conf file:

AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
Action application/x-httpd-php /usr/local/bin/php

any ideas?

also, when php is compiled as a cgi where does the php.ini end up and is there anything I need to put in it in order to make it play nice w/ apache?

any help greatly appreciated

    this is just an off the wall guess, but have you tried setting the headers using your php script file?

    example:
    #################
    Content-Type: text/html

    gotta have the headers content type then two enters before the content of the page.

    #################

      thanks for the suggestion... just tried it and it didnt work. very frustrating.

      as I mentioned, this error has popped up ALOT on the php mailing lists but I have yet to see anybody actually address the problem.

      furthermore the folks at php.net have verly poorly documented (actually there doesnt seem to be ANY documention of) the entire process of getting apache + php as a cgi working smoothly.

      any other suggestions or theories are very much appreciated.

      regards,
      d

        if you're running apache, then you might have suexec. could it possibly be that you haven't set the permissions correctly on your script, and suexec is returning the "premature end of script headers" because of the permission error?

        try setting your script permissions higher.

          a month later

          I share your frustration. I've been scanning the web for the last 24 hours, and nobody addresses this. Did you ever find a solution, because it may be the same one I'm searching for to fix my issue. Let me know if you found something out, and I'll let you know if I come up with something. Thanks.

          BB

            Also experienced the much dreaded "premature end of script headers" message.

            Much to my dismay, after nearly an hour of perplexing troubleshooting--to no avail-- I discovered that the file being called did not exist in the directory where it was being called. (Simple operator error.)

            Moved the file to the correct LOCATION (i.e. directory) that was being called (a .php file) and apache stopped complaining (i.e. it worked!)

            My experience would appear to equate to:
            "FILE NOT FOUND" message equivalents -- much more appropriate here than the "premature end of script headers" message. Go figure!

            C'est la vie!

              4 days later

              I was getting the same error and finally traced it back to what seems to have been the problem. It started when I added the DBG and the Java extensions to PHP 4. I commented out those lines in php.ini and I don't get the error anymore

                I followed the suggestion below and my problem has gone. I have yet to find out why exactly it happen.

                From: http://www.vbulletin.com/forum/showthread.php?threadid=6457

                They shouldn't be in your cgi-bin directory, since the server will try to execute all files in there with the perl engine. Try putting them in your root public directory, like public_html or whatever it is.

                -jim

                  a month later

                  Jeff is right. In my case the "premature end
                  of script headers" problem was also caused
                  by bad file location. It seems that the right
                  translation of "premature end of script headers" announcement is "the file is missing"

                  dav

                    24 days later

                    I was having the same problem with the simplest "hello.pl" script.

                    None of the usual suspects were in error.

                    I changed the first line to add "-w" to the invocation of the Perl interpreter.

                    I changed it from:

                    #!/usr/bin/perl

                    TO:

                    #!/usr/bin/perl -w

                    That seemed to work. The Apache log file claimend that I had: Premature end of script headers FOLLOWED BY this error:

                    (2)No such file or directory: exec of /home/htdocs/xxxxx/cgi-bin/hello.pl failed

                      2 months later

                      I was getting this error because of an infinitely recursive function.

                      I found out the problem was in the script by commenting out all the code, and then the file would display. After that, I uncommented sections at a time until I got the error again, then I knew where the problem was.

                      Anna

                        2 months later

                        YES!!!!!

                        Yay! Thank you! Thank you! Thank you! Thank you! Thank you! I just got my php apache + win95 working thank to your message!

                        :-)

                        premie end of script headers indeed ...

                          7 days later

                          I always assumed that php files had to be in the cgi-bin directory, but this seems to be incorrect! When I placed my test.php file in the htdocs directory, it worked absolutely wonderfully!! Cheers

                            2 months later

                            Include the path to the apache directory in your path-specification residing in autoexec.bat or simply copy ApacheCore.dll and Win9xConHook.dll from the Apache directory to c:\windows\system.

                            Apparently Perl, PHP and the likes don't know where to find aforementioned files when Apache invokes them (Perl, PHP, etc) to process a script (and please, let's not dwell on the fact why Apache simply generates a 'premature end of script headers' error instead of the enlightening 'file not found', shall we).

                            Hopefully, this will be the end of at least one of the many (I'm sure) inconveniences regarding the usage of php (and the likes) and apache...

                              9 days later

                              Here's a fun one. I was getting the "Premature end of script headers" running php4 as a cgi, even going so far as to copy over scripts that worked to the names of the files that gave the message, to no avail. If I switched from .cgi extension to .php (which on this server only runs php3), the scripts also worked, but I wanted to use php4 for the implementation of session variables. Turned out I had given too MUCH permissions to the aberrant cgi/php4 files. Switching from 775 to 755 (no write privileges for group) fixed the problem. I have no idea why that would be.

                                7 days later

                                LOL I just realized the problem I was having.

                                I moved a script from /dir to /dir2, & kept my browser pointed at /dir/script.php, instead of looking for /dir2/script.php.

                                :-P

                                /me smacks own forehead

                                  16 days later

                                  In your php.ini (in Windows, I assume there is an equivalent file in other OS's), there is a directive named doc_root.

                                  I think it defaults to something like:
                                  doc_root = "C:\Program Files\Apache Group\Apache\htdocs"

                                  I used an Alias directive for Apache to put my scripts and html in a different directory, so this tripped up PHP. Just comment this line out to allow PHP to execute scripts from any directory, or specify the exact directories you want and it may fix this problem.

                                    Vivek Raman,

                                    Your suggesstion of commention doc_root worked for me too...Thanks a lot...spent lots of hrs fixing this stuff.

                                    sanjay

                                      19 days later

                                      commenting out the doc_root worked for me as well.

                                      thanks

                                      tom

                                        5 days later

                                        I was experiencing the same problem when using php3 with apache.
                                        The problem was due to a misplaced php3.ini file. This file needs to be put in a directory which is part of the PATH-Variable. Otherwise the php-processor is not able to find its configuration and exits. Apache is responding with "premature end of script headers".