Hello World Azure in PHP

Building on the Windows Azure Command Line Tools blog post, I thought we could kick it up a notch and get PHP running in Azure leveraging the command line tools. The primary thing that we need to do is to copy the PHP bits up with the rest of your deployment and configure the FastCGI handler to know where the PHP interpreter can be found.

If you have PHP installed, locate your directory. Otherwise, download one of the versions from http://windows.php.net/download/. I haven’t personally tested it with every possibly distribution but it should work with all of them up there.

To get started, setup a simple folder structure as before but with the addition of a folder called PHP:

>Project
        >WebRole
                 >php

Now copy the contents of the PHP download or your PHP install to the php folder.

Now add a file called index.php to the WebRole directory:

<html>
<head><title>Hello World PHP</title></head>
<body>

<?php
echo 'Today is '. date('Y-m-d') ."\n";
?>

</body>
</html>

The one other actual difference between the HTML version and the PHP version of this little hello world sample is that you need to add one more file to the WebRole directory called web.roleconfig.

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

On your own IIS server you would call this the web.config but in Azure, it’s called the web.roleconfig.

Now, just like in the other sample you need to set up your Config and Definition files.

Simple.csdef

<?xml version="1.0" encoding="utf-8"?>
<ServiceDefinition name="Simple" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition">
  <WebRole name="WebRole" enableNativeCodeExecution="true">
    <ConfigurationSettings>
    </ConfigurationSettings>
    <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>

Simple.cscfg

<?xml version="1.0"?>
<ServiceConfiguration serviceName="Simple" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration">
  <Role name="WebRole">
    <Instances count="1"/>
    <ConfigurationSettings>
    </ConfigurationSettings>
  </Role>
</ServiceConfiguration>

Package everything up with cspack.

c:\Project>cspack simple.csdef /copyonly
Windows(R) Azure(TM) Packaging Tool version 1.0.0.0
for Microsoft(R) .NET Framework 3.5
Copyright (c) Microsoft Corporation. All rights reserved.

And run in the dev fabric with csrun.

c:\Project>csrun simple.csx simple.cscfg
Windows(R) Azure(TM) Desktop Execution Tool version 1.0.0.0
for Microsoft(R) .NET Framework 3.5
Copyright (c) Microsoft Corporation. All rights reserved.

Using session id 1
Created deployment(31)
Started deployment(31)
Deployment input endpoint HttpIn of role WebRole at http://127.0.0.1:82/

Now you are ready to run and test

image 

And that’s all that there really is to getting PHP running in Azure. Soon I’ll blog about getting a full blown app running in Azure. Stay tuned.