The March CTP release of the Windows Azure SDK includes support for the IIS 7 FastCGI module which means that php applications can now be run in Windows Azure. I thought I’d take a look at how to set it up, I’ve not set up FastCGI on IIS7 on Windows Server before and I don't know anything about php, this is how I got it going.

I didn’t have php installed on my system so I installed it via the Microsoft Web Platform Installer 2.0 Beta

First I created a solution with no roles in using the Blank Cloud Service project in Visual Studio

image

Next I added a new web role to it by selecting the role folder in the cloud project, right clicking and then selecting add new web role project, I then selected the Cgi Web Role template

image

I then created a new php folder in my web role project and added the files from the c:\program files\PHP folder. In order to get the php runtime files part of my deployment I also selected all the files and changed their build action to content and set them to be copied to the output directory if they changed

image

Next I edited the Web.roleconfig file like so:

<?xml version="1.0"?>
<configuration>
  <system.webServer>
    <fastCgi>
      <application fullPath="%RoleRoot%\php\php-cgi.exe" />
    </fastCgi>
  </system.webServer>
</configuration>

I also needed to enable Full trust code by editing the cloud projects ServiceDefinition.csdef file, adding the attribute enableNativeCodeExecution:

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

Finally in order to route requests for php files to the php interpreter I added the following to the handlers section in the  of the web.config file i my web role.

<add name="PHP FastGGI Handler"
             verb="*"
             path="*.php"
             scriptProcessor="%RoleRoot%\php\php-cgi.exe"
             modules="FastCgiModule"
             resourceType="Unspecified" />

At this point I was ready to try and run a php script, so created a new text file in my web role called hello.php and then added the following content:

<html>
 <head>
  <title>PHP Test</title>
 </head>
 <body>
 <?php echo '<p>Hello World</p>'; ?> 
 </body>
</html> 

Now I was ready to run my “app” so I hit F5 in visual studio, unfortunately I then received an error from php-cgi.exe saying that the application had failed to start because it could not find ntwdblib.dll, a few searches later and I found out the attempt to load this dll was because of a configuration entry in php.ini. (There is some guidance about how to configure php.ini on windows here)

I edited php.ini and commented out the SQL Server extension and the error was gone.

[PHP_MSSQL]
;extension=php_mssql.dll

I also created a second php script that called the php phpinfo() function, you can see that here

After I completed this I realised that a lot of php developers would not use Visual Studio, Ryan Dunn has a post explaining how get php going with only the Windows Azure SDK here

Technorati Tags: ,,