Running CGI With Perl under Apache Permission Problem

11,115

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
Share:
11,115

Related videos on Youtube

neversaint
Author by

neversaint

Updated on September 17, 2022

Comments

  • neversaint
    neversaint over 1 year

    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
    neversaint about 14 years
    @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
    neversaint about 14 years
    How do I check and enable that?
  • Michalis
    Michalis about 14 years
    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.
  • neversaint
    neversaint about 14 years
    @cHao: Thanks but how can I change the config to grant access?
  • Michalis
    Michalis about 14 years
    Updated my answer. Reread. :)
  • neversaint
    neversaint about 14 years
    I modified the Script alias to ScriptAlias /var/www/mychosendir/cgi-bin/ /var/www/mychosendir/cgi-bin/ and add the <Directory> 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.
  • Michalis
    Michalis about 14 years
    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.
  • neversaint
    neversaint about 14 years
    It does have. See my update on the test.cgi code.
  • neversaint
    neversaint about 14 years
    You mean like this: ScriptAlias /mychosendir/cgi-bin/ /cgi-bin/ . Sorry please be patient with me.
  • Htbaa
    Htbaa about 14 years
    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.
  • neversaint
    neversaint about 14 years
    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.
  • Michalis
    Michalis about 14 years
    I mean like this: ScriptAlias /mychosendir/cgi-bin/ /var/www/mychosendir/cgi-bin/
  • Michalis
    Michalis about 14 years
    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.