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/"
change new version of apache : Options +FollowSymLinks +ExecCGI
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.on mac os x 10.8
i had to do this
and
uncomment this
With
Apache 2.4
(on OSX Yosemite, 10.10.5), if I use a shebang line with the wrong path, my browser displays: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: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:The shebang line that I'm using is, depending on what language my cgi program is written in:
or:
or:
A cgi program has to be an executable file as well, or else you will get an Internal Server error:
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:(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:
This is how I installed apache:
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:
Apache DSO docs here.
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.Also There are some mistakes/misconfiguration points in your httpd.conf
httpd.conf
. You should replace your<Directory "D:\webserver">
part with below.cgi-bin
directory and your cgi script. And also you can create directory or file with write permissions. If not create acgi-bin
directory at some other place where you can have write permissions and provide rather its path inalias
anddirectory
attributes inhttpd.conf
instead.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 wassudo a2enmod cgi
. Before I did that, the website simply showed me the contents of the script.)