Running a PHP application on Windows Azure
I recently returned from MIX09 – what a fantastic event! So many great things were announced; so many that there’s no way I can list them all here. Please visit http://live.visitmix.com/ for access to the keynote videos and all the breakout sessions. I would also recommend reading the day one recap and the day two recap.
One of the major announcements was new capabilities for the Windows Azure CTP. This includes the support for FastCGI, Full Trust, and Geo-location. Please take a look at the Windows Azure team’s blog for all the announcements.
In this post, I want to walk you through the step you need to take to run a PHP application on Windows Azure. As you’ll see, it’s quite simple and straightforward – after a few configuration updates and additional steps, you can run nearly any PHP application. This walkthrough assumes that you have installed the latest bits for Windows Azure. If you are just getting started with Azure, please visit http://www.azure.com/ – this is the primary landing page for the Azure Services Platform. If you want to get the updated bits that allows for Full Trust and FastCGI, you can grab the bits at the Windows Azure Tools for Microsoft Visual Studio March 2009 CTP page.
- Create a new project. Select File –> New –> Project.
- Select Cloud Service project type and select the Blank Cloud Service template. Specify the name/location, and click OK.

- Add a Cgi Web Role project. Right-click the project (e.g. CloudService1) and select New Web Role Project.

- Specify the Name and click OK.

- By default the Web.roleconfig file is opened in the new project. Remove the comments and update the application fullPath to “%RoleRoot%\php\php-cgi.exe”.

- Now we need to actually grab the PHP executable that will bet invoked. Browse to http://www.php.net/downloads.php and grab the non-thread-safe Windows binary (at the time of writing, it is “PHP 5.2.9-1 Non-thread-safe zip package”).
- You might as well configure PHP to run on your Windows environment. Rather than specifying all these steps, please see Simon Guest’s post on hosting PHP applications in 5 easy steps. Follow the steps in his post, and at the end you should be able to confirm that PHP works in IIS 7.
- Copy all the files in the \PHP folder you downloaded into your Cgi Web Role project. XCopy works really well. For example, open a command prompt in the root of your Cgi Web Role project and type:
xcopy /s \php php
When asked, specify “D for directory”. This will copy everything from c:\php into a folder called php in your Cgi Web Role project. Note that this is where we pointed Windows Azure to in step 5. - Select your Cgi Web Role project, show all files, and add the new php folder into your project. This way it will get packaged up with the rest of your application and deployed to Windows Azure.
- Open up the Web.config file and specify a new default document. This will tell Windows Azure to open up our PHP page by default. Under the <system.webServer> element, add a <defaultDocument>:

- Additionally, we need to setup a handler in the Web.config file. Add the following code to the <handlers> element in the web.Config:

- In order for this to function, we have to enable native code execution in Windows Azure. To do so, open the ServiceDefinition.csdef file and set the enableNativeCodeExecutiion flag to true:

- Now, let’s create a new PHP file. Right-click on your Cgi Web Role and add a new item. Select the Text File template and change the name to index.php. Click Add.

- To test, create some PHP code. A quick, but effective, test is to simply echo “Hello World”. In the index.php file, write the following code:

- Press F5, and your browser should open and show you the following page:

And that’s all it takes!
While this is a simple demonstration, I hope it impresses upon you the impact that allowing native code execution has in Windows Azure. With native code execution, and fast CGI support, you can now leverage all the rich content created in the PHP community on Windows Azure. The story even gets better with the new relational capabilities of SQL Data Services, but I’ll save that for another post.
I hope this helps!
Wade Wegner works for Microsoft and is the Technical Evangelist for Windows Azure AppFabric.
Could you add some more detail. like how can you handle path. to different catalogues? and could you grab a screen from php_info(); ?
and is there any way to code in php for Azure whit out visual studio?
PackageValidationFailure – BadUrl
What do I do?
I’ll need more information to provide any kind of assistance. Can you please provide a little more context? Did you follow the steps exactly as I in this post?
What happend with your promise that you will post the code from your latest talk about AZURE at CNUG .NET user group?
Are you going to post the code samples or not?
thanks.
You know, I simply forgot! I’m on the road this week, but as soon as I get home I’ll will post the code.
Thanks for keeping me honest!
I was getting the “PackageValidationFailure – BadUrl” because I had changed the port to 443 and it was supposed to be 80
InputEndpoint name=”HttpIn” protocol=”http” port=”80″
Now I am trying to figue out the database stuff. I have some user names and passwords for SQL services and do not know what to do with them
Hi, I did everything as explained in here. index.php is working on localhost. But when I deploy my project on Azure, page could not be opened. I got the error like this:
404 – File or directory not found.
The resource you are looking for might have been removed, had its name changed, or is temporarily unavailable.
Any suggestions?
Hi Bek -
Can you zip up your entire solution so that I can take a look? You can send the zip to wwegner 23 – at – hotmail – dot – com.
Thanks,
Wade
Hi. I’v encountered with same issue. Works fine on localhost, but “File or directory not found” if project deployed on Azure.
Hi Oleg,
I’ve solved this problem. Check your php folder. It should be properly packaged into your project. Otherwise, php will not be deployed into Azure.
[...] Wade Wegner’s Post on ‘Running a PHP application on Windows Azure’ [...]
There is a typo in the above tutorial.
When adding the handlers at Step 11.
There is a missing “*” on path attribute.
Thus, the correct input for step 11 should be as follows:
[...] Wade Wegner’s Post on ‘Running a PHP application on Windows Azure’ [...]
@Bill – you’re absolutely right – I neglected to put a “*” in the path for the “FastCGI Handler.” I’ve updated the post. Thanks so much, and I apologize for the error!
Thanks for you time to write this, its hard to find some really useful info about php and azure.
1 thing, in Step 11, may be you forgot to put “\approot\” in scriptProcessor=”%RoleRoot%\php\php-cgi.exe”?
If I don’t use regular PHP from the php.net site the php page gets displayed in the development fabric (when published to Windows Azure I get a 403 error like some of the others, I guess the CGI executable is not properly packaged, may be).
If I use the non-thread safe file then I can only see the directory listing although default page has been defined.
I made a mistake of typing FastGGI instead of FastCGI and it still compliled without errors and dsiaplyed the directory structure.
If I read the instructions bundled with the download there are a few more changes for the php.ini file that you don’t mention.
Is there a final version of this somewhere?