How to Add CGI Script Support to Apache on Windows

Test Your Perl/Python/etc CGI Scripts on Your Own Windows Apache Server


How to Add Perl CGI Script Support to Your Apache 1.x Web Server on Windows

by Christopher Heng, thesitewizard.com

Following thesitewizard.com's earlier articles on how to install the Apache 1.x web server on Windows and how to install PHP support, I have received numerous queries on how to add support for executing Perl scripts in a Windows installation of the Apache web server. This article explains what you need to do to make your Apache server run Perl CGI scripts (or Python or any other CGI script) on Windows systems.

If you missed the articles on installing Apache and PHP on Windows, you can find them at:

As mentioned in those articles, installing Apache on your local machine allows you to test your CGI and PHP scripts without having to go online and upload them to your web host. This makes for faster and easier development of your scripts.

Note: this article applies to the Apache 1.x web server (where "x" is some number). I have not tested the procedure below on later versions of Apache (since I mostly use PHP for web scripts nowadays). Chances are that it will not work on Apache 2 and later, since a lot of things have changed since the version 1 series. If you want an up-to-date way of installing Apache and integrating Perl and PHP into it, please see How to Install and Configure Apache, PHP, Perl and MySQL on Windows the Easy Way (with XAMPP) instead.

Installing Perl (for Perl users)

Obviously if you are going to run Perl scripts on your computer you will need to install Perl. You can obtain a Windows version of Perl free of charge from one of the sources listed on the Free Perl Executables page of thefreecountry.com (such as ActivePerl or Strawberry Perl).

Install Perl on your computer according to the instructions given by the implementation you downloaded.

Configuring Apache

  1. Running Perl Scripts in a CGI directory

    You can configure Apache to treat any file in a particular directory as a CGI script. Typically, web hosts call such a directory the cgi-bin directory.

    To configure Apache to treat a particular directory as your script directory, search for the following line in your "httpd.conf" file.

    For those who have forgotten where the "httpd.conf" file can be found, try looking for it in the "conf" directory of your Apache folder. If you used the default directories supplied by the Apache installer, it would be "c:\Program Files\Apache Group\Apache\conf\httpd.conf".

    ScriptAlias /cgi-bin/ "C:/Program Files/Apache Group/Apache/cgi-bin/"

    If it has been commented out, that is, if there is a hash mark ("#") before the line, remove the hash character to enable it. If it has not been commented out, it means that your Apache is already configured to run CGI scripts in that directory. You can change the directory to another directory if you wish.

  2. Running CGI scripts anywhere in your domain

    If you don't want to be restricted to running CGI scripts within the ScriptAlias directory in your domain, and want CGI scripts to run anywhere in your domain, add the following line to your "httpd.conf" file.

    AddHandler cgi-script .cgi

    You can add it yourself manually, but since the default httpd.conf file that is supplied by Apache already comes with that line commented out, the simplest thing would be to search for that string in your existing file, and remove the preceding comment character, that is, remove the "#".

    If you want the .pl extension recognised as a CGI script as well, simply append the extension to the list, as follows:

    AddHandler cgi-script .cgi .pl

    Next, search for the line that says "<Directory /> in the file. It should look something like this:

    <Directory />
      Options FollowSymLinks
      AllowOverride None
    </Directory>

    Add "+ExecCGI" to the options list. The line now looks like this:

    Options FollowSymLinks +ExecCGI

  3. Making a CGI Script Your Default Page

    If you want to make your CGI script execute as the default page for a directory, you have to add another line to the Apache configuration file (httpd.conf). Simply search for the line in the file that begins with a "DirectoryIndex" and add "index.cgi" to the list of files on that line. For example, if the line used to be:

    DirectoryIndex index.html

    change it to

    DirectoryIndex index.cgi index.html

    The next time you access "http://localhost/" or "http://localhost/directory/" without any filename specified, Apache will run "index.cgi" if available or deliver "index.html" if it cannot find "index.cgi". If you have both "index.cgi" and "index.html" in the directory, "index.cgi" is used since it's listed first.

    You are not limited to the above two files. For example, I have set up Apache on my computer with the following line:

    DirectoryIndex index.php index.cgi index.shtml index.html

    If you have also installed PHP support and enabled server side includes in Apache, the directive will instruct Apache to look first for an index.php file, or failing that an index.cgi file, or if that could not be found, an index.shtml file, and finally an index.html file if all else fails.

    That's it. Your Apache server now supports the execution of CGI scripts. You will have to restart the server before the new configuration comes into effect. The work's not ended however — read on.

Modifying Your CGI Script

As it stands, if your CGI script is a Windows executable, it would be executed by Apache correctly. However, if it's a script that relies on an interpreter such as Perl or Python, you will have to modify the first line of the script.

Your Perl script will typically contain an initial line of

#!/usr/bin/perl

or some such thing. This tells a Unix-based kernel to look for an interpreter at the path "/usr/bin/perl" and invoke it to interpret the instructions in the file.

Since the script now runs on Windows, and it is unlikely that you installed your Perl interpreter in a /usr/bin directory, you will need to change that first line to point to the real location of your Perl interpreter.

For example, on my Windows box, I have to change the first line of my Perl scripts to:

#!c:/program files/perl/bin/perl.exe

Of course if you are using other scripting languages, you will have to set that initial line to point to whatever interpreter you are using.

It's Worth It

In my opinion, it's actually worth your while to install Apache, Perl, PHP and MySQL on your own computer. Testing your scripts offline allows you to work out the most obvious bugs and problems in a setting where you don't have to worry about bringing down the real web server or incurring ISP and telephone charges. It also allows you to work more efficiently, since you can simply edit your scripts on your computer to instantly fix problems while testing, without having to re-upload them before testing again. Of course, you will still have to test it when you actually put the script on your site. But at least, by then, you would be more likely to have a script that works as advertised.

This article can be found at https://www.thesitewizard.com/archive/addcgitoapache.shtml

Copyright 2001-2018 by Christopher Heng. All rights reserved.
Get more free tips and articles like this, on web design, promotion, revenue and scripting, from https://www.thesitewizard.com/

thesitewizard™ News Feed (RSS Site Feed)  Subscribe to thesitewizard.com newsfeed

Do you find this article useful? You can learn of new articles and scripts that are published on thesitewizard.com by subscribing to the RSS feed. Simply point your RSS feed reader or a browser that supports RSS feeds at https://www.thesitewizard.com/thesitewizard.xml. You can read more about how to subscribe to RSS site feeds from my RSS FAQ.

Please Do Not Reprint This Article

This article is copyrighted. Please do not reproduce or distribute this article in whole or part, in any form.

Related Articles

New Articles

How to Link to This Page

It will appear on your page as:

How to Add Perl CGI Script Support to Your Apache Server on Windows





Home
Donate
Contact
Link to Us
No Spam Policy
Privacy Policy
Topics
Site Map

Getting Started
Web Design
Search Engines
Revenue Making
Domains
Web Hosting
Blogging
JavaScripts
PHP
Perl / CGI
HTML
CSS
.htaccess / Apache
Newsletters
General
Seasonal
Reviews
FAQs
Wizards

 

 
Free webmasters and programmers resources, scripts and tutorials
 
HowtoHaven.com: Free How-To Guides
 
Site Design Tips at thesitewizard.com
Find this site useful?
Please link to us.