Perl Apache : Perl script displayed as plain text

msinfo picture msinfo · Feb 10, 2013 · Viewed 49.9k times · Source

While configuring with apache and perl cgi scripts, don't know why index.cgi/index.pl are displayed as plain text instead of executing them. When I put http://localhost in browser it displays below code, instead of executing it.

List item
    #!C:/Dwimperl/perl/bin/perl.exe -w

     print "Content-type: text/html\n\n";
        print <<HTML;
        <html>
        <head>
        <title>A perl web page</title>
        </head>
        <body>
        <h3>A hello world form perl</h3>
        </body>

        HTML
        exit;

This are parts of httpd.conf file which I have edited most of the times (after reading various online reference, tutorials)

# This should be changed to whatever you set DocumentRoot to.
<Directory "D:\webserver">

Listen 80
ServerName localhost:80
LoadModule cgi_module modules/mod_cgi.so

# First, we configure the "default" to be a very restrictive set of 
# features.  
<Directory />
    Options FollowSymLinks +ExecCGI
    AllowOverride None
</Directory>

DirectoryIndex index.html index.html.var index.cgi index.pl

AccessFileName .htaccess

# To use CGI scripts outside of ScriptAliased directories:
# (You will also need to add "ExecCGI" to the "Options" directive.)
#
#AddHandler cgi-script .cgi .pl

ScriptAlias /cgi-bin/ "C:/Apache/Apache2/cgi-bin/"

Answer

user966588 picture user966588 · Feb 10, 2013

When browser is printing code of script that means it's unable to find the application to run the script. Below two lines should be your first steps to solve this. AddHandler will make sure files ending with .cgi and .pl to be treated as cgi scripts. And +ExecCGI option will allow to execute the script. Also make sure your script is pointing to correct perl binary location.

    AddHandler cgi-script .cgi .pl
    Options FollowSymLinks +ExecCGI

Also There are some mistakes/misconfiguration points in your httpd.conf

  • Alias line should point to cgi-bin directory where your cgi scripts are present.

ScriptAlias /cgi-bin/ "D:\webserver\cgi-bin"

  • For same cgi-bin directory following configuration should be in httpd.conf. You should replace your <Directory "D:\webserver"> part with below.
<Directory "D:\webserver\cgi-bin" />
    AddHandler cgi-script .cgi .pl
    Options FollowSymLinks +ExecCGI
    AllowOverride None  
</Directory>
  • Try running your cgi script from command line like below. It should print or run from command line first.

perl test.cgi

  • Make sure you have read-write recursive permissions to cgi-bin directory and your cgi script. And also you can create directory or file with write permissions. If not create a cgi-bin directory at some other place where you can have write permissions and provide rather its path in alias and directory attributes in httpd.conf instead.
  • Check apache error log for exact error message every time you run into apache conf issues. It will give you good insight into the problem.

Also this link should help you.

(Extra comment, not by the original answerer: You may also need to enable the cgi module. For me, the final step to getting cgi to work on a fresh install of Apache 2 was sudo a2enmod cgi. Before I did that, the website simply showed me the contents of the script.)

sudo a2enmod cgi