0

i am having strange problem.,

from command line cgi bash scripts and cgi perl scripts are working, but from browser only cgi bash scripts are working and not the cgi perl.

after accessing cgi perl scripts from browser i get 500 Internal server error.

and apache error log says

[Thu Oct 25 01:58:59 2012] [error] [client x.x.x.x] (13)Permission denied: exec of '/home/x/x/x/x/public_html/cgi-bin/test.cgi' failed
[Thu Oct 25 01:58:59 2012] [error] [client x.x.x.x] Premature end of script headers: test.cgi

i want to run cgi perl scripts from browser .

how do i do it.

[root@www ~]# which perl
/usr/bin/perl

[root@www cgi-bin]# perl test.cgi
Content-type: text/plain

testing...

source of test.cgi

#!/usr/bin/perl

print "Content-type: text/plain\n\n";
print "testing...\n";

Thanks for your time.

its a dedicated server with apache 2.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 2
    Make sure the web server's userid has read and execute permission to everything in the path to `test.cgi`. – Barmar Oct 25 '12 at 07:23
  • Try `su www-data` (or whatever the user of apache is), *then* run the script and see if it works. – January Oct 25 '12 at 07:35
  • su apache was not working, so i changed user and group in httpd.conf to the owner of the cgi file and voila everything worked perfectly., thanks lot , now how do i add permisions to user apache to access .cgi files –  Oct 25 '12 at 08:11
  • now i modded passwd and gave a shell to user apache , i tried su apache it worked then i tried executing the script it says "bash: /usr/bin/perl: Permission denied " thanks –  Oct 25 '12 at 08:30

3 Answers3

2

As mentioned by Barmar, you probably have a permissions issue. Also, since you are just getting started, here is an improved test script:

#!/usr/bin/perl
use strict;
use warnings;

#Useful for testing: Perl error messages, plus your die statements, will get
#sent to the browser.  Otherwise you will just see "Internal Server Error".
use CGI::Carp qw/fatalsToBrowser/;

#Always use the CGI module for your scripts.
use CGI; 

#Create simple HTML output (taken directly from CGI documentation).
my $q = CGI->new;                    # create new CGI object
print $q->header,                    # create the HTTP header
      $q->start_html('hello world'), # start the HTML
      $q->h1('hello world'),         # level 1 header
      $q->end_html;                  # end the HTML

Please see the documentation on CGI for more information.

Also, for clarification: the "Premature End of Script Headers" error means that your script sent no output to the browser. In this case, it is because your script didn't run. However, it could also happen if your script runs but doesn't actually send any output. It is a useful error to know about.

dan1111
  • 6,576
  • 2
  • 18
  • 29
  • i created new test script from your data and chmod +x and also 755 to it but still getting same errors., its running from command line but not from browser, is mod_perl required to run perl cgi scripts ? thanks –  Oct 25 '12 at 07:58
  • i just checked the german article and created new document using unix format just to be on safe side and got the same error., thanks –  Oct 25 '12 at 08:02
  • @AmbroseBwangatto, `mod_perl` is not required (and not recommended if you are just getting started). Check out this link for more possible sources of the error: http://kurinchilamp.kurinchilion.com/2009/08/permission-denied-exec-of-failed-in-apache-server.html – dan1111 Oct 25 '12 at 08:06
  • issue solved when i changed USER and GROUP in httpd.conf file to the owner of test.cgi file as i tried su apache and it wasnt working. –  Oct 25 '12 at 08:11
1

Did you set permissions correctly for the perl script? Compare them with the permissions you set for the bash script.

Maybe chmod 755 would help as suggested here (unfortunately German only)

  • yes i have double checked the permissions and owner and grooup and chmodded +x too, still the cgi bash script are working and cgi perl arent working in browser. thanks –  Oct 25 '12 at 07:44
0

check which user is running httpd by

[root@www ~]# top | grep httpd
15607 apache    16   0  210m  19m 4228 S 23.5  0.2   0:00.52 httpd

in above the apache user is running.

try su apache,

confirm you are apache by using command whoami ,

if you havent switched to apache then this means no shell is assigned to user apache,

now modify /etc/passwd file

find user apache and change ending from

/bin/false

to 

/bin/bash

save passwd file , then try su apache, check with whoami, if you successfully switched to user apache then go to the directory where the script is located and try to run it from there.

you will get something like

bash: /usr/bin/perl: Permission denied

this means user "apache" does not have permissions to user perl.

you need to add permissions to execute perl script to user "apache".

***dont forget to change the passwd file as it was previously.

you can do that by adding a group to specific user and changing the group name httpd.conf.

thanks