Using 3rd Party Programming Languages via FastCGI

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=1.0 encoding=utf-8?>

<ServiceDefinition name=MyService xmlns=http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition>

  <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: http://support.microsoft.com/kb/967131.

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

<?xml version=1.0 encoding=utf-8 ?>

<configuration>

  <system.webServer>

    <fastCgi>

      <application fullPath=%RoleRoot%\php\php-cgi.exe />

    </fastCgi>

  </system.webServer>

</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 http://php.net.

4) Finally, modify your Web.config to configure your handlers.  This tells us which paths (e.g. *.php) map to the FastCGI application:

<configuration>

  <system.webServer>

    <handlers>

      <add name=PHP via FastCGI

           path=*.php

           verb=*

           modules=FastCgiModule

           scriptProcessor=%RoleRoot%\php\php-cgi.exe

           resourceType=Unspecified />

    </handlers>
  </
system.webServer>

</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!