|P is for Practical|
Setting up Perl/CGI with Windowsby Ovid (Cardinal)
|on Dec 02, 2000 at 05:43 UTC||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:
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
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.