0

I have the following entry under apache2.conf in my Debian box.

AddHandler cgi-script .cgi .pl
Options +ExecCGI
ScriptAlias /cgi-bin/ /var/www/mychosendir/cgi-bin/

<Directory /var/www/mychosendir/cgi-bin>
Options +ExecCGI -Indexes
allow from all
</Directory>

Then I have a perl cgi script stored under these directories and permissions:

nvs@somename:/var/www/mychosendir$ ls -lhR 
.:
total 12K
drwxr-xr-x 2 nvs nvs 4.0K 2010-04-21 13:42 cgi-bin

./cgi-bin:
total 4.0K
-rwxr-xr-x 1 nvs nvs 90 2010-04-21 13:40 test.cgi

However when I tried to access it in the web browser:

http://myhost.com/mychosendir/cgi-bin/test.cgi

They gave me this error:

[Wed Apr 21 15:26:09 2010] [error] [client 150.82.219.158] (8)Exec format error: exec of '/var/www/mychosendir/cgi-bin/test.cgi' failed
[Wed Apr 21 15:26:09 2010] [error] [client 150.82.219.158] Premature end of script headers: test.cgi

What's wrong with it?

Update:

I also have the following entry in my apache2.conf:

<Files ~ "^\.ht">
    Order allow,deny
    Deny from all
</Files>

And the content of test.cgi is this:

#!/usr/bin/perl -wT
print "Content-type: text/html\n\n";
print "Hello, world!\n";
neversaint
  • 103
  • 1
  • 1
  • 5

3 Answers3

1

Make sure you have a <Directory> section for the cgi-bin directory. And make sure "allow from all" is in it.

<Directory /var/www/mychosendir/cgi-bin>
    Options +ExecCGI -Indexes
    allow from all
</Directory>

Also...your ScriptAlias is for /cgi-bin/. Your URL is /mychosendir/cgi-bin. Unless you have some rewrite magic going on, your url should probably be http://my.host.com/cgi-bin/test.cgi , or you'll need to change your ScriptAlias line to look like

ScriptAlias /mychosendir/cgi-bin/ /var/www/mychosendir/cgi-bin

The error you posted in your update, sounds like you don't have a #! line at the beginning of your script. You'll need one, and it should look like

#!/path/to/your/perl
cHao
  • 473
  • 1
  • 3
  • 10
  • @cHao: Sorry I don't get it. As I pointed out in OP I already have `cgi-bin` directory with the right permisson (a+x). – neversaint Apr 21 '10 at 05:56
  • 1
    The directory is there, but unless you have a section in the config file that grants access to it, Apache won't let you see it. –  Apr 21 '10 at 06:12
  • @cHao: Thanks but how can I change the config to grant access? – neversaint Apr 21 '10 at 06:15
  • 1
    Updated my answer. Reread. :) –  Apr 21 '10 at 06:17
  • I modified the Script alias to `ScriptAlias /var/www/mychosendir/cgi-bin/ /var/www/mychosendir/cgi-bin/` and add the `` section exactly like yours. But now it give internal server error instead. And BTW, I do really want my url to be `http://myhost.com/mychosendir/cgi-bin/test.cgi` instead of removing the `mychosendir`. – neversaint Apr 21 '10 at 06:22
  • 1
    The first thing in the ScriptAlias directive is the url you want to refer to that directory. So you'll probably want to get rid of the first /var/www in your ScriptAlias line. –  Apr 21 '10 at 06:24
  • You mean like this: `ScriptAlias /mychosendir/cgi-bin/ /cgi-bin/` . Sorry please be patient with me. – neversaint Apr 21 '10 at 06:42
  • 1
    I mean like this: ScriptAlias /mychosendir/cgi-bin/ /var/www/mychosendir/cgi-bin/ –  Apr 21 '10 at 06:49
  • Still internal error. BTW the perl code already contained the right header. See my update. – neversaint Apr 21 '10 at 06:54
  • 1
    I see it. I'm running the same code on one of my servers right now, so IF your perl is at /usr/bin/perl, then the script should be fine. –  Apr 21 '10 at 06:56
0

Are you sure the Apache user/group has read and execute access for the given files? Usually this user is called httpd or www-data.

Htbaa
  • 323
  • 2
  • 11
  • How do I check and enable that? – neversaint Apr 21 '10 at 06:10
  • First find out as which user Apache is running. This can be done by checking your `httpd.conf` or `apache2.conf` - look for the User and Group settings. Next, `ls -al /var/www/mychosendir/cgi-bin` to see the permissions and file owner. The user under which Apache is running must have access to these files. They should also contain the execute bit. – Htbaa Apr 21 '10 at 06:44
0

"Premature end of script headers" means that the CGI script is not printing out the correct HTTP header. It needs to print out at least

Content-Type: text/html;

Also, you have

ScriptAlias /cgi-bin/ /var/www/mychosendir/cgi-bin/

and yet you are asking for

http://myhost.com/mychosendir/cgi-bin/test.cgi

That should be

http://myhost.com/cgi-bin/test.cgi

or say

ScriptAlias /mychosendir/cgi-bin/ /var/www/mychosendir/cgi-bin/
  • It does have. See my update on the test.cgi code. – neversaint Apr 21 '10 at 06:38
  • I really want it to be accessed here: `http://myhost.com/mychosendir/cgi-bin/test.cgi`. Then how should I modify the ScriptAlias? I also tried this `ScriptAlias /cgi-bin/ /mychosendir/cgi-bin/` but internal server error. – neversaint Apr 21 '10 at 06:44
  • 2
    You are too dependent on other people to solve all your problems. –  Apr 21 '10 at 06:54