Perl Apache : Perl script displayed as plain text

旧街凉风 提交于 2019-11-27 20:40:02

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

The directory/location/file doesn't have the right handler associated with it, or doesn't have the ExecCGI option enabled. See Apache Tutorial: Dynamic Content with CGI.

change new version of apache : Options +FollowSymLinks +ExecCGI

joshua paul

on mac os x 10.8

i had to do this

<Directory />
    Options FollowSymLinks +ExecCGI
    AllowOverride None
</Directory>

and

uncomment this

#AddHandler cgi-script .cgi .pl
 # use types www.site.com/visible-in-url
 # Apache serves /var/path/to/dir
 ScriptAlias /visible-in-url/ /var/path/to/dir/

 # Note the order of the aliases matters - first cgi than static content
 # Note this dir is a symlink pointing to the desirable directory
 <Directory "/var/path/to/dir">
    AddHandler cgi-script .cgi .pl
    AllowOverride Indexes
    Options +ExecCGI +MultiViews +SymLinksIfOwnerMatch
    Require all granted
 </Directory>

 <Files ~ "\.(pl|cgi)$">
    SetHandler perl-script
    PerlResponseHandler ModPerl::PerlRun
    Options +ExecCGI +SymLinksIfOwnerMatch
    PerlSendHeader On
 </Files>

When browser is printing code of script that means it's unable to find the application to run the script.

With Apache 2.4 (on OSX Yosemite, 10.10.5), if I use a shebang line with the wrong path, my browser displays:

Internal Server Error

But even with a valid shebang line, I could not get my cgi program to execute by following the advice in the accepted answer--Apache just served up the text of the program to my browser. After some experimenting, I found that the only change I needed to make to my /usr/local/apache2/conf/httpd.conf file was uncommenting the line:

LoadModule cgid_module modules/mod_cgid.so

My cgi programs have the extensions .pl, .py, and .rb depending on what language I'm programming in (and the apache cgi-bin directory contains a test cgi script with no extension), and they all execute without having to specify valid extensions anywhere in the httpd.conf file. My default httpd.conf file has only the following relevant lines:

<IfModule alias_module>
    #Lots of comments here

    ScriptAlias /cgi-bin/ "/usr/local/apache2/cgi-bin/"

</IfModule>
...
...
<Directory "/usr/local/apache2/cgi-bin">
    AllowOverride None
    Options None
    Require all granted
</Directory>

The shebang line that I'm using is, depending on what language my cgi program is written in:

#!/usr/bin/env perl

or:

#!/usr/bin/env python 

or:

#!/usr/bin/env ruby

A cgi program has to be an executable file as well, or else you will get an Internal Server error:

$ chmod a+x myprog.pl

a+x => all + executable. In other words, add the executable permission to each of owner, group, other.

And, at a minimum the cgi program has to generate a Content-Type header before outputting the body of the response:

print "Content-Type: text/html\n\n";
print "<h1>Hello, World.</h1>";

(By the way, that exact code will work in perl, python, or ruby.) Otherwise, once again you will get an Internal Server error.

The url to execute the cgi script:

http://localhost:8080/cgi-bin/myprog.pl

This is how I installed apache:

~/Downloads$ tar xvfz httpd-2.4.18.tar.bz2
...
...
~/Downloads$ cd httpd-2.4.18
...
...
~/Downloads/httpd-2.4.18$ ./configure --help
...
...
--enable-so       DSO capability. This module will be automatically
                  enabled unless you build all modules statically.
...
...

I had no idea what the heck that meant, but the php docs say to install apache with that option, so I went ahead and did this:

~/Downloads/httpd-2.4.18$ ./configure --enable-so
...
...
~/Downloads/httpd-2.4.18$ make
...
...
~/Downloads/httpd-2.4.18$ sudo make install

Apache DSO docs here.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!