The Writing Confederation

Computers, anime, and writing – A confederation of topics

Running Perl and CGI Files in Windows

A comprehensive guide to installing and running the programs necessary to create and run Perl and CGI files on Windows.

Lately I’ve been listening to Build & Analyze quite often, and one of the things that Dan Benjamin and Marco Arment have talked about recently (in episodes 18-21—I’m listening to Build & Analyze from the first episode on) is building a special kind of CMS, or Content Management System. I will not go into the details of their CMS design here, for brevities sake, but I will say that it has inspired me to do some programming recently. Since Python is my favorite language to create webpages with, I began looking for a way to be able to run CGI files—which are files that are written in Python, suffixed with .cgi instead of .py, and are executable by browsers—on my computer in order to work on designing my own site and with a CMS of my own design.

By far the most popular solution to the problem of being able to run CGI files, as well as Perl and PHP files, natively is to use a program from the Apache Software Foundation called Apache. Apache is a web server that can be installed on Windows, Mac, and Linux machines, thus giving programmers the ability to run programs as if they were hosted on a web server natively and without an internet connection. Apache, if configured correctly, solves this problem very well and works for many people. However, it is not exactly simple to configure Apache and make it work consistently, so after trying to work with Apache for quite some time I decided to look for an alternative. While researching some of the problems I was having with Apache, I heard various mentions of something called XAMPP. I had used XAMPP in the past, but could not remember exactly what it did, so it was not until I got tired of trying to work out Apache’s bugs that I looked into it. XAMPP is made by a group called Apache Friends, which is a group created in 2002 in order to promote Apache and make it easier to use. The XAMPP website describes XAMPP as “…an easy to install Apache distribution containing MySQL, PHP and Perl. XAMPP is really very easy to install and to use – just download, extract and start.” You can check out the Apache Friends website here: Apache Friends home page. After reading a bit about XAMPP, I decided to give it a try since I could not find anything else that looked as promising. The rest of this article will be devoted to going through the process of installing XAMPP and giving you the ability to not only run Perl and PHP scripts natively, which is an ability that comes from installing XAMPP alone, but also to give you the ability to write, edit, debug, and run not only Python programs, but also C programs as well as many others.

Here’s a list of programs I will recommend that you download throughout this article:

You can choose either to download them now, wait and see what I recommend them for and then download them, or not download them at all; however, I do strongly recommend that you download each of the programs I specified if you are planning on programming regularly.

Alright then, the first step is to download the XAMPP installer. I have a link in my list of recommended software, so go to the download page and download the XAMPP installer for your particular operating system: Windows, Max, Linux, or Solaris. Once the installer has finished downloading, proceed to the next step. Note that I am running Windows though, so this guide will be tailored to those running Windows as well. However, the programming concepts and example code presented throughout this article are applicable regardless of the operating system you are running.

Now that the XAMPP installer has been downloaded, open it up. Note: if you are running Windows Vista (or Windows 7) it would be wise to install XAMPP in a different folder than Program Files or Program Files (x86) as doing so can cause problems with file permissions and such. I recommend that you install XAMPP in C:/xampp, which is the default installation folder. Once you have chosen the installation folder, click Next and choose where icons for XAMPP will be placed. Due to my personal fetish of having my desktop cluttered, I only chose to place an icon in my start menu. Once you are finished in this screen, click Next again. When prompted to choose which programs you would like to install (You will have the choices of Apache, MySql, FileZilla, and Mercury) un-check all but Apache unless you know for sure that you want to install another one of the programs. After choosing, click Next and the installation process will begin.

The XAMPP installation process could take a few minutes here, so now would be a great time to get a head-start on the second step of this tutorial. If you are not planning on running CGI files, you can skip this step and move ahead; however, if you wish to be able to run CGI files natively, you must follow these steps:

  1. Go to python.organd download the latest version of Python (currently 3.1.4). When I installed Python earlier today, I opted for version 2.7 instead of the latest version simply because it is what I had used before. The version you choose is all up to you, and both will work equally as well.
  2. Once the Python installer has finished downloading, open it. Follow the on-screen prompts and install Python with the default preferences. If prompted, be sure to install the Python IDE as it can be very helpful when working with Python files. Once the installation of Python is completed, or during the installation, check back on your XAMPP installation to see how it’s going.

Once the XAMPP installation is finished, click the Finish button. A pop-up dialog box should come up asking you whether you want to start the XAMPP control panel now, or do it later. Start it now by clicking the “Yes” button. After clicking Yes, another dialog box will come up, this time with a warning telling you to be sure only to run the XAMPP Control Panel from the XAMPP root directory, which it lists underneath the warning. Make a note of the XAMPP root directory—it should be C:/xampp—as you will need to run the XAMPP Control Panel from this directory in the future. Upon clicking OK, the XAMPP Control Panel will open. It should look like this, or pretty close:

xampp_control_panel

The XAMPP Control Panel can be somewhat confusing at first, so here’s a quick rundown of what you should be seeing: Apache, MySql, FileZilla, and Mercury are names of programs that you could have installed during the XAMPP installation process. I installed both Apache and MySql, and as you can see from the picture above, both are running—running programs are denoted by highlighted text reading “Running” next to the program. If I were to stop one of those programs, the text would disappear. For the most part, you will not be using this window a whole lot: the XAMPP Control Panel is most useful for making sure that programs you installed through XAMPP are running, and to open the XAMPP root directory, which can be done by clicking the button labeled “Explore…”.

Once you have finished exploring the XAMPP Control Panel, you can either close it or minimize it, either way you will still be able to run you Perl or PHP files.

Before going on to my explanation of how to run CGI files with XAMPP, I am going to run through an option step. Even though this step is optional, I highly recommend that you follow it if you are planning on doing very much programming, regardless of the language, on a system with Windows installed. This option recommendation is to install Cygwin. Now for those of you that do not know what Cygwin is or what it does, check out my post Installing Cygwin for more information on it, as well as an installation guide and some basic usage instructions. I recommend that you install Cygwin because you can run, debug, and edit your files, regardless of the language, in it. If you do not have Cygwin installed, it will not be possible to debug CGI or Perl files very easily, if at all. Note, though, that the Python IDE will give you the same ability to run CGI (but not Perl or other types of programs) files before testing them out in your browser. However, I recommend that you download Cygwin to do this instead of using the Python IDE for these reasons: Cygwin will give you access to a Unix-like terminal and quite a few of the Unix terminal commands as well, and because Cygwin will allow you to compile (if needed) and run quite a a number of different kinds of files, including Python, Perl, C, and many others natively. These two abilities provided by Cygwin make debugging much easier and streamlines the entire development process in general. For the rest of this article, I will assume that you have installed Cygwin. Again, for a guide on installing Cygwin, as well as a basic usage guide, check out my article Installing Cygwin.

The next step in this process is to open an existing CGI file, or create a new one. I prefer Notepad++ on Windows, and gedit on Linux, but you can use any program you want to do this. First, then, open a new file. Before typing anything into it, save it. The location that you save it to is extremely important: you must save it to the folder named cgi-bin within the XAMPP root folder (the XAMPP root directory is C:/xampp by default, so the cgi-bin folder is, by default, C:/xampp/cgi-bin. If you chose to install XAMPP in a folder other than the default, this does not apply to you.) If you do not save the CGI (or Perl) file in the cgi-bin folder within the XAMPP root folder, the file will not execute. Once you have saved the file to the cgi-bin folder, you are now ready to start making your first program.

This concludes the first part of this article, the purpose of which was to provide you with the tools and the knowledge to obtain the necessary programs and run them correctly in order to be able to run CGI and Perl programs natively. The second part of this article will walk you through the basics of both Perl and Python, as well as show you how to run them natively with your XAMPP installation.

Comments are closed.

Follow

Get every new post delivered to your Inbox.