Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Setting up Perl/CGI with Windows

by Ovid (Cardinal)
on Dec 02, 2000 at 05:43 UTC ( #44536=perltutorial: print w/ replies, xml ) Need Help??

Recently, I received an e-mail from a gentleman in the UK who was asking questions about how to set up a CGI programming environment on a Windows box. Below is an edited copy of my reply. It's an "Idiot's Guide" to getting started with CGI programming with Perl on Windows. I present as a way to get started, but not as "the" way to get started. Hopefully Monks who are totally new to this process can use this information.


First, you need to get Perl if you do not already have it installed. Go to http://www.activestate.com/Products/ActivePerl/Download.html and download the latest release for Windows. As of the time I write this, it's a link near the top that reads "Windows MSI". If for some reason you cannot install an MSI package, you can use the "Windows as Package" installer below that (but it's larger and you will have difficulty uninstalling it). If you do not enjoy the luxury of a high-speed Internet connection, you can find a smaller download near the bottom of the page at "Intel, APi522e.exe". This is about 2 to 3 megs smaller than the releases above. It is only Perl 5.005 (the others are 5.6), but the majority of Perl programs are still written in 5.005, so that's not an issue. However, some of the latest and coolest stuff you can do with Perl won't be available. If you're new to Perl, it will be a long time before you worry about stuff like that.

Once downloaded, double-click on the executable to install it. Accept all of the defaults, if possible.

Next, you need a Web server. I recommend you dowload Apache for Windows. Go to http://www.apache.org/dist/ and find "Windows Installer". It's about a 3.6 meg download. Double-click the icon after downloading and select the defaults. When it asks if you want a "Typical", "Full", or "Custom Install", choose "Typical" and let it do its thing. If all is successful, Apache will be on your system.

Go to C:\Program Files\Apache Group\Apache and run the Apache.exe program. Congratulations! If all went well, you have a Web server running on your box. Once the server is running, open a Web browser and type http://localhost/ into the address bar. You should see a Web page telling you that your installation of Apache was successful.

If there was a problem with the Apache install, often the error message and the window it appears in will dissappear quickly (assuming that you double-clicked the executable and didn't run it from the command line). To see the message, open a command prompt and CD to the directory. Then, run the program from the command line. If there is an error message, you'll see it and take the appropriate action.

Next, you want to ensure that you can run CGI scripts. First, go to C:\Program Files\Apache Group\Apache\cgi-bin and create a file named "test.cgi". Assuming that you have followed the defaults for your Perl installation, edit test.cgi and enter the following program:

#!C:\perl\bin\perl.exe -wT use strict; use CGI; my $query = new CGI; print $query->header( "text/html" ); print <<END_HERE; <html> <head> <title>My First CGI Script</title> </head> <body bgcolor="#FFFFCC"> <h1>This is a pretty lame Web page</h1> <p>Who is this Ovid guy, anyway?</p> </body> </html> END_HERE # must have a line after "END_HERE" or Perl won't recognize # the token
It's important to remember that you must have a line (even a blank one) after the final END_HERE. Heck, hit return a couple of times just to be safe. Once you have that entered exactly as I have it above, enter http://localhost/cgi-bin/test.cgi into the browser's address bar and hit return. If all went well, you'll see a Web page telling you how lame it is.

If for some reason you could not accept the defaults for installing Perl, make sure that the first line of the Perl script points to the correct Perl executable. That's how Apache knows how to run this program.

If END_HERE is the last line of the program and has no return after it, Perl will complain that it

Can't find string terminator "END_HERE" anywhere before EOF at c:/prog +ram files/apache group/apache/cgi-bin/test.cgi line 8.
Further, you'll just see an "Internal Server Error" or something similar in your browser. To find the actual error message, you'll go to C:\Program Files\Apache Group\Apache\logs and open up the "error.log". In fact, open up this log any time you want to figure out why your CGI program didn't run.

The above instructions should work fine if you are one Windows9.x or WindowsNT (you might need administrator privileges for WinNT).

If you are interested in books on the subject, you'll want Learning Perl for Win32 Systems (published by O'Reilly press). Also, CGI Programming with Perl, second edition (also by O'Reilly) is an excellent resource, but assumes that you already know Perl. You can find Perl books and related merchandise at this link.

Currently, almost every other book out there (there are a couple of exceptions) that explains CGI programming is simply miserable. Sorry to be rude, but there is no other way to explain it. One book to stay away from in particular is "Perl and CGI for the World Wide Web, Visual Quickstart Guide" by Elizabeth Castro. This book has been wildly popular and demonstrates not only poor Perl programming techniques, but many of her scripts have terrible security holes that are likely to get new programmers fired and their Web sites cracked.

This little example won't teach you to write CGI scripts, but it will get you started. If you are interested, check out my online Web programming course for further information on this subject.

Update: I have just upgraded to Perl v5.6 on my home machine only to discover that for some reason, I had to edit the autoexec.bat file to add C:\PERL\BIN to the path. If you aren't sure how to do this, grab a different version of the Perl installer as it's very easy to damage things while editing autoexec.bat.

Update 2: Another book should be added to the list of recommended books. "Writing CGI Applications with Perl" by Kevin Meltzer and Brent Michalski, published by Addison Wesley. From my initial readings, it appears to be very good.

Update 3: I warn the reader to stay away from "Perl and CGI for the World Wide Web" by Elizabeth Castro, but I now cautiously recommend the Second edition (not the first!). The second edition is much better than the first and while it has some security issues, it's a good introduction to Perl/CGI.

Comment on Setting up Perl/CGI with Windows
Select or Download Code
Re: Setting up Perl/CGI with Windows
by Anonymous Monk on Jul 15, 2002 at 14:16 UTC
    Good Advices and Guidance. Thank you. Darani
Re: Setting up Perl/CGI with Windows
by strat (Canon) on Nov 07, 2002 at 11:49 UTC
    Good tutorial, Ovid!

    I'd just like to point you to http://www.apachefriends.org/index-en.html where you can download WAMPP, a package of Apache, MySql, Perl and PHP and sometimes PhpMyAdmin, OpenSSL, mod_perl and much more for Win32. The installation process is well documented, and both in english or german. There also exists LAMPP, about the same for Linux. But I haven't yet tried it.

    Best regards,
    perl -e "s>>*F>e=>y)\*martinF)stronat)=>print,print v8.8.8.32.11.32"

      Hello I have installed activeperl on windows 2003 with IIS6. when i try to run a cgi file, i get the error CGI application misbehaved by not returning a complete set of HTTP headers Anyone has an idea?
        Either you don't send a content-type, or you don't send it as the first output. Does the following code work?
        #! /usr/bin/perl use warnings; use strict; use CGI; use CGI::Carp qw(fatalsToBrowser warningsToBrowser); my $cgi = CGI->new(); print $cgi->header(), $cgi->start_html('Testpage'); print $cgi->h1('Testpage'); print $cgi->end_html();

        Best regards,
        perl -e "s>>*F>e=>y)\*martinF)stronat)=>print,print v8.8.8.32.11.32"

        I also setup perl on win2k3 and iis6 and i too get the cgi header error. i do have the content-type line as my first line after the shebang line. i have read pages and pages of information on the web. all my scripts work in a command line, but not in IE. Any ideas???
Re: Setting up Perl/CGI with Windows
by ff (Hermit) on Jun 15, 2003 at 00:59 UTC
    Some newbie comments on setup: The files on the mirror site under "httpd" and "binaries" were sorted in order such that a version 1.x of Apache was at the top of the list. Looking recently built and not bothering to scroll down to "older" binaries at the mirror, I downloaded that one and started installing. Its README said that if version 2 of Apache was available that it should be preferred. Guess what, further in the list, there's Apache2. So, instead of installing V1 I got and installed V2.

    Along the way you have to tell Apache what your domain name is. (You are not supposed to make one up....). The httpd.conf file in the conf directory says to use your Internet number instead, and combined with an install-time instruction about running just with my login id, I wound up configuring a ServerName of 192.168.1.1:8080 (not my real ip address...) which, when I type http://192.168.1.1:8080/ works! :-) The test script gets invoked via http://192.168.1.1:8080/cgi-bin/test.cgi

      About setting this up with domain names and such. Im on an internal network with only once box. The box im on is Windows 2000 Pro. I would like to setup a cgi testbed to work with but cant have traffic popping out on the network as to not make IDS mad. Is there a way to set this up w/o using the domain info and such that I am on? Should I have to worry about random packets going out to the network or does Apache just wait for requests?
Re: Setting up Perl/CGI with Windows
by Anonymous Monk on Jul 11, 2003 at 09:41 UTC
    Very good tutorial.... I do not speak very well english (and so understand), and I have now an Apache web server on my computer... lots of thanks
Re: Setting up Perl/CGI with Windows
by skiboy (Sexton) on Dec 12, 2003 at 02:07 UTC
    This is a fantasic article!! Thank you so much. My Boss turned me on to this site and I've been coding Perl for like a year in mostly Windows and simple stuff. I just started to attempt to do CGI but had problems and need to practice at home on a server that has cgi. I was able to download the Windows Apache Server and install it on my box at home with your directions. Of couse I had typo errors and I didn't catch them but your directions to the error.log solved that problem. Now I'm off to your site for the "online Web programming course". Once again Thanks! ;-)

      You are very welcome. I'm glad that people still find it useful after three years.

      Welcome to Perlmonks!

      Cheers,
      Ovid

      New address of my CGI Course.

        Hi I just installed perl on win 2003 server, with IIS6. Do I need to install Apache too?
        Secondly, when I run the test.cgi file, it gives me this error:
        CGI Error The specified CGI application misbehaved by not returning a complete s +et of HTTP headers.
        Can you help me? Thanks
        Dzigner http://www.des-us.com http://freehost14.websamba.com/netdzigns
        I have no words to praise a piece of article which i found very useful for starting up on PERL or CGI. Thank you very much. Ramesh Babu.
Re: Setting up Perl/CGI with Windows
by Anonymous Monk on Mar 11, 2004 at 04:19 UTC
    I've installed active perl and apache 2.0.48 on my windows box but I am having trouble figuring out how to configure the localhost part of the domain. How do I configure this? I would like to try and run the test.cgi file, however I can't figure out which localhost to use or how to set it up. Any suggestions?
Re: Setting up Perl/CGI with Windows
by Anonymous Monk on Jul 29, 2004 at 06:17 UTC
    I cant say anymore JUST U SAVED MYLIFE THATS ALL
Re: Setting up Perl/CGI with Windows
by Anonymous Monk on Oct 08, 2004 at 01:01 UTC
    FANTASTIC...Worked the 1st time, and as I'm sure we are all aware, that's rare. Thanks (Consider yourself bookmarked)
      Internal Server Error The server encountered an internal error or misconfiguration and was unable to complete your request. Please contact the server administrator, 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.
Re: Setting up Perl/CGI with Windows
by Anonymous Monk on Oct 28, 2004 at 00:51 UTC
    Thank You, Finally I got CGI to work on my apache2 and active Perl, now I only need to get the .pl working! FYI I'm running Apache 2.0.50, PHP 4.3.8, Active Perl 5.8 on a WinXP Pro SP2 : )
      If I'm understanding correctly what you're asking, the solution is as follows: To map .PL to perl just like a .CGI file, in any Explorer window go to Tools -> Folder Options -> tab to File Types -> scroll to .PL. If it's not there, add a New one. Copy to that record the same settings from whatever you have .CGI mapped in that list, which basically consists of the path to your Perl Command Line Interpretor (usually c:\perl\bin\perl.exe).
Re: Setting up Perl/CGI with Windows
by mrodent (Initiate) on Dec 30, 2004 at 10:58 UTC
    Dear Ovid, This was brilliant... I've been tearing out my hair over these first faltering steps of cgi perl stuff... and nobody seemed to be able to explain what "make a perl script executable" means, and people told me to install mod_perl, or to install heaps of stuff like "php" and "mysql" and "phpadmin" before I could get anywhere... thank gawd for the monastery mike rodent
Re: Setting up Perl/CGI with Windows
by paradisedo (Initiate) on Dec 07, 2006 at 16:33 UTC
    yes good infooo thx a lot
Re: Setting up Perl/CGI with Windows
by Anonymous Monk on May 28, 2008 at 10:39 UTC
    Hi Ovid, I still cannot run my cgi. I ve installed apache Tomcat and activeperl as you suggested. i ve uncomment web.xml and rename the jar file in lib folder. but when i ask for http://localhost:8081/prototype/cgi-bin/digest.cgi it still didn't work. please tell me if i am missing something there. thanks.
Re: Setting up Perl/CGI with Windows
by Anonymous Monk on Nov 04, 2008 at 09:45 UTC
    Well written! Concise and clutter-free. I was able to have my Apache-perl setup running in less than 20 minutes. Thank you very much for sharing this with us. I am sure many of us found this useful, and this ariticle wouln't go unappreciated as well. Keep such articles coming :-) Nagaraj
Re: Setting up Perl/CGI with Windows
by Hartmut (Initiate) on Jun 01, 2012 at 14:10 UTC

    Hi

    I used this manual to install Perl5.14.2 and Apache2.2.22 running a Windows 7 machine. Till the early afternoon I never saw Perl or Apache.

    THX - it is just working.

    Hartmut

      Hi Hartmut, Can you share / send me the httpd.conf file changes made to run cgi/perl ?I require this for configuring Bugzilla on windows 7. Thanks Venkat at vemula99@gmail.com
        bugzilla comes with lots of clear install instructions, its a good idea to read them
Re: Setting up Perl/CGI with Windows
by Anonymous Monk on Jul 01, 2013 at 00:55 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perltutorial [id://44536]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (18)
As of 2014-08-27 14:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (242 votes), past polls