0

Edit: This question has been wrongly marked as a duplicate of How can I troubleshoot my Perl CGI script?, that question deals with how to debug a perl program and the answers explain how to do that. In this case I did not even get as far as that, and the problem turned out to be a apache configuration issue, where I had used a relative path in the Directory directive, none of these these things are mentioned in the answers to that question.

Original Question: I have set up an ubuntu test server (16.04.2 LTS) and are trying to get apache on it to execute a perl cgi script. It runs fine from the command line, but in the browser it always display the source code. I have gone through many pages of advice both on this site and others, but I cannot get it to work, so any help would be greatly appreciated.

My site's conf file looks like this (I want to be able to execute scripts on a per directory basis):

<VirtualHost *:443>
    DocumentRoot "/var/www/test"
    ServerName mydomain.org
    <Directory "/var/www/test">
            allow from all
            Options None
            Require all granted
    </Directory>

    SSLEngine on
    SSLCertificateFile /etc/webmin/letsencrypt-cert.pem
    SSLCertificateKeyFile /etc/webmin/letsencrypt-key.pem
    SSLCACertificateFile /etc/webmin/letsencrypt-ca.pem

    <Directory "path/to/dir">
            Options ExecCGI Indexes
            AddHandler cgi-script .pl
    </Directory>
</VirtualHost>

My mods-enabled directory looks like this:

access_compat.load  autoindex.conf  mime.conf         setenvif.load
alias.conf          autoindex.load  mime.load         socache_shmcb.load
alias.load          cgi.load        mpm_prefork.conf  ssl.conf
auth_basic.load     deflate.conf    mpm_prefork.load  ssl.load
authn_core.load     deflate.load    negotiation.conf  status.conf
authn_file.load     dir.conf        negotiation.load  status.load
authz_core.load     dir.load        php7.0.conf
authz_host.load     env.load        php7.0.load
authz_user.load     filter.load     setenvif.conf

There are no errors in the error.log and the access logs tells me:

mydomain.org:443 xx.xxx.xxx.xx - - [27/Apr/2017:10:37:02 +0100] "GET /path/to/dir/test.pl HTTP/1.1" 304 3921 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36"

The script (test.pl) is simply this, and works from the command line:

#!/usr/bin/perl

print "Content-type: text/html\n\n";

print "Hello, world!\n";

Can anyone tell me what I am doing wrong here?

Community
  • 1
  • 1
Kees
  • 95
  • 10
  • First thing i see: `"path/to/dir"` should be an absolute path: `"/path/to/dir"` shouldn't it? Also, is your `.pl` script executable with the proper `#!` line? – cbmckay Apr 27 '17 at 11:11
  • Your a star! Making that an absolute path worked! Just to clarify, is there a way of specifying a path that is relative to the DocumentRoot? – Kees Apr 27 '17 at 11:20
  • I think `Location:` is what you want. You might also use `.htaccess` files. They apply to the directory they are located in. And / or you can use the `Files` directive to use the file extension to execute. – cbmckay Apr 27 '17 at 11:35
  • 2
    @cbmckay: I think you should write that up as an answer. – Borodin Apr 27 '17 at 11:48

1 Answers1

0

Just add ScriptAlias directive, if you don't have non-dynamic content inside the directive:

ScriptAlias /program_dir/ "/full/path/to/dir/"
Pradeep
  • 3,093
  • 17
  • 21