Perl/CGI Premature end of script headers


I'm running a CentOS Linux system with the default apache install (apache 2) I have perl installed and the script I'm attempting to run works perfectly from the command line, but whenever I try to hit it from a browser, I get a 500 Internal server error. My error_log states "Premature end of script headers:"
The script was written with the linux command-line vi editor, so this is not a windows character issue. I've tried all kinds of verious permissions... none seem to work. I've tried several extensions, .cgi, .pl, etc. No effect. Here's a copy of the test.cgi script I'm trying to run:

#! /usr/bin/perl -w


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

use CGI;
print "hello world!";

The only thing I can figure at this point is that it's an apache config problem, but I've read their documentation on that and have checked the things they mention. (Perl path - running the script both with perl in front of the script and without perl in front of the script, etc etc etc.)

I've run out of things to try. Can anyone here offer assistance on where to look next? The script is being run in a VirtualHost with a ScriptAlias for the CGI part.

I'd very much appreciate any suggestions! Thanks!
Who is Participating?
TintinConnect With a Mentor Commented:
Your entry:

SuexecUserGroup "#501" "#501"

looks wrong to me.

Usually, you specify the username and group, eg:

SuexecUserGroup wol users

Do you really want the SuexecUserGroup entry?
When you run it from the command line, how are you doing it?

What happens if you try a CGI shell script, eg:

cat <<EOF
Content-Type: text/html

<h1>Hello World</h1>
how do you call the script in your browser?
have you configured a ScriptAlias in httpd.conf? Please post the corresponding line.
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

As long as you are using CGI, why not do the entre stuff with it. Also, you are missing the body start and end tags, the head part is missing entirely.

#! /usr/bin/perl -w
use CGI qw/:standard/;
print header,start_html ;
print "hello world!";
print end_html ;
Manav, even without any HTML tags it should never return "Premature end of script headers"

Another reason might be that the server is configured for NPH - none parsing header ..
viviotechAuthor Commented:
>When you run it from the command line, how are you doing it?

# perl -w
Content-type: text/html

hello world!

>What happens if you try a CGI shell script, eg:

The shell script also yields an internal server error.

From the error_log: "Premature end of script headers: test2.cgi" (I called the script you provided test2.cgi)

From # ll
-rwxr-xr-x    1 wol      wol            70 Apr 28 10:17 test2.cgi

Any other suggestions? Thanks for your help!
viviotechAuthor Commented:
>  how do you call the script in your browser?

I'm calling it from Mozilla 1.7 on a SuSE Linux 9.1 system. URL is something like this:

> have you configured a ScriptAlias in httpd.conf?


> Please post the corresponding line.

<VirtualHost *:*> [ip omitted for privacy reasons]
SuexecUserGroup "#501" "#501"
DocumentRoot /home/wol/public_html
ErrorLog /home/wol/logs/error_log
CustomLog /home/wol/logs/access_log common
ScriptAlias /cgi-bin/ /home/wol/cgi-bin/
<Directory /home/wol/public_html>
Options Indexes IncludesNOEXEC FollowSymLinks

Off the cuff, I don't see anything wrong with it personally.
viviotechAuthor Commented:
> Another reason might be that the server is configured for NPH - none parsing header ..

Is there any way that I can check this?

Thanks for your help guys, I really appreciate it!
> From the error_log: "Premature end of script headers: test2.cgi" (I

you should make a decission for either naming the script (with proper httpd.conf settings) or call it with its real name test2cgi
simple, isn't it?  ;-)
viviotechAuthor Commented:
Whoops. Sorry about that. is the file with the code that I posted with the origional question.

test2.cgi is the shell script code that tintin asked me to try. Both code examples had the same problem with the same error message in the logs.

Sorry for being unclear. =(
viviotechAuthor Commented:
Commenting out the Suexec entry, restarting apache, and hitting the scripts again with a browser made the problem go away. So now I just need to figure out what I *really* need for those entries and I should be good to go.

Thanks for pointing me in the right direction tintin!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.