Search code examples
perlpermissionsapache2cgiexecution

Apache2 CGI Execution Permission Denied


I am getting this error when I try executing a basic Perl script on my Apache server. In my browser, I type in localhost/cgi-bin/first.pl, and I receive this error:

(13)Permission denied: exec of '/usr/lib/cgi-bin/first.pl' failed

This is my perl script:

#!/usr/lib/cgi-bin

print "Content-type: text/html\n\n";
print "Hello, World.";

And this is my default file in the sites-available folder. As you can see, every file in /usr/lib/cgi-bin should be recognized as a CGI file. And, /usr/lib/cgi-bin is exactly where first.pl is located.

<VirtualHost *:80>
    ServerAdmin webmaster@localhost

DocumentRoot /home/www
<Directory />
    Options FollowSymLinks
    AllowOverride None
</Directory>
<Directory /home/www/>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride None
    Order allow,deny
    allow from all
</Directory>

ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
    AddHandler cgi-script .cgi .py
    AllowOverride None
    Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
    Order allow,deny
    Allow from all
</Directory>

ErrorLog ${APACHE_LOG_DIR}/error.log

# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn

CustomLog ${APACHE_LOG_DIR}/access.log combined

Alias /doc/ "/usr/share/doc/"
<Directory "/usr/share/doc/">
    Options Indexes MultiViews FollowSymLinks
    AllowOverride None
    Order deny,allow
    Deny from all
    Allow from 127.0.0.0/255.0.0.0 ::1/128
</Directory>

ALSO, I did do chmod a+x first.pl.


Solution

  • You are getting this error because the shebang line (the first line of the script, starting with #!) specifies the interpreter that is launched to execute the script. What failed was therefore launching /usr/lib/cgi-bin as an executable.

    Replace

    #!/usr/lib/cgi-bin
    

    with

    #!/usr/bin/perl
    

    If that still doesn't work, one possibility is that perl is in an ununsual location, and you could try

    #!/usr/bin/env perl
    

    One suggestion, if you can use a shell on the machine where your script lives, would be to try executing it directly. Had you done this, you would have seen a slightly more explanatory message "bad interpreter: Permission denied".