Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

PERL CGI - Premature end of script headers:

Posted on 2002-12-19
18
Medium Priority
?
4,199 Views
Last Modified: 2013-11-18
I've tried all kinds of permissions combinations and simplified the program as much as possible.  I just can't seem to find what's keeping perl from running as a CGI.

I'm getting this error in the log at:
/home/httpd/vhosts/northrockbp.com/logs/error_log

Premature end of script headers: /home/httpd/vhosts/northrockbp.com/cgi-bin/hello.pl

Here's the script I'm using:
#!/usr/bin/perl -w
print "Content-type: text/html\n\n";

print "<html><head>\n";
print "<title>Hello, world!</title></head>\n";
print "<body><h1>Hello, world!</h1></body></html>\n";

The permissions on the file are:
-rwxr-xr-x    1 northroc psacln        184 Dec 19 09:53 hello.pl

The permissions on the cgi-bin are:
drwxr-xr-x    2 northroc psacln       4096 Dec 18 16:36 cgi-bin

The perl location is:
# which perl
/usr/bin/perl

The output from the command line is:
# perl hello.pl
Content-type: text/html

<html><head>
<title>Hello, world!</title></head>
<body><h1>Hello, world!</h1></body></html>

From the browser I'm getting the standard:
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request...Apache/1.3.22 Server at www.northrockbp.com Port 80
0
Comment
Question by:andersongrant24
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 3
  • +5
18 Comments
 
LVL 10

Expert Comment

by:rj2
ID: 7608160
Try to upload it in ASCII mode, not binary mode.
0
 

Author Comment

by:andersongrant24
ID: 7608173
Thanks, rj2 but in this case I'm writing directly on the server.  So it's not an FTP issue either.
0
 
LVL 2

Expert Comment

by:serg111
ID: 7608607
Second line of script should be empty:

not


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


but

#!/usr/bin/perl -w

print "Content-type: text/html\n\n";
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

by:seesik
ID: 7608611
works for me. try ./hello.pl just to see if you've got a shiesty linefeed on the hashbang line. vi should show hat m
's (^M) if so.
0
 
LVL 1

Expert Comment

by:seesik
ID: 7608623
serg111, what the hell should the second line of your script be empty for? that's utter b.s.
0
 

Author Comment

by:andersongrant24
ID: 7608635
serg111 - no luck

seesik - what do you mean exactly.  I'm not that familiar with vi
0
 
LVL 1

Expert Comment

by:seesik
ID: 7608673
you're using the simplest possible example, so we're not looking for a syntax error. i'm not sure what platform/editor you're using (some nix variant obviously), but there's a pretty common problem w/ symptoms similar to yours. different platforms use different end-of-line characters, special characters that most editors don't display. we're looking for an invisible trailing \r\n after your hashbang line. the easiest thing to do to ensure you don't have that problem would be to just delete and retype that "#!/usr/bin/perl -w" line again.

did ./hello.pl work?

tbc...
0
 
LVL 1

Expert Comment

by:seesik
ID: 7608681
serg111, what the hell should the second line of your script be empty for? that's utter b.s.
0
 
LVL 5

Expert Comment

by:Peewee
ID: 7608906
andersongrant24,

this can be only one of a few problems?

A:
syntax error  - No
lines endings - Possible
perl location - Possible
apache server error - Possible
upload problem - No

your sntax is fine and this basic script works fine on my windows box under apache server.  This does not mean it will work under linux due to line endings. To get rid of any line endings at the unix prompt type dos2unix filename and then chmod 777 filename.

retest and see what happens?

if still no good, check your path to perl ie your first line, are you sure this pointing to perl.  if still no luck check your apache logs and see what the error message is:

ie my logs are at:

tail -f /etc/httpd/logs/error_log  

and then run the script in the browser again check the error, report back with it.


one final question, you have configured cgi on your server haven't you, you can check by looking in the conf file, mime is at /etc/httpd/conf/httpd.conf  


let me know if you need more help.
regards Peewee




0
 

Author Comment

by:andersongrant24
ID: 7609078
Peewee

syntax error  - No
lines endings - Possible - it's not this, I created the file on Linux, I double checked it all, even ran dos2unix
perl location - Possible - the "which perl" and "whereis perl" both show it in the right place
apache server error - Possible - is possible
upload problem - No

We recently had somebody upgrade PHP and he may have made some changes to the config files.  I don't know.  What specifically should I be looking for in the httpd.conf file.  I found it at /etc/httpd/conf/httpd.conf

I searched for CGI in there and there was a ExecCGI that was not specified under Options.  I specified it and restarted apache but no luck.

the ScriptAlias says
ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
and I tried putting
ScriptAlias /cgi-bin/ "/home/httpd/vhosts/northrockbp.com/cgi-bin/"
but no luck

Where exactly would be the directive to allow CGI scripting?
0
 
LVL 5

Accepted Solution

by:
Peewee earned 555 total points
ID: 7609301
andersongrant24,

eliminate all the other options before looking at the server.

but:

ScriptAlias allows cgi scripts to execute in ther specified directory only, ie outside this and cgi will not work.

and:

AddHandler cgi-script .cgi

would allow file scripts with .cgi extension to be forced as CGI statements..


regards Peewee
0
 

Author Comment

by:andersongrant24
ID: 7609407
I did the AddHandler thing like this
AddHandler cgi-script .cgi
AddHandler cgi-script .pl

and now the script runs if I run it out of ...com/hello.pl but it won't run out of ...com/cgi-bin/hello.pl

funny huh.  

That tells me that the script is structured fine and also that perl is working fine.  And the fact that I'm getting a 500 error and not a 404 error tells me that the script is being found.  But for some reason it just won't run CGI in the CGI bin.
0
 

Author Comment

by:andersongrant24
ID: 7609444
I tried this for the script alias.  Is this right?  I just changed the option to ExecCGI and changed the paths from the default server CGI-BIN

    # Specify CGI-BIN for NorthrockBP
    ScriptAlias /cgi-bin/ "/home/httpd/vhosts/northrockbp.com/cgi-bin/"

    <Directory "/home/httpd/vhosts/northrockbp.com/cgi-bin">
        AllowOverride None
        Options ExecCGI
        Order allow,deny
        Allow from all
    </Directory>
0
 
LVL 48

Expert Comment

by:Tintin
ID: 7609779
I'll make a fair bet that the problem is buffering.  "Premature end of script headers" means the script has not outputted the correct headers.

Try this:

#!/usr/bin/perl

$|=1;  # Turn buffering off.

print "Content-Type: text/html\n\n";

print "<html><head>\n";
print "<title>Hello, world!</title></head>\n";
print "<body><h1>Hello, world!</h1></body></html>\n";
0
 

Author Comment

by:andersongrant24
ID: 7609980
I used an AddHandler and got all my CGI scripts working but they are not working in the CGI-BIN (everywhere but the CGI-BIN, go figure)

Thanks for the help PeeWee
0
 
LVL 5

Expert Comment

by:Peewee
ID: 7612577
andersongrant24,

glad to be of help

regards Peewee
0
 

Expert Comment

by:dagg
ID: 7907802
I just ran into this exact problem and it turned out
my webserver provider is using "suexec" with Apache.
When that option of Apache is turned on, you have
to make sure of two main things:

1. Your CGI script is _not_ group or world writeable.

2. The directory that the CGI script is in is _not_
  group or world writeable.

After I ran:

  chmod go-w cgidir/myscript.pl

and

  chmod go-w cgi-dir/

my CGI script ran perfectly.

More info about "suexec" is here: http://www.apache.org/docs/suexec.html
0
 
LVL 6

Expert Comment

by:ren_b
ID: 10368386
this is indeed an old paq. anyway i came across this problem and it had to do with my text encoding when i saved it. the settings on my text editor was set to save in unicode, and changing it back to the local encoding (mac roman if on mac), fixed it.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question