chakkaln has asked for the wisdom of the Perl Monks concerning the following question:

Hi All I am very new to the area of CGI (on top I am a biologist try to develop a web page for my results) hence some of the questions may seem to be trivial and that I would have dome something really silly. To train my self first, I wrote a simple perl cgi script which can display an image (dynamically generated using perl GD module) when opened in the browser. After discussing some of the basic stuff in this forum, I was finally able to set up the web server running but still not successful in executing my script. When I run the script from the browser (, I get an 500 internal server error with the folloing message in the error logs
[Fri Jul 29 12:28:44 2005] [error] (8)Exec format error: exec of /home +/nagesh/apache/cgi-bin/simpleImage.cgi failed [Fri Jul 29 12:28:44 2005] [error] [client] Premature end of + script headers: /home/nagesh/apache/cgi-bin/simpleImage.cgi
When I run the same script from the command line, it generate the binary for the image. Does this mean that my server configuration is not right?


my $LIB = defined $ENV{LD_LIBRARY_PATH} ? $ENV{LD_LIBRARY_PATH} : ""; $ENV{LD_LIBRARY_PATH} = "/usr/lib:$LIB"; #!/usr/local/bin/perl use strict; use GD; use CGI; # This creates a Web GIF image on the fly # create a new image $im = new GD::Image(1000,100); # allocate some colors $white = $im ->colorAllocate(255,255,255); $black= $im ->colorAllocate(0,0,0); $red = $im ->colorAllocate(255,100,0); $blue= $im ->colorAllocate(0,150,255); $yellow= $im ->colorAllocate(255,200,0); $pink= $im ->colorAllocate(150,0,150); $im->filledRectangle(10,5,990,15,$pink); $im->arc(10,10,10,10,0,360,$black); $im->fill(10,10,$white); $im->arc(990,10,10,10,270,90,$black); $im->fillToBorder(990,10,$black,$pink); # Convert the image to GIF and print it on standard output #binmode STDOUT; print "Content-type: image/gif\n\n"; print $im->gif;
I made the code executable by chmod 755 simpleImage.cgi

This problem does not occur when I tested with a simple cgi script to check whether the server is able to execute the .cgi script and indeed it was working fine as I got some display on the browser. test.cgi #!/usr/local/bin/perl -w use strict; use CGI; my $cgi = new CGI; print $cgi->header(-type => 'text/html'); print "This is a test message"; Thanks very much in advance. Nagesh

Update: Got the problem fixed. Thanks to all of you guys who helped out. See you all very soon with more problems Cheers

Replies are listed 'Best First'.
Re: Problem running cgi script (Exec format error)
by Anonymous Monk on Jul 29, 2005 at 03:13 UTC
    It looks like you may be taking the advice given in Re: Trouble with Perl CGI script a bit too literally. The lines there should be at the top of the script, but still underneath the line
    This is because when you are executing a script as an executable it needs to start with the shebang line. However, I'm not so sure this will actually help you. I think you might want to reread the whole thread in your earlier question Trouble with Perl CGI script, particularly the discussion started by ChrisR.
      Thanks for the message. To start with I had tried with the shebang line followed by setting the path for library. But that retuned the error message that it cant find the GD module Fri Jul 29 12:46:02 2005 error client Premature end of script headers: /home/nagesh/apache/cgi-bin/simpleImage.cgi Can't locate in @INC (@INC contains: /usr/local/lib/perl5/5.8.6/i686-linux /usr/local/lib/perl5/5.8.6 /usr/local/lib/perl5/site_perl/5.8.6/i686-linux /usr/local/lib/perl5/site_perl/5.8.6 /usr/local/lib/perl5/site_perl .) at /home/nagesh/apache/cgi-bin/simpleImage.cgi line 8. BEGIN failed--compilation aborted at /home/nagesh/apache/cgi-bin/simpleImage.cgi line 8. Fri Jul 29 13:16:05 2005 error client Premature end of script headers: /home/nagesh/apache/cgi-bin/simpleImage.cgi This was the reason I moved it to top line before shebang which dint give me the error message about the missing GD module. Thanks Nagesh
      Hi This is the the output of @INC from the browser /usr/local/lib/perl5/5.8.6/i686-linux /usr/local/lib/perl5/5.8.6 /usr/local/lib/perl5/site_perl/5.8.6/i686-linux /usr/local/lib/perl5/site_perl/5.8.6 /usr/local/lib/perl5/site_perl from the command line /home/nagesh/progs/lib/perl5//i686-linux /home/nagesh/progs/lib/perl5/ /usr/local/lib/perl5/5.8.6/i686-linux /usr/local/lib/perl5/5.8.6 /usr/local/lib/perl5/site_perl/5.8.6/i686-linux /usr/local/lib/perl5/site_perl/5.8.6 /usr/local/lib/perl5/site_perl The first two line from the command line out put are missing from the browser output How do I make the change? Thanks
        Well, maybe GD is installed in your home directory. In that case, I guess the simplest way would be to add the following at the beginning of your script (though still underneath the #! line):
        BEGIN { unshift @INC, '/home/nagesh/progs/lib/perl5/i686-linux', '/home/nagesh/progs/lib/perl5/'; }
        Putting it in a BEGIN is important because otherwise it won't have any affect when you say use GD a few lines later. See perlmod for all the details. But, this makes it especially important that the files in your home directory are readable by Perl, which is not running under your user but rather as the Apache account.
Re: Problem running cgi script (Exec format error)
by tlm (Prior) on Jul 29, 2005 at 08:28 UTC

    Also consider using lib. It's sole purpose in life is to solve the problem you describe.

    As I rule I regard solutions like this (i.e. messing directly or indirectly with @INC) as short-term expedients; in the long run I try to have the site's administrator install whatever modules are required by my code in the standard site perl's @INC.

    the lowliest monk