How to configure Apache server to run Perl CGI files

Topics on common programming languages
Post Reply
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

How to configure Apache server to run Perl CGI files

Post by Neo » Wed Apr 06, 2011 3:41 pm

  1. Install Perl under Windows. See How to install Perl under Windows XP.
  2. Activating CGI
    Using Notepad (or other text editor) open httpd.conf (should be start-menu shortcut "Apache HTTP Server 2.2 > Configure Apache Server > Edit the Apache httpd.conf Configuration File") and search for Options Indexes FollowSymLinks (about line 190) when you find it add ExecCGI to the end so it looks like Options Indexes FollowSymLinks ExecCGI
  3. Enabling CGI in any directory (OPTIONAL)
    If you want to use CGI outside the C:/Apache2_2/cgi-bin/ ScriptAliased directory, you will need to uncomment the following line: #AddHandler cgi-script .cgi becomes AddHandler cgi-script .cgi (remove the #) I also added .pl behind .cgi so 'perl' extension is also treated as cgi files.

    If you will be creating your own cgi-bin, you will want to comment out: ScriptAlias /cgi-bin/ "C:/Program Files/Apache Software Foundation/Apache2.2/cgi-bin/" so it becomes #ScriptAlias /cgi-bin/ "C:/Program Files/Apache Software Foundation/Apache2.2/cgi-bin/"

    Finding your location to perl
    If you do not know where your perl.exe installed to, go to Start -> Search and type in a search for perl.exe This location is the path to perl you put on the top of all your cgi scripts. If you have followed the ROBOT.LK manual to install ActivePerl, then the path should be C:/Perl/bin/perl.exe

    Some notes
    For the perl path C:/Perl/bin/perl.exe all of these are/were valid. I prefer the last one because of the Linux <<>> Windows portability.
    #!C:/Perl/bin/perl.exe
    #!C:/Perl/bin/perl
    #!/Perl/bin/perl.exe
    #!/Perl/bin/perl
  4. Testing CGI
    If you have uncommented (removed the # symbol) the line AddHandler cgi-script .cgi in step 3, then create a file in your document_root called hello.cgi and put these three lines in it (if you did not comment/disable it, put the CGI file in C:/Apache2_2/cgi-bin/):

    Code: Select all

    #!/usr/bin/perl
    print "Content-type:text/html\n\n";
    print "hello world";
  5. Restart Apache if it is already running. Now go to http://localhost/cgi-bin/hello.cgi (or wherever you put the file) and run the script.
  6. If you get a hello world in your browser, CGI is running. If you get a 500 error, go to the last entry in Apache error log (Apache HTTP Server 2.2 > Review Server Log Files > Review Error Log shortcut in the Start Menu) to see exactly what caused this error.
Post Reply

Return to “.Net & Other Programming”