Using 3rd Party Programming Languages via FastCGI

Posted on 18 March, 2009

One of our goals for MIX was to give our developers more flexibility while ensuring they still benefit from the unique time-saving deployment, monitoring, and management features of Windows Azure  One way in which we went about this was to enable IIS FastCGI module in the Web role  This module enables developers to deploy and run applications written with 3rd party programming languages such as PHP

How to Get Started

1) As with the NET full trust example, the first step is to set the enableNativeCodeExecution attribute to true in your Service Definition file:

<?xml version="10" encoding="utf-8"?>

<ServiceDefinition name="MyService" xmlns="">

  <WebRole name="WebRole" enableNativeCodeExecution="true">

    <InputEndpoints>

      <InputEndpoint name="HttpIn" protocol="http" port="80" />

    </InputEndpoints>

  </WebRole>

</ServiceDefinition>

2) Install the following required fix for FastCGI in the Windows Azure Development Fabric: https://supportmicrosoftcom/kb/967131

3) To enable FastCGI, include a Webroleconfig file in the root of your project, telling us which FastCGI application you will be using  For example, for PHP:

<?xml version="10" encoding="utf-8" ?>

<configuration>

  <systemwebServer>

    <fastCgi>

      <application fullPath="%RoleRoot%\php\php-cgiexe" />

    </fastCgi>

  </systemwebServer>

</configuration>

Note: The FastCGI application must be xcopy-deployable and contained in your project  In this case, it is in the “php” subdirectory and specified via the special %RoleRoot% environment variable  An xcopy-deployable version of PHP for Windows can be downloaded from https://phpnet

4) Finally, modify your Webconfig to configure your handlers  This tells us which paths (eg *php) map to the FastCGI application:

<configuration>

  <systemwebServer>

    <handlers>

      <add name="PHP via FastCGI"

           path="*php"

           verb="*"

           modules="FastCgiModule"

           scriptProcessor="%RoleRoot%\php\php-cgiexe"

           resourceType="Unspecified" />

    </handlers>
  </
systemwebServer>

</configuration>

Putting It All Together

Now, you should be able to invoke your FastCGI application  Here is an example of a multi-instance PHP-based application that has been one-click deployed to the Development Fabric (a simulation of the cloud environment)  It has been configured to use three Web role instances via a simple modification to the Service Configuration file:

 

As a bonus

  1. We have also enabled the IIS URL Rewrite Module  URL rewriting, a feature often used by FastCGI developers, enables the creation of URLs that are easier for users to remember and easier for search engines to find
  2. The Visual Studio Tools for Windows Azure includes a FastCGI Web Role that creates a Web Application project tailored to make it easier to configure, run and package a FastCGI application

For more detail, please refer to the documentation and FastCGI sample in the Windows Azure SDK  Enjoy!