Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
Do you know where your variables are?
 
PerlMonks  

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

( #24447=categorized question: print w/ replies, xml ) Need Help??
Contributed by frodo on Jul 26, 2000 at 15:26 UTC
Q&A  > debugging


Description:

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?

Answer: How to get over 'premature end of script headers' in Apache?
contributed by davorg

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).
Answer: How to get over 'premature end of script headers' in Apache?
contributed by le

Be sure to send correct HTTP headers:

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

Answer: How to get over 'premature end of script headers' in Apache?
contributed by Anonymous Monk

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

Answer: How to get over 'premature end of script headers' in Apache?
contributed by Anonymous Monk

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.

Answer: How to get over 'premature end of script headers' in Apache?
contributed by wardk

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

Answer: How to get over 'premature end of script headers' in Apache?
contributed by TheThinker

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.

Answer: How to get over 'premature end of script headers' in Apache?
contributed by Anonymous Monk

If you are uploading the file to a server via FTP, make sure that you are uploading as ASCII and not binary.

Answer: How to get over 'premature end of script headers' in Apache?
contributed by jundy

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".

Answer: How to get over 'premature end of script headers' in Apache?
contributed by tsayman

Another possibility is to make sure the shebang line isn't indented.

Answer: How to get over 'premature end of script headers' in Apache?
contributed by Anonymous Monk

Debian users should see if they may be affected by bug #161639.

Answer: How to get over 'premature end of script headers' in Apache?
contributed by sherab

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/.

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others perusing the Monastery: (4)
    As of 2014-04-18 11:02 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      April first is:







      Results (466 votes), past polls