How to run a perl script in localhost?

9.4k views Asked by At

I had already installed Apache. I am using PHP for my scripting in localhost. Need to know how to run the perl script. I have installed sudo aptitude install libapache2-mod-perl2 I have created a directory name cgi-bin in my /var/www/cgi-bin there inside this folder i have kept my perl script perl_1.pl The directory permissions are given. What more i have to do to run the script???? i just type http://localhost/cgi-bin/ and i got error 403 You don't have permission to access /cgi-bin/ on this server. please help!!

Thanks

3

There are 3 answers

2
Jarmund On

you can't read the cgi-bin contents. You must refer directly to one of the scripts in it, in this case: http://localhost/cgi-bin/perl_1.pl

Outside of that, ensure that your cgi-bin/ directory is actually treated as such in httpd.conf.

Oh, and in case you stumble on 500 afterwards: make sure that your perl script prints a valid HTTP header. This can easily be achieved by:

use CGI qw(:standard);
print header();

And as Pwex pointed out: make sure your script has the executable bit set.

chmod 755 perl_1.pl

...should work in most cases

Additionally, for future reference it is worth mentioning mod_perl, as it is a natural next step after getting the basics of cgi + perl + apache down. Going into detail about it would be beyond the scope of this answer, but I thought I'd mention it so that you know where to go next when you've got the basics nailed down as well as seen the limitations of cgi.

1
ruz On

If you are not tied to apache or can run these scripts on different port then you can use Plack/PSGI toolchain that have solutions to run old CGI scripts as PSGI applications. See Running CGI scripts on Plack for several ways to do it.

0
Ricky Levi On

How's your Apache configured ? Did you make sure you're telling the Apache to execute CGI script in the cgi-bin directory ?

Something like:

ScriptAlias /cgi-bin/ "/var/www/website/cgi-bin/"
<Directory "/var/www/website/cgi-bin/">
                Options ExecCGI -MultiViews +SymLinksIfOwnerMatch

                ...
</Directory>