Getting Perl working on IIS7 with FastCGI [Answered]RSS

20 replies

Last post Aug 19, 2011 08:37 AM by HCamper

  • Getting Perl working on IIS7 with FastCGI

    Oct 12, 2007 01:49 PM|CosmicNetworks|LINK

    Hi All,
      There doesn't appear to be a guide anywhere for this on the net. I've found a few posts about it but nothing leading to a solution. So here goes. I'm going to post how far I've got, how I did it, a few questions, and hopefully, with a bit of luck, together we'll be able to figure it out.

     
    First of all lets get ActivePerl installed and working as a normal CGI. Download from ActiveState http://www.activestate.com/store/activeperl/download/

    I'm running Vista Home Premium 64 bit, I've found ActivePerl 64 bit to be incomplete for my needs, so instead I've installed the 32 bit perl 5.8.
    Make a note of where you have installed Perl, I installed in c:\Perl. If you install somewhere different you'll need to adjust the examples below.
     

    When I installed IIS7 I made sure I had the complete kit checked, with CGI, etc.

    1. Check IIS is running properly by visiting http://localhost in your browser.
    2. Check Perl is working properly by running "perl -v" in a command prompt.
    3. Open the IIS manager "Control Panel->System and Maintenance->Administrative Tools->Internet Information Services (IIS) Manager"
    4. Under the Connections tab on the left click on your computers name. Then double click "Handler Mappings"
    5. Under the Actions tab on the right click "Add script map"
    6. Request path: *.pl
      Executable: c:\Perl\bin\perl.exe "%s" %s
      Name: Perl Script (PL)
      Click OK
      Click Yes on the "enable ISAPI message"
    7. For people running 64bit Windows like me you'll get an error saying illegal characters. Enter the executable as just "c:\Perl\bin\perl.exe" and we'll correct this later.
    8. Click "add script map" again and enter:-
      Request path: *.cgi
      Executable: c:\Perl\bin\perl.exe "%s" %s
      Name: Perl Script (CGI)
      Click OK
      Click Yes on the "enable ISAPI message"
    9. Under the Connections tab on the left click on your computers name again.
    10. Under the Actions tab on the right (under Manage server) click Restart.

    Those running Windows 64 bit need to now take these steps to get Perl working properly...

    1. Edit the file C:\Windows\System32\inetsrv\config\applicationHost.config
    2. Find the line:-
      <add name="Perl Script (PL)" path="*.pl" verb="*" modules="CgiModule" scriptProcessor="c:\perl\bin\perl.exe" resourceType="File" preCondition="bitness32" />
    3. Change scriptProcessor="c:\perl\bin\perl.exe" to scriptProcessor='c:\perl\bin\perl.exe "%s" %s'
      and remove preCondition="bitness32"
    4. Make sure you swap the DOUBLE quotes " around the filename path for single quotes ' like in the example above.
    5. Repeat this for the Perl Script (CGI).
    6. Now find the line:-
      <add path="c:\Perl\bin\perl.exe" allowed="true" />
    7. Change this to:-
    8. <add path='c:\Perl\bin\perl.exe "%s" %s' allowed="true" />
    9. Save the changes.
    10. Restart IIS.

    Voila! Perl.exe working correctly. (please test your is working before you continue)

    Now on to FastCGI...

    Download the FastCGI update:

    32bit http://www.iis.net/downloads/default.aspx?tabid=34&i=1299&g=6
    64bit http://www.iis.net/downloads/default.aspx?tabid=34&i=1343&g=6

    Unzip to a temp folder, I used c:\temp\FCGI

    Follow the readme for your version, mine was:-

      fcgisetup.exe /install (from the command prompt)

    this didn't work, so set "User account control" to off for my user (which was Administrator). Had to restart, but when I tried again it worked fine. (If anyone can tell me how to get this to work with User account control turned on that'd be great)

    I downloaded and installed PHP (php.net), so I could verify FastCGI was indeed working:-

    fcgisetup.exe /add c:\php\php-cgi.exe PHP

    This worked fine, so now getting Perl to work:-

    fcgisetup.exe /add c:\perl\bin\perl.exe FCGI

    Seems to be added ok.

    Perl doesn't come with the standard FastCGI modules by default, so from the command prompt run:-

    "ppm install FCGI" 

    Here are two testing scripts, I named the first hello.fcgi:-

    #!/perl

    use FCGI;

    my $count = 0;
    my $request = FCGI::Request();

    while($request->Accept() >= 0) {
        print("Content-type: text/html\r\n\r\n", ++$count);
    }

     

    And the other fast.fcgi 

    #!/perl

    use CGI::Fast qw(:standard);
    $COUNTER = 0;
    while (new CGI::Fast) {
        print header;
        print start_html("Fast CGI Rocks");
        print
          h1("Fast CGI Rocks"),
          "Invocation number ",b($COUNTER++),
          " PID ",b($$),".",
          hr;
        print end_html;
    }

     

    I've verified that both of these work, by testing them on anther windows machine running a different webserver with FastCGI support. 

    I put them in c:\Inetpub\wwwroot

    Tried in browser "http://localhost/fast.fcgi"

    Unfortunately this gives the plain old error 500 :( I've hit a dead end, feel like I'm close, but not sure what to do now. Please let me know your thoughts, I'll let you know if I figure anything else out.
     

  • Re: Getting Perl working on IIS7 with FastCGI

    Oct 12, 2007 02:51 PM|andrew.sears|LINK

    Hi CosmicNetworks,

     

    I think you may be running into some of the same issues with Perl that I saw with Rails. One of the key points was making sure the right arguments were passed to the script interpreter. There's a discussion about it in this thread.


    1. Make sure you're really using TP2. Per MVolo, the download may be giving you parts of TP1. Check the version of C:\windows\system32\inetsrv\iisfcgi.dll. It should be 7.0.6001.16659 from 9/16/2007. If you have the one from January, you're on TP1. You can find the right DLL in the 2K8 ISO, or email me and I will send you a copy. TP1 does not support arguments.
    2. Once you know you've got TP2, update your applicationHost.config and web.config to append arguments as needed. They should look something like this: <application fullPath="c:\perl\bin\perl.exe" arguments="&quot;%s&quot; %s" /> and <add name="fcgi-perl" path="*.fcgi" verb="*" modules="FastCgiModule" scriptProcessor="c:\perl\bin\perl.exe|&quot;%s&quot; %s" resourceType="Unspecified" />
    3. Get ProcMon. Set the filter to include ProcessName = perl.exe. Include the Command Line column. Set it to capture events before you test your FCGI page. This will show you that perl actually ran, and let you see what arguments it received.

    Now, some good news and bad:

    Using the above steps, Perl still fails. Using ProcMon, you can see that Perl spawns with the right arguments, spins around for a second, tries to find a file located at C:\Perl\bin\%s, and then dies with exit code 9. Not good. It's clearly still expecting the actual file path to come in as the argument.

    Indeed, if you change the arguments to "c:\inetpub\wwwroot\hello.fcgi", it actually works. It runs hello.fcgi every time, and the counter increments as expected, but there is no mapping between the URL you typed and file that's loaded. It's not a solution, but it narrows the issue down a good bit.

    So it seems like the task from here is to figure out how to get Perl to read the desired input file from the FCGI socket instead of expecting it to come in through the args.
     

  • Re: Getting Perl working on IIS7 with FastCGI

    Oct 12, 2007 04:48 PM|andrew.sears|LINK

    So, I guess there are two distinct ways you might expect to use FastCGI with a script interpreter.

    1. Load the interpreter once, and load a different script each time. This makes sense if you only want to pay the price of loading the EXE into memory once, and you want a drop-in replacement for a suite of existing scripts that otherwise would have been handled via CGI or ISAPI. PHP FastCGI works this way, and the Perl ISAPI extension does effectively the same thing. If this is what you want, I would probably stick with the Perl ISAPI extension. It's plenty fast and reliable.

    2. Load the interpreter and load one script, and then process each request with a loop iteration. All of the Perl FastCGI examples I've found online seem to have this in mind. The use case is a particular script with a very complex initialization followed by a request loop, that you explicitly design for use with FastCGI. If this is what you want, the method listed in my previous post would certainly work. It's a bit clumsy in that you have to create a different handler and application for every file. You would also want to change the "request path" to an exact file name, e.g. "hello.fcgi". Maybe this can be simplified with a custom module, I don't know.

    Getting method 1 might require some creative solutions. One way to do this would be to basically use a wrapper script. Create c:\perl\wrapper.fcgi as follows:

    use FCGI;
    my $request = FCGI::Request();
    while($request->Accept() >= 0) {
     do($ENV{SCRIPT_FILENAME});
    }

    Change your scriptProcessor to "c:\perl\bin\perl.exe|c:\perl\wrapper.fcgi" and update your app arguments accordingly. Change the request path from *.fcgi to *.pl, or *.cgi, etc, and hit a normal Perl script.

    I'm not saying this is a clean solution, but it works. At least for my test script; I have no idea how this would fare with a real application.

  • Re: Getting Perl working on IIS7 with FastCGI

    Oct 14, 2007 12:54 AM|andrew.sears|LINK

    After some more testing, the picture doesn't look too rosy.

    My FCGI wrapper works in general, but when I try to plug it into real apps (eg YaBB), everything just 500s. I don't really use Perl myself, so I'll leave it to someone with more expertise to trace through the failures, if they care to.

    Also, ActiveState's Perl ISAPI extension (perlis.dll) seems to be borked. If you don't explicitly add an HTTP 200 to your response headers, IIS doesn't send a status code at all. Also, if you hit a script with a syntax error, instead of throwing an HTTP 500, w3wp.exe crashes. Yikes!

  • Re: Getting Perl working on IIS7 with FastCGI

    Oct 14, 2007 04:32 PM|CosmicNetworks|LINK

    Hi Andrew,
      Thanks for all your help. I'll work through this and try out some things and hopefully come up with something. Did you get my email about that file? I could do with a copy of the new one as I do indeed have the old one.


    Lyle
     

  • Re: Getting Perl working on IIS7 with FastCGI

    Oct 16, 2007 08:18 PM|CosmicNetworks|LINK

     Got the updated iisfcgi.dll files, but unfortunately they do not work on Vista's IIS7. Guess that's why a new download hasn't been release yet as it isn't a simple case of re-packaging...

      I've got W2k8 64 running now, so I'll do some testing on that and get back to you with what I find.

     

    Lyle
     

  • Re: Getting Perl working on IIS7 with FastCGI

    Oct 18, 2007 09:48 PM|CosmicNetworks|LINK

    Hi All,
      Thank you very much to "Andrew Sears" who gave me a great deal of help, putting me on the right track to making a solution to this.

    Now I've released FCGI::IIS on CPAN, 3rd revision so far.

    I've got it working on Windows 2003, and Windows 2008. Vista IIS7 is pending an update from Microsoft for me to be able to try it out. (hint, hint, still wan the Vista version of the new iisfcgi.dll)

    It's working (mostly) for perl scripts that want to run a bit faster on IIS using FastCGI (no FastCGI code needed in the actual scripts, so this should work with almost any windows compatible perl script) I've not put together a guide for running your Perl FastCGI scripts in their own application pool... yet...

    Either way, I'm quite chuffed with it. Looking at the code of the module, it looks like hardly anything, but figuring it all out and testing it has taken ages.

    Guide to get it working is at:-
    http://www.cosmicscripts.com/servers/fastcgi.html#iis


    Lyle



     

  • Re: Getting Perl working on IIS7 with FastCGI

    Oct 19, 2007 12:14 PM|andrew.sears|LINK

    Hi Lyle,

    Thanks for the update. I tried your module on 2008, with a script that is known to work through CGI. "test" works, "do" returns a 500, and "eval" gives me this:

    Error! Can't locate object method "FILENO" via package "FCGI::Stream" at c:/perl/lib/CGI.pm line 794. Compilation failed in require at (eval 2) line 23. BEGIN failed--compilation aborted at (eval 2) line 23.

  • Re: Getting Perl working on IIS7 with FastCGI

    Oct 20, 2007 06:34 PM|CosmicNetworks|LINK

    Working on an update.. should have one in the next day or so. Any chance you could give me the script you tested?

     

    Lyle 

  • Re: Getting Perl working on IIS7 with FastCGI

    Oct 20, 2007 09:08 PM|andrew.sears|LINK

    I was using this script to test:

    http://www.scatterchart.com/statistical_calculator/
  • Re: Getting Perl working on IIS7 with FastCGI

    Oct 22, 2007 02:33 AM|Diederichh|LINK

    Hi,

     thanks for FCGI:IIS and the guide!

     I try to use otrs from orts.org. Scripts word in CGI Mode (Script map to perl.exe "%s" %s)

     Using FastCGI in eval-mode I am getting:

    Error! Can't locate object method "FILENO" via package "FCGI::Stream" at c:/perl/lib/CGI.pm line 794. Compilation failed in require at C:\Programme\OTRS\otrs/Kernel/System/Web/Request.pm line 66. BEGIN failed--compilation aborted at C:\Programme\OTRS\otrs/Kernel/System/Web/Request.pm line 66. Compilation failed in require at C:\Programme\OTRS\otrs/Kernel/System/Web/InterfaceAgent.pm line 26. BEGIN failed--compilation aborted at C:\Programme\OTRS\otrs/Kernel/System/Web/InterfaceAgent.pm line 26. Compilation failed in require at (eval 6) line 43. BEGIN failed--compilation aborted at (eval 6) line 43. 

     is there any workaround known yet?

    henning 

  • Re: Getting Perl working on IIS7 with FastCGI

    Oct 26, 2007 10:40 AM|CosmicNetworks|LINK

     Hi All,

      This is a problem with the original FCGI.pm, I've been in contact with Sven and we are working toward a solution. Hopefully there will an update soon. In the mean time, you can try editing:-

    c:\perl\site\lib\FCGI.pm and uncommenting the lines:-

    sub FILENO {
      return -2;
    }

     

    To see if that gets you any further. Also download my latest FCGI::IIS as it has some other improvements. 

     

     Lyle

  • Re: Getting Perl working on IIS7 with FastCGI

    Nov 01, 2007 12:44 PM|Diederichh|LINK

     Hi Lyle,

     

    that doesn't work in my case. Could you make the Script avaible: http://www.cosmicscripts.com/servers/hello.fcgi.txt ? (always getting a 404)

    I have uncommented the lines you discribed above, but now i am getting

    The FastCGI Handler was unable to process the request.

    Error Details:

    • Error Number: 13 (0x8007000d).
    • Error Description: The data is invalid.

    Do you have an Idea? I am using IIS6 on an x86 machine

    Henning 

  • Re: Getting Perl working on IIS7 with FastCGI

    Nov 01, 2007 05:55 PM|CosmicNetworks|LINK

     Hi Henning,

      Thanks for pointing that out. I've now fixed that link. Still working toward a fix for the module. It's looking like I'm going to gave to learn the FastCGI protocol inside out (which isn't really a bad thing) and the FCGI module (which uses C libraries). To really understand what is going on. I'll post my finding here.

     

    Lyle
     

  • Re: Getting Perl working on IIS7 with FastCGI

    Nov 13, 2007 08:22 AM|Diederichh|LINK

    Hi Lyle,thanks for your work. I have testet your hello.fcgi Script, In testmode it works fine, but in Do or Eval mode I am Always getting an FCGI Error:Error Details:
    • The FastCGI process exceeded configured activity timeout
    • Error Number: 258 (0x80070102).

    I have monitored the thread behaviour by process monitor from sysinternals. The perl.exe thread is created with "c:\perl\bin\perl.exe" -MFCGI::IIS=eval, the Script Hello.fcgi is never pointed. After 30secs I’m getting the error described above. If I am configure the Script-mapping to "perl.exe "%s" %s" or "perlis.dll" the Script works as expected.

     

    Do you know where my mistake is? 

     

    Henning  
  • Re: Getting Perl working on IIS7 with FastCGI

    Nov 15, 2007 09:39 AM|Diederichh|LINK

    update:

    if I use the folowing configuration in fcgiext.ini your demo Script runs as expected:

    [c:\perl\bin\perl.exe]
    Arguments="C:\Inetpub\wwwroot\Hallo.fcgi"
    QueueLength=999
    MaxInstances=20
    InstanceMaxRequests=500
     

    if I point the ini-file to my Application (OTRS from OTRS.org I am only getting a blank Screen) 

    but every request now pointing on this Script. Do you have any updated information for me?

     Henning  

  • Re: Getting Perl working on IIS7 with FastCGI

    Dec 05, 2007 05:23 PM|kingdutka|LINK

    I am stuck on Step 6. I am in the IIS Information Manager, clicked on my computer, and clicked "Add Script Map" and filled out the form as described in step 6, push 'Ok' and then 'Yes', but then I get an error message:

    "One or more of the modules specified for this handler does not exist in the modules list. If you are trying to add a script map the IsapiModule or the CgiModule is not present in the modules list."

     

    I am running Windows Vista Ultimate 64bit. I had my YaBB forum running on XP Pro w/SP2 and I just can't seem to figure this out...

  • Re: Getting Perl working on IIS7 with FastCGI

    Dec 07, 2007 05:20 PM|anilr|LINK

    What vista build are you on? (rtm, sp1 beta, sp1 rc1?) - you need to choose "Add Module mapping" and choose FastCgiModule - the "add scriptmap" dialog only allows mappings for CGI/ISAPI apps, not for fastcgi apps.

    Anil Ruia
    Software Design Engineer
    IIS Core Server
  • Re: Getting Perl working on IIS7 with FastCGI

    Dec 07, 2007 09:19 PM|kingdutka|LINK

    I don't know which Vista built I am on, but it is 64bit Ultimate Edition.

     I guess I don't need 'fast' CGI, I don't even know what that is. All I know, is I had my website with a Perl based YaBB forum running at home on my DSL connection with Windows XP and the IIS server it came with. In the IIS Manager, I would right-click my 'cgi-bin' folder and go to 'properties' and set it to "read", "write" and "execute" and that was it, my site was working.

    When I right-click my cgi-bin folder in Vista, there is no "properties" option. All of the properties are in the big window in the middle of my screen to the right of the folder views and I can't find anything relating to "read" "write" "execute" permissions. All I know is that I tried to follow the debugging steps given to me by my "IIS 7.0 Detailed Error" and, following the steps it gave, I was led here...

     I opened a cmd and verified that perl is working: perl -v (it is!)

    So my problem is in getting my website to actually USE the perl scripts. I may as well include a full detailed report for you:

     This is BEFORE trying to add perl as a "Module Map" (I tried every type of module in my list...)

    HTTP Error 404.3 - Not Found

    Description: The page you are requesting cannot be served because of the Multipurpose Internet Mail Extensions (MIME) map policy that is configured on the Web server. The page you requested has a file name extension that is not recognized, and is not allowed.

    Error Code: 0x80070032

    Notification: ExecuteRequestHandler

    Module: StaticFileModule

    Requested URL: http://192.168.254.10:80/cgi-bin/yabb2/YaBB.pl

    Physical Path: C:\Web\cgi-bin\yabb2\YaBB.pl

    Logon User: Anonymous

    Logon Method: Anonymous

    Handler: StaticFile

    Most likely causes:

    • It is possible that a handler mapping is missing. By default, the static file handler processes all content.
    • The feature you are trying to use may not be installed.
    • The appropriate MIME map is not enabled for the Web site or application. (Warning: Do not create a MIME map for content that users should not download, such as .ASPX pages or .config files.)

     

    Here is AFTER adding Perl as a "Module Mapping":

     

    HTTP Error 404.3 - Not Found

    Description: The page you are requesting cannot be served because of the Multipurpose Internet Mail Extensions (MIME) map policy that is configured on the Web server. The page you requested has a file name extension that is not recognized, and is not allowed.

    Error Code: 0x80070032

    Notification: ExecuteRequestHandler

    Module: StaticFileModule

    Requested URL: http://192.168.254.10:80/cgi-bin/yabb2/YaBB.pl

    Physical Path: C:\Web\cgi-bin\yabb2\YaBB.pl

    Logon User: Anonymous

    Logon Method: Anonymous

    Handler: Perl Script (PL)

    Most likely causes:

    • It is possible that a handler mapping is missing. By default, the static file handler processes all content.
    • The feature you are trying to use may not be installed.
    • The appropriate MIME map is not enabled for the Web site or application. (Warning: Do not create a MIME map for content that users should not download, such as .ASPX pages or .config files.)

    In the "Module Mapping" window, there is a drop-down menu for "Module". I tried every one in the list and got mixed results, but nothing worked. I could include all those error descriptions if you need them.

  • Re: Getting Perl working on IIS7 with FastCGI

    Dec 10, 2007 12:58 PM|anilr|LINK

    Well, you are in the wrong forum then.

    To map .pl to perl CGI - first make sure that CGI feature (under IIS) is installed in add/remove windows features and then just add a scripmap mapping .pl to "path to perl.exe %s %s" - this should do the trick.

    Anil Ruia
    Software Design Engineer
    IIS Core Server
  • Re: Getting Perl working on IIS7 with FastCGI

    Aug 19, 2011 08:37 AM|HCamper|LINK

    Hi,

    Anir's suggestion:

    To map .pl to perl CGI - first make sure that CGI feature (under IIS) is installed in add/remove windows

     features and then just add a scripmap mapping .pl to "path to perl.exe %s %s" - this should do the trick.

    Works for me : http://forums.iis.net/t/1186738.aspx  IIS Forums Anil Rua tthreads &  posts.

    Steve Schofield's Blog   http://www.iislogs.com/steveschofield/hosting-perl-on-iis-7.x-thread tips and IIS Forums collection.

    Cheers: ) 

    Martin

     

     

    Vista EXE fascgi PERL FASTCGI python iis 7 http windows 7 Perl cgi-bin Information

    Windows and Linux work Together IT-Pros
    Community Member Award 2011