PHP FastCGI on Windows Azure

After downloading the latest Azure Developer Kits (March 2009 CTP), I am very excited to let you know that Windows Azure offers native code execution, including support for FastCGI and full trust. As developers, we can choose to write services in a native language such as C or C++ and publish them to Windows Azure in the same manner as any other service, but to be more specific, below is its native execution supports:

- Windows Azure supports the IIS 7.0 FastCGI module.
- We can host web roles that require an native interpreter like PHP.
- We can host web roles and worker roles written in managed code under Windows Azure full trust.
- A role running under full trust can call .NET libraries, read the local server registry, and call native code libraries.
- We can develop a service in native code and test and publish this service to Windows Azure.
- A role running native code can interact with the Windows Azure runtime

Sure, I will show you how to start working with PHP in Windows Azure. What you need to do before being able to follow this article is (I assumed you already have VS 2008 SP1 installed):

- Install the Hotfix: Support for FastCGI on Development Fabric
- Install the Hotfix: Native Debugging Improvements
- Install the Hotfix: Improve Visual Studio Stability
- Install the Windows Azure SDK (March 2009 CTP)
- Install the Windows Azure Tools for VS 2008 (March 09 CTP)

If you have installed previous version of Windows Azure SDK and Tools, please un-install it before then install the hotfixes before the new Windows Azure SDK and Tools. If you are done, you can move forward.


Hosting FastCGI Applications

Windows Azure supports IIS 7.0 FastCGI module, so we can host web roles that call applications written in native interpreted languages, such as PHP. To host a web role that runs FastCGI on Windows Azure, we will follow the steps below.

First, create a Cloud Service - Web Cloud Service project in VS 2008, then go to Service Definition file to set the enableNativeCodeExecution attribute to true. It will look like this:

01

<?xml version="1.0" encoding="utf-8"?>
<ServiceDefinition name="PHPAzure" 
xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition"> <WebRole name="WebRole" enableNativeCodeExecution ="true"> <InputEndpoints> <!-- Must use port 80 for http and port 443 for https when running in the cloud --> <InputEndpoint name="HttpIn" protocol="http" port="80" /> </InputEndpoints> </WebRole> </ServiceDefinition>


To enable FastCGI, add a web.roleConfig file in the root of the Web Role project, and specify which FastCGI application we will be using.  Please Note that web.roleConfig is an optional file; it's only needed if we wish to enable FastCGI. After add web.roleConfig file, specify the absolute path to the PHP FastCGI application (php-cgi.exe).

To specify the absolute path, precede the interpreter file name with the %RoleRoot% environment variable. The %RoleRoot% variable returns the absolute path to the directory in which the role is running. This directory corresponds to the root of web role project, the same location as the project's web.config file and the web.roleConfig file. Below is the config for PHP FastCGI application:

<?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 our 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. After downloaded, extract it do the %RoleRoot%.

Once enabled FastCGI, we need to configure handlers for a web role running a PHP FastCGI application by adding them to the system.webServer section of our project's web.config file. The FastCGI module will then handle each *.php request type. To add handlers to the project's web.config file, locate the handlers element of the system.webServer element, and add PHP handlers within this section. Below is our web.config file looks like after modified to handle .php file extension :

<?xmlversion="1.0"?>
<
configuration>
  <
system.webServer>
    <
handlers>
      <
addname="PHP via FastCGI"
           path="*.php"
           verb="*"
           modules="FastCgiModule"
           scriptProcessor="%RoleRoot%\php\php-cgi.exe"
           resourceType="Unspecified" />
    </
handlers>
  </
system.webServer>
</
configuration>

Now you are ready to create a PHP page. Lets make simple page using phpinfo().

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>PHP on Azure</title>
</head>

<body>
<?php phpinfo();?>

</body>

</html>

So our project will look like below:

04  06

Normally we should be able to invoke our FastCGI application using Visual Studio Debug Menu (Control-F5) and get the PHP welcome page.  If you prefer to try without Visual Studio, Ryan Dunn has provide this cool manual way using *.bat files.  You can change number of web role instances via a simple modification to the Service Configuration file <Instances count="3"/>, see get more scale.

Note : Becareful with some thread safe PHP extensions in you php.ini file, as it may cause APPCRASH like below. Let me know if you have this problem too.

02  03

For further exploration, I do recommend you this MIX 09 presentation : Building Web Application with Windows Azure. In his presentation, Steve Marx did a very cool PHP on Azure demo, Tweval. That is a real deployed PHP sample on Windows Azure.

07

Hope this helps!.

 

Cheers – RAM

Published 03-29-2009 1:39 AM by risman