Keep Server Online
If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.
or
A donation makes a contribution towards the costs, the time and effort that's going in this site and building.
Thank You! Steffen
Your donations will help to keep this site alive and well, and continuing building binaries. Apache Lounge is not sponsored.
| |
|
Topic: Please help me get started with using CGI with Apache (Perl) |
|
Author |
|
literat
Joined: 24 Jan 2011 Posts: 2 Location: London
|
Posted: Mon 24 Jan '11 8:19 Post subject: Please help me get started with using CGI with Apache (Perl) |
|
|
Edit: I have solved this problem now, I think. It was the shebang line.
Please could you help me to get Perl CGI scripts running on my pc
this is my first question here, and I am sorry if I haven't formatted it in the correct way - like maybe this is too verbose
24/1/11 Downloaded WampServer2.1d-x64.exe
WampServer 2 (with Apache 2.2.17) from http://www.wampserver.com/en/download.php
installed in the directory that was suggested to me: C:\wamp
(on a Win 7 x64 Intel)
It said: "Please choose your default browser. If you not sure, just click Open:"
I just clicked Open (with filename: explorer.exe displayed in C:/Windows)
It suggests an SMTP server of: localhost
and email (to be used by PHP when using the function mail) of: you@yourdomain (I just clicked: ok)
I launch WampServer
It seems to work fine:
1) I left-click WampServer icon in system tray (next to the Windows clock at the bottom left of the screen).
2) From the drop-down menu, I select: Localhost
Chrome opens with the standard Localhost Server Configuration page:
"Server Configuration Apache Version : 2.2.17 PHP Version : 5.3.4 Loaded Extensions : Core bcmath calendar etc."
I find a directory called "cgi-bin" at C:\wamp\bin\apache\Apache2.2.17\cgi-bin
In there is a file called printenv.pl (I didn't put it there)
the file basically just prints out %ENV:
Code: | #!C:/Perl64/bin/perl.exe
##
## printenv -- demo CGI program which just prints its environment
##
print "Content-type: text/plain; charset=iso-8859-1\n\n";
foreach $var (sort(keys(%ENV))) {
$val = $ENV{$var};
$val =~ s|\n|\\n|g;
$val =~ s|"|\\"|g;
print "${var}=\"${val}\"\n";
} |
I try entering, in the normal URL address bit in Chrome:
C:\wamp\bin\apache\Apache2.2.17\cgi-bin\printenv.pl
it just prints the code verbatim without processing any of the Perl. (I need it to go through the foreach loop etc. and if it can do that then everything will be sorted.)
I left-click the Apache icon and select: Apache > Apache Error Log
nothing changed in there (when I used C:\wamp\bin\apache\Apache2.2.17\cgi-bin\printenv.pl in the address bar)
I try just entering in the address bar:
1) /cgi-bin/printenv.pl
this gives: "Oops! Google Chrome could not find cgi-bin"
(and the Apache error log doesn't change)
2) localhost/printenv.pl
this makes Chrome say: "Oops! This link appears to be broken."
and the Apache error log says:
[Mon Jan 24 05:21:51 2011] [error] [client 127.0.0.1] File does not exist: C:/wamp/www/printenv.pl
[Mon Jan 24 05:21:51 2011] [error] [client 127.0.0.1] File does not exist: C:/wamp/www/favicon.ico
3) localhost/cgi-bin/printenv.pl
this gives: "Internal Server Error - The server encountered an internal error or misconfiguration and was unable to complete your request. Please contact the server administrator, admin@localhost and inform them of the time the error occurred, and anything you might have done that may have caused the error. More information about this error may be available in the server error log."
and the Apache error log says:
[Mon Jan 24 05:26:49 2011] [error] [client 127.0.0.1] (OS 2)The system cannot find the file specified. : couldn't create child process: 720002: printenv.pl
[Mon Jan 24 05:26:49 2011] [error] [client 127.0.0.1] (OS 2)The system cannot find the file specified. : couldn't spawn child process: C:/wamp/bin/apache/Apache2.2.17/cgi-bin/printenv.pl
[Mon Jan 24 05:26:50 2011] [error] [client 127.0.0.1] File does not exist: C:/wamp/www/favicon.ico
4) localhost/wamp/bin/apache/Apache2.2.17/cgi-bin/printenv.pl
this makes Chrome say: "Oops! This link appears to be broken."
and the Apache error log says:
[Mon Jan 24 05:28:10 2011] [error] [client 127.0.0.1] File does not exist: C:/wamp/www/wamp
[Mon Jan 24 05:28:10 2011] [error] [client 127.0.0.1] File does not exist: C:/wamp/www/favicon.ico
5) localhost/C:/wamp/bin/apache/Apache2.2.17/cgi-bin/printenv.pl
this makes Chrome say: "Forbidden - You don't have permission to access /C:/wamp/bin/apache/Apache2.2.17/cgi-bin/printenv.pl on this server."
and the Apache error log says:
[Mon Jan 24 05:29:33 2011] [error] [client 127.0.0.1] (20023)The given path was above the root path: Cannot map GET /C:/wamp/bin/apache/Apache2.2.17/cgi-bin/printenv.pl HTTP/1.1 to file
[Mon Jan 24 05:29:33 2011] [error] [client 127.0.0.1] File does not exist: C:/wamp/www/favicon.ico
I google Apache CGI tutorial, and get to: httpd.apache.org/docs/2.2/howto/cgi.html
It says:
"In order to get your CGI programs to work properly, you'll need to have Apache configured to permit CGI execution. There are several ways to do this.
ScriptAlias
The ScriptAlias directive tells Apache that a particular directory is set aside for CGI programs. Apache will assume that every file in this directory is a CGI program, and will attempt to execute it, when that particular resource is requested by a client.
The ScriptAlias directive looks like:
ScriptAlias /cgi-bin/ /usr/local/apache2/cgi-bin/
The example shown is from your default httpd.conf configuration file, if you installed Apache in the default location."
I open httpd.conf, search for 'scriptalias' and get:
ScriptAlias /cgi-bin/ "cgi-bin/"
it doesn't say: ScriptAlias /cgi-bin/ /usr/local/apache2/cgi-bin/
The help document said: "The example shown is from your default httpd.conf configuration file"
No it's not because mine says: ScriptAlias /cgi-bin/ "cgi-bin/"
but anyway, the help document continues:
"the example above tells Apache that any request for a resource beginning with /cgi-bin/ should be served from the directory /usr/local/apache2/cgi-bin/, and should be treated as a CGI program."
so presumably if I change the /usr/local/apache2/cgi-bin/ bit to C:/wamp/bin/apache/Apache2.2.17/cgi-bin/
or maybe just /wamp/bin/apache/Apache2.2.17/cgi-bin/
then Apache will treat printenv.pl as a CGI program
okay so I change: ScriptAlias /cgi-bin/ "cgi-bin/"
to: ScriptAlias /cgi-bin/ /wamp/bin/apache/Apache2.2.17/cgi-bin/
and save the file
maybe Apache only uses httpd.conf when it boots up, so I left-click the icon and Exit. The icon disappears from the system tray. Then I start it up again. Chrome shows LocalHost Server Configuration screen without any problems.
Then I try again in the Chrome URL address bar:
3) localhost/cgi-bin/printenv.pl
it still gives: "Internal Server Error - The server encountered an internal error or misconfiguration and was unable to complete your request. Please contact the server administrator, admin@localhost and inform them of the time the error occurred, and anything you might have done that may have caused the error. More information about this error may be available in the server error log."
I try emailing the server administrator, but he is completely ignoring me. And the others are the same also:
1) /cgi-bin/printenv.pl = "Oops! Google Chrome could not find cgi-bin"
2) localhost/printenv.pl = "Oops! This link appears to be broken."
4) localhost/wamp/bin/apache/Apache2.2.17/cgi-bin/printenv.pl = "Oops! This link appears to be broken."
5) localhost/C:/wamp/bin/apache/Apache2.2.17/cgi-bin/printenv.pl = "Forbidden - You don't have permission to access /C:/wamp/bin/apache/Apache2.2.17/cgi-bin/printenv.pl on this server."
Am I doing something wrong? There's more stuff I could try. Like using quotation marks in the ScriptAlias bit
If there's anything you could suggest that would really be appreciated.
Last edited by literat on Mon 24 Jan '11 8:54; edited 1 time in total |
|
Back to top |
|
literat
Joined: 24 Jan 2011 Posts: 2 Location: London
|
Posted: Mon 24 Jan '11 8:50 Post subject: |
|
|
oh sorry for wasting your time guys - it was the shebang line
when WampServer created printenv.ppl
it automatically put #!C:/Perl64/bin/perl.exe in there
and I looked at that and thought "yeah that's about right"
but I had installed perl in C:/Program Files (x86)/Perl64/bin/perl.exe
so I just changed that one thing and it is printing out %ENV fine now
it is probably a typical thing a perl windows user will do, because like personally, i always look at shebang's and totally don't give them the time of day "oh that's just a comment, i'm not going to even bother including that bit"
but with Apache shebang's obviously do matter a lot
So to get Perl CGI working on a Windows PC, it's actually pretty simple:
1) install WampServer2
2) getthe path for the cgi-bin directory that WampServer created
for me it was at C:/wamp/bin/apache/Apache2.2.17/cgi-bin
3) open httpd.conf with notepad
4) change the ScriptAlias line
mine now says:
ScriptAlias /cgi-bin/ /wamp/bin/apache/Apache2.2.17/cgi-bin/
5) WampServer may need rebooting at this point
6) change the shebang line at the beginning of printenv.pl (or test.pl or whatever the file is that you want to run as CGI from the cgi-bin folder)
The shebang line must say where your perl interpreter (aka perl.exe) is.
For me perl.exe is in: C:/Program Files (x86)/Perl64/bin/
so the shebang line needed to be:
#! C:/Program Files (x86)/Perl64/bin/perl.exe |
|
Back to top |
|
|
|
|
|
|