http://www.perlmonks.org?node_id=24447

frodo has asked for the wisdom of the Perl Monks concerning the following question: (debugging)

Scenario: running web server (Apache) and web browser on the same machine. (Linux) Perl scripts in under cgi-bin.

With one script in particular, I submit a form, and get "Error 500 - Internal server error". In the Apache error log file I see "Premature end of script headers". I checked the script on Windows Perl Builder; it works there, with no problems. Also, most of the other Perl scripts in cgi-bin run with no problem.

Could the problem be with the Apache configuration?

Originally posted as a Categorized Question.

  • Comment on How to get over 'premature end of script headers' in Apache?

Replies are listed 'Best First'.
Re: How to get over 'premature end of script headers' in Apache?
by davorg (Chancellor) on Jul 26, 2000 at 15:36 UTC

    This message says "your script broke and it didn't send any HTTP headers before sending the error messages". Some suggestions for further investigation:

    • Check the error message that was written in your apache error log.
    • Switch off output buffering (set $| to a true value) to ensure that headers get out before your errors.
    • Put use CGI::Carp qw(fatalsToBrowser) to make debugging easier (but remove it before putting the script live).
Re: How to get over 'premature end of script headers' in Apache?
by le (Friar) on Jul 26, 2000 at 15:32 UTC

    Be sure to send correct HTTP headers:

    print "Content-type: text/html\n\n";
    or
    use CGI; print header();

Re: How to get over 'premature end of script headers' in Apache?
by Anonymous Monk on May 26, 2004 at 09:41 UTC

    If you developed this script on Windows, it's possible that the script file has non-UNIX line endings. (The perl interpreter can handle them, but the shebang line is interpreted by the shell, and is not tolerant of incorrect line endings.) If this is the problem, the script may terminate with an error right at the shebang line.

    When you bring a script over to UNIX from other OSes, check the file format, and correct line endings if necessary. For example, using vi, when you open the script the file format is displayed at the bottom of the window. To force the file to UNIX format, enter the vi command

    :set fileformat:unix

      You can also use cat -A to check the line endings. If you see anything but #!/usr/local/bin/perl$, you've got the wrong line endings, and need to use dos2unix or perl -pi -e 's/\r\n/\n/g' foo.pl.
Re: How to get over 'premature end of script headers' in Apache?
by Anonymous Monk on Oct 03, 2003 at 07:58 UTC

    Make sure there's an empty line after the HTTP headers (such as "Content-type:") and before the body text. For example, print "Content-type: text/html\n\n";

    Forgetting that empty line will result in a "500 Server Error" being sent to the client, and a "Premature end of script header" message being written to the server error log, so it could be the source of your problem.

Re: How to get over 'premature end of script headers' in Apache?
by wardk (Deacon) on Aug 21, 2002 at 20:36 UTC

    For debugging purposes, you can try running the script from the command line; then you will see exactly what the program is outputting.

    If using CGI.pm, you can pass in any required parameters to replicate form input:

    scriptname fee=fi fo=fum foo=bar

      That will definitely help, but you need to make sure that the environment is identical to the one the webserver is using. Webserver accounts tend to be very restricted, moreso than human accounts.

      Being right, does not endow the right to be rude; politeness costs nothing.
      Being unknowing, is not the same as being stupid.
      Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
      Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

Re: How to get over 'premature end of script headers' in Apache?
by TheThinker (Initiate) on Jan 27, 2005 at 19:44 UTC

    This can be caused by the suexec wrapper in Apache. Check the suexec logs for the real error message, as the Apache log will only show a generic message.

Re: How to get over 'premature end of script headers' in Apache?
by Anonymous Monk on Apr 13, 2004 at 01:25 UTC
    If you are uploading the file to a server via FTP, make sure that you are uploading as ASCII and not binary.
Re: How to get over 'premature end of script headers' in Apache?
by jundy (Initiate) on Apr 01, 2005 at 01:07 UTC

    This may seem a little obvious, but make sure that your script produces valid output no matter what the input. (I say this from first-hand experience ;-) Even if you think that you have thought of every conceivable scenario, make sure that you have a default behavior "just in case".

Re: How to get over 'premature end of script headers' in Apache?
by Anonymous Monk on Aug 21, 2002 at 17:26 UTC
    Make sure you check /usr/local/bin/perl or whereever your perl file is and make sure that it is executable by the web browser.

    Originally posted as a Categorized Answer.

Re: How to get over 'premature end of script headers' in Apache?
by Anonymous Monk on Jun 14, 2004 at 20:48 UTC
    Make sure you made the script executable!

    Originally posted as a Categorized Answer.

RE: How to get over 'premature end of script headers' in Apache?
by Mushy (Scribe) on Aug 16, 2000 at 23:54 UTC
    That only happens when your script had an error in it and perl was not able to compile it successfully. Always test your script first before deploying it as a CGI. perl -c as a minimum.

    You could also look at CGI.pm 's fatals_to_browser define.

Re: How to get over 'premature end of script headers' in Apache?
by tsayman (Initiate) on Jun 04, 2007 at 21:19 UTC
    Another possibility is to make sure the shebang line isn't indented.
Re: How to get over
by frodo (Initiate) on Jul 26, 2000 at 16:11 UTC

    I always do like this

    #!/usr/bin/perl print "Content-type:text/html\n\n"; #html - document itself

    It's really shame for me not to use CGI.pm. But I'm too new in Perl programming.

    I've checked all my servers log - and only one error is there - "Premature and of script headers". And sometimes I get from perl errors that I'm trying to write on closed handler while it's full nonsense!!!But still he(perl) is writing in them . Bur this is not the main problem. Thank's to davorg - I'kk try everything he said about

    Originally posted as a Categorized Answer.

Re: How to get over 'premature end of script headers' in Apache?
by Anonymous Monk on Jul 02, 2004 at 15:21 UTC
    Debian users should see if they may be affected by bug #161639.
Re: How to get over
by frodo (Initiate) on Jul 26, 2000 at 16:13 UTC

    I always do like this

    #!/usr/bin/perl print "Content-type:text/html\n\n"; #html - document itself

    It's really shame for me not to use CGI.pm. But I'm too new in Perl programming.

    I've checked all my servers log - and only one error is there - "Premature and of script headers". And sometimes I get from perl errors that I'm trying to write on closed handler while it's full nonsense!!!But still he(perl) is writing in them . Bur this is not the main problem. Thank's to davorg - I'll try everything he said about

    Originally posted as a Categorized Answer.

Re: How to get over 'premature end of script headers' in Apache?
by Anonymous Monk on Jul 27, 2000 at 08:09 UTC

    Re: How to get over 'premature end of script headers' in Apache?

    Originally posted as a Categorized Answer.

Re: How to get over 'premature end of script headers' in Apache?
by TheThinker (Initiate) on Jan 18, 2005 at 16:57 UTC
    I had this problem on my site http://www.24by7mall.com and I found it was caused, believe it or not, by a corrupted copy of PERL. Apache was unable to run it successfully. I reinstalled PERL and it worked fine. This was a Deb distro FYI. Hope this helps someone. Took me 2 days to track it down.

    Originally posted as a Categorized Answer.

Re: How to get over 'premature end of script headers' in Apache?
by sherab (Scribe) on Mar 13, 2014 at 14:32 UTC
    This works on CentOS:
    chcon -R -t httpd_sys_content_t /usr/local/web/cgi-bin
    It changes security context recursively on /usr/local/web/cgi-bin/.
Re: How to get over 'premature end of script headers' in Apache?
by mrjcleaver (Initiate) on Jan 21, 2005 at 09:27 UTC
    My hosting provider mandates that neither the script nor the directory can be group or other writeable. If they are I get the error.

    Originally posted as a Categorized Answer.