Share via


How to Run PHP in Windows Azure

In my last post I described how to access SQL Azure from PHP, but I described how to do this when PHP was running on my local server. In other words, I only described how to leverage half the cloud. In this post, I’ll leverage the other half of the cloud by describing how to run PHP in Windows Azure. Once again, I won’t go into the benefits of cloud computing here, but will instead point you (again) to this Wikipedia article for information: Cloud Computing.

One acknowledgement before I get started: I’m basing much of the content here on a blog post by Josh Holmes: Hello World Azure in PHP. I only ran into a couple of minor problems, which Josh graciously helped me work through. In exploring his blog further, I found lots of other interesting and useful posts…check it out: https://www.joshholmes.com/.

Note (Dec. 30, 2010) : I wrote this post before exploring the Windows Azure Command Line Tools for PHP. These tools handle much of the tedious work described below (such as creating configuration files and including your PHP installation). If you are looking for tutorials that use the Windows Azure Command Line Tools for PHP, I highly suggest the following articles:

Requirements

Some things you will need to get PHP running in Windows Azure:

  1. A Windows Azure subscription. If you don’t already have one, you can create one here: https://www.microsoft.com/windowsazure/offers/. (You’ll need a Windows Live ID to sign up.) I purchased the Windows Azure Platform Introductory Special, which allows me to get started for free as long as keep my usage limited. (This is a limited offer. For complete pricing details, see https://www.microsoft.com/windowsazure/pricing/.) After you purchase your subscription, you will have to activate it before you can begin using it (activation instructions will be provided in an e-mail after signing up).
  2. The Windows Azure SDK, which you can download here: https://www.microsoft.com/downloads/en/details.aspx?FamilyID=7a1089b6-4050-4307-86c4-9dadaa5ed018. In this post, you will only use two command line tools available in this SDK, but the complete documentation is available here: Windows Azure SDK.

 

Overview

To deploy PHP in Azure, you need to upload two files to an Azure hosted service:

  1. A package file (.cspkg) that contains the PHP binaries, 3 configuration files (.cscfg, .roleconfig, and Web.config), and your application files.
  2. A service definition file (.csdef).

A command line tool, cspack.exe (in the Windows Azure SDK), makes creation of the .cspkg file easy. I will provide example configuration and service definition files later in this post (and they are also available in the .zip file attached to this post). These files can be modified for your particular application.

Another command line tool, csrun.exe (also in the Azure SDK), will allow us to run your application in a simulated Azure environment before uploading it to the hosted service.

If you are someone who prefers to not work from the command line, you can use Visual Web Developer Express (which is a free download) to package an application for testing and/or deployment

 

Creating an Azure Hosted Service

Once you have a Windows Azure subscription, you need to create a hosted service. To do this, go to the Windows Azure developer portal, click on New Service

NewService

…select Hosted Services

image

…provide a Service Label and Service Description

image

…provide a URL and specify the region in which you want to create the service…

image 

…click Create, and you’re finished. You’ll come back to this portal when you are ready to deploy your application.

 

Creating the Configuration Files

Before creating the necessary configuration files, create a directory on your C:\ drive (or wherever you want) like this:

>ExampleAzureApp

          >WebRole

Now do the following:

  • Create a file called Web.roleconfig in the WebRole folder and add the following code to it:

<?xml version="1.0"?>

<configuration>

  <system.webServer>

    <fastCgi>

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

    </fastCgi>

  </system.webServer>

</configuration>

  • Create a file called Web.config in the WebRole folder and add the following code to it:

<?xml version="1.0"?>

<configuration>

    <system.webServer>

      <defaultDocument>

        <files>

          <add value="ExampleForm.php"/>

        </files>

      </defaultDocument>

      <handlers>

        <add name="FastGGI Handler"

             verb="*"

             path="*.php"

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

             modules="FastCgiModule"

             resourceType="Unspecified" />

      </handlers>

    </system.webServer>

</configuration>

These two files specify PHP as the FastCGI handler and ExampleForm.php as the default document in the application.

Two more files to go…

  • Create a file called ServiceConfiguration.cscfg in the ExampleAzureApp folder and add the following code to it:

<?xml version="1.0"?>

<ServiceConfiguration serviceName="ExampleAzureApp" xmlns="https://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration">

  <Role name="WebRole">

    <Instances count="1" />

    <ConfigurationSettings>

      <Setting name="DiagnosticsConnectionString" value="UseDevelopmentStorage=true" />

    </ConfigurationSettings>

  </Role>

</ServiceConfiguration>

The service configuration file specifies the name of the service role (WebRole) and the number of instances we want to run (1).

Finally…

  • Create a file called ServiceDefinition.csdef in the ExampleAzureApp directory and add the following code to it:

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

<ServiceDefinition name="ExampleAzureApp" xmlns="https://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition">

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

    <InputEndpoints>

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

    </InputEndpoints>

    <ConfigurationSettings>

      <Setting name="DiagnosticsConnectionString" />

    </ConfigurationSettings>

  </WebRole>

</ServiceDefinition>

The service definition file specifies that our application will be a Web Role (specified by the WebRole element) and that native code execution is allowed (so that PHP can run).

 

Adding PHP and Your Web Application

Copy your entire PHP installation to the WebRole folder in the directory we created above. And, copy your application files to the WebRole folder. My application consists of one form, ExampleForm.php. This is the same application I created in my last post and will only work if you have followed the instructions in that post. You can replace the PHP script with something even simpler than my mine (like a script that just calls phpinfo()) if you didn’t (or don’t want to) follow that post. Your directory should now look like this:

>ExampleAzureApp

          ServiceDefinition.csdef

          ServiceConfiguration.cscfg

>WebRole

                    Web.roleconfig

                    Web.config

                    PHP

Note: Make sure that paths in your php.ini file are relative paths. For example, the extension_dir directive should be set to something like "./ext", not an absolute path.

 

Packaging Your Application for Testing

Now you are ready to use the command line tools, cspack.exe and csrun.exe, to test your application in a simulated Azure environment.

Note: To use the command line tools you will need to open the command prompt with administrator privileges and you may need to add the directory that contains the command line tools to your PATH environment variable (instructions here can be applied to our situation). The default location for these tools is  C:\Program Files\Windows Azure SDK\v1.1\bin.

From the command line, execute the following (the /copyonly option specifies that the packaging is for testing only):

C:\ExampleAzureApp>cspack ServiceDefinition.csdef /copyonly

clip_image002

This created a ServiceDefinition.csx file in the ExampleAzureApp directory. Now, to test the application in a simulated Azure environment, execute the following:

C:\ExampleAzureApp>csrun ServiceDefinition.csx ServiceConfiguration.cscfg /launchbrowser

clip_image002[5]

After a short wait, your application should be running in a browser.

To shutdown the simulated environment, execute the following:

C:\ExampleAzureApp>csrun /devfabric:shutdown

clip_image002[7]

 

Packaging Your Application for Deployment to the Cloud

To create a package that you can deploy to the cloud, execute the following (again from a command prompt with administrator privileges):

C:\ExampleAzureApp>cspack ServiceDefinition.csdef

clip_image002[9]

This creates a ServiceDefinition.cspkg that you can upload to the hosted service you created earlier. Now return to the Windows Azure developer portal, navigate to the service you created earlier, and click Deploy:

clip_image002[11]

Browse to the ServiceDefinition.cspkg and ServiceConfiguration.cscfg files in your ExampleAzureApp folder, specify a deployment name, and click Deploy:

clip_image002[13]

It will take a few minutes for your application to deploy. When it does, click Run. It will take a few more minutes for your application to be available (it will go through phases of "Initializing", "Busy", and finally "Ready"):

clip_image002[15]

When you click on the provided Web Site URL, you should see your application running in the cloud!

All the files I’ve mentioned in this post are in the attached ExampleAzureApp.zip file. I have not included the PHP binaries, however. You’ll have to copy your own PHP installation to the empty PHP folder in the attached file.

In upcoming blog entries, I'll look into using the Windows Azure SDK for PHP to leverage more features of Windows Azure with PHP.

That’s it. Hope this was helpful…questions and comments welcome, as always.

Thanks.

-Brian

Share this on Twitter

ExampleAzureApp.zip

Comments

  • Anonymous
    December 07, 2010
    Thanks Brian. Your blog helped us a lot. We were trying to implement Joomla! on Windows Azure. Anybody who needs the steps can find it at: www.hooduku.com/.../joomla-on-windows-azure.html
  • Anonymous
    December 07, 2010
    The comment has been removed