How to run a perl script in localhost? How to run a perl script in localhost? apache apache

How to run a perl script in localhost?


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.


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>


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.