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: http://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:
- Getting the Windows Azure Prerequisites via the Microsoft Web Platform Installer 2.0
- Deploying Your First PHP Application With the Windows Azure Command Line Tools for PHP
- Deploying Your First PHP Application to Windows Azure
Some things you will need to get PHP running in Windows Azure:
- A Windows Azure subscription. If you don’t already have one, you can create one here: http://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 http://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).
- The Windows Azure SDK, which you can download here: http://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.
To deploy PHP in Azure, you need to upload two files to an Azure hosted service:
- A package file (.cspkg) that contains the PHP binaries, 3 configuration files (.cscfg, .roleconfig, and Web.config), and your application files.
- 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…
…select Hosted Services…
…provide a Service Label and Service Description…
…provide a URL and specify the region in which you want to create the service…
…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:
Now do the following:
- Create a file called Web.roleconfig in the WebRole folder and add the following code to it:
<application fullPath="%RoleRoot%\approot\php\php-cgi.exe" />
- Create a file called Web.config in the WebRole folder and add the following code to it:
<add name="FastGGI Handler"
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:
<ServiceConfiguration serviceName="ExampleAzureApp" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration">
<Instances count="1" />
<Setting name="DiagnosticsConnectionString" value="UseDevelopmentStorage=true" />
The service configuration file specifies the name of the service role (WebRole) and the number of instances we want to run (1).
- 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="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition">
<WebRole name="WebRole" enableNativeCodeExecution="true">
<InputEndpoint name="HttpIn" protocol="http" port="80" />
<Setting name="DiagnosticsConnectionString" />
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:
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
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
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
After a short wait, your application should be running in a browser.
To shutdown the simulated environment, execute the following:
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):
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:
Browse to the ServiceDefinition.cspkg and ServiceConfiguration.cscfg files in your ExampleAzureApp folder, specify a deployment name, and click Deploy:
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"):
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.