This tutorial demonstrates how to use the Windows Azure SDK for PHP and the Windows Azure Command-Line Tools for PHP Developers to create, test, and deploy a PHP application to Windows Azure. While the Windows Azure SDK for PHP allows you to leverage the Table, Blob, and Queue storage services in Windows Azure, this tutorial will focus on using the Windows Azure Blob service. This tutorial parallels another tutorial (here) that builds a similar application using Visual Studio and ASP.NET.
Table of Contents ObjectivesPrerequisitesUnderstanding the ArchitectureUnderstanding the Blob ServicePart 1: Create Your Windows Azure PHP Project Configure Storage Information (storageConfig.php)Create the MessagePicture Class (MessagePicture.php)Create the MessageBoardEntry Class (MessageBoardEntry.php)Create Functionality for Retrieving Messages (getMessages.php)Build the Main page (index.php) HTMLJavascriptPHPCSSPart 2: Test Your Windows Azure PHP Project Testing in the Windows Azure Compute and Storage EmulatorsTest Using Live Windows Azure StoragePart 3: Deploy Your Application to Windows Azure Deploying to the Staging EnvironmentDeploying to the Windows Azure Production Environment
Note: If you wish to contribute to this page, use the Edit tab at the top of the page (sign-in required). If you wish to provide feedback on this page, use the comments field at the bottom of the page or send mail to azure@microsoft.com.
In this tutorial, you will create a simple golfer message board application. In the application, a Web role provides the front-end that allows golfers to view the contents of the message board and add new entries. Each entry contains a name and a message (stored in a Windows Azure Table) and an optional picture (stored in a Windows Azure Blob). When a golfer posts a new message, the Web role creates an entry using the Table service (containing message information. The Web role also renders this information to the browser so golfers can view the content of the message board.
Note: The tutorial code has been tested on Windows Azure SDK versions 1.3 and 1.4.20227.1419, the Windows Azure SDK for PHP version 3.0.0, and the Windows Azure Command-Line Tools for PHP Developers March 2011 Update.
In this tutorial, you will learn...
The following diagram illustrates the development and runtime components involved in this tutorial:
The Windows Azure Blob service enables applications to store large objects, up to 1TB each. It supports a massively scalable blob system via the Windows Azure CDN, where hot blobs will be served from many servers to scale out and meet the traffic needs of your application. Furthermore, the system is highly available and durable. You can always access your data from anywhere at any time, and the data is replicated at least 3 times for durability. In addition, strong consistency is provided to ensure that the object is immediately accessible once it is added or updated; a subsequent read will immediately see the changes made from a previously committed write.
An application must use a valid account to access Windows Azure Storage. An account can have many Blob containers. Each container provides a grouping of a set of blobs.
The only difference between developing a Windows Azure PHP project and any other PHP project is that a Windows Azure PHP project will leverage the classes that are in the Windows Azure SDK for PHP.
1. Create a directory for the application. In the root directory for IIS (C:\inetpub\wwwroot), create a directory called GolferMessageBoard.
2. Create the files for the application. This application will consist of 6 files: index.php, MessageBoardEntry.php, MessagePicture.php, getMessages.php, storageConfig.php, and main.css.
Note: You can download the source code for this application from the MSDN Code Gallery at Windows Azure Platform Tutorials - PHP. You directory should now look as follows:
The Windows Azure SDK for PHP contains classes for interacting with the Blob and Table services. The primary classes for doing so are the Microsoft_WindowsAzure_Storage_Blob and the Microsoft_WindowsAzure_Storage_Table classes. To instantiate these classes so that they can use the Blob and Table services, your account name and key are necessary. Since these classes will be used to create Blob and Table clients in multiple places in our application, put configuration information in the storageConfig.php file:
<?php define("STORAGE_ACCOUNT_NAME", "Your Storage Account Name"); define("STORAGE_ACCOUNT_KEY", "Your Storage Account Key");
define("PROD_SITE", false); define("BLOB_CONTAINER_NAME", "picturecontainer"); define("TABLE_NAME", "MessageBoardEntry");
?>
Note the following about the code above:
The MessagePicture class is a class that contains information for saving a picture (associated with a particular message) to a Windows Azure Blob. To create this class, add the following code to the MessagePicture.php file:
When a class name matches a table name and properties are annotated with comments correctly, the Windows Azure SDK for PHP will automatically map the class properties to the table. To do this for your application, add the following code to the MessageBoardEntry.php file:
<?php
require_once 'Microsoft\WindowsAzure\Storage\Table.php';
class MessageBoardEntry extends Microsoft_WindowsAzure_Storage_TableEntity
{
/**
* @azure golferName
*/
public $golferName;
* @azure golferMessage
public $golferMessage; /** * @azure pictureUrl */ public $pictureUrl = '';
function __construct()
$this->_partitionKey = date("mdY");
$this->_rowKey = trim(com_create_guid(), "{}");
}
function send()
include_once 'storageConfig.php';
$tableStorageClient->insertEntity(TABLE_NAME, $this);
To add functionality for retrieving messages from the MessageBoardEntry table, add the following code to the getMessages.php file:
The index.php file is the main page for the GolferMessageBoard application. It essentially has 3 sections, divided by language: PHP, Javascript, and HTML. Breaking these sections down one-by-one will help in understanding how the page works.
The body of the HTML markup is a form (for entering a new message) and a table (for displaying sent messages). After creating the basic layout of an HTML page in the index.php file (i.e. <html>, <head>, and <body> elements), replace the body element with the following:
Add the following <script> element to the <head> element of the index.php file to facilitate retrieval of Table messages and validation of form input:
<script type="text/javascript">
function getMessages()
xmlhttp=new XMLHttpRequest();
var url="getMessages.php";
xmlhttp.onreadystatechange=getMessagesInfoStateChanged;
xmlhttp.open("GET", url, true);
xmlhttp.send(null);
function getMessagesInfoStateChanged()
if (xmlhttp.readyState==4)
document.getElementById("tablerows").innerHTML =
'<div id="tablerows">' +
xmlhttp.responseText +
'</div>';
setTimeout('getMessages()', 60000);
function validateInput()
var name = document.getElementById('txtName');
var message = document.getElementById('txtMessage');
if(name.value == '' || message.value == '')
alert("Both Name and Message are required.");
return false;
return true;
</script>
PHP is used on the main page to handle form submission (sending of a message). To process a submitted form, add the following code at the top of theindex.php file:
How the HTML in this application is formatted is arbitrary and up to you. Add CCS code the main.css file as you see fit. If you choose, you can use the CSS code that is available in source code download for this application, available here: MSDN Code Gallery at Windows Azure Platform Tutorials - PHP.
Your application is now ready for testing.
In this section you will test your application in two phases. In the first phase the application will run locally in the Windows Azure Compute Emulator and messages and pictures will be stored in the Windows Azure Storage Emulator. In the second phase, the application will run in the Compute Emulator, but messages and pictures will be stored to a live Windows Azure Storage account. When testing is complete, you will deploy the application to a Windows Azure hosted service.
To run your application locally, follow these steps:
1. In a command prompt, navigate to your Windows Azure Command-Line Tools for PHP Developers directory and execute the following command without line breaks:
>php package.php --project=GolferMessageBoard
--source="c:\inetpub\wwwroot\GolferMessageBoard"
--phpRuntime="C:\Program Files (x86)\PHP\v5.3.4"
--target="c:\workspace"
--runDevFabric
Note: You may need to change values for the source and phpRuntime parameters if your application source code and PHP installation are in different directories.
Note: All paths in your php.ini file need to be relative before deploying an application to Windows Azure. (e.g. extension_dir='.\ext' instead of extension_dir='c:\PHP\ext'). Now is a good time to make sure that all paths are relative.
2. When you are prompted to provide administrator privilege, click Yes (you may be prompted more than once):
3. You should now see an icon for the Compute Emulator in your system tray. Right click the icon and select Start Storage Emulator:
4. You should now be able to access your running application at this URL: http://127.0.0.1:81/.
To test your application using a live Windows Storage account, you need to first create a storage account. To create a storage account for the golfer message board application…
Name
Value
Choose a subscription
(select a subscription that is associated with the storage account)
Enter a URL
(e.g.<yourname>gmb)
Choose a region or affinity group
(select Create a new affinity group. In Affinity Group Name, type (for example) ag<yourname>GMB; in Location, choose the region where you wish your service to run, most likely, the one that is closest to your current location, and then click OK.)
Now you can configure your application to use your live storage account. Open the storageConfig.php file, enter your storage account name, your storage account key, and define PROD_SITE to be true:
define("STORAGE_ACCOUNT_NAME", "Your_Account_Name_Here");
define("STORAGE_ACCOUNT_KEY", "Your_Account_Key_Here");
define("PROD_SITE", true);
Be sure to save the file after you make changes.
Now you can follow the instructions in the Testing in the Windows Azure Compute and Storage Emulators section above, but you can skip step 3 since you will not be using the Storage Emulator.
In this section you will deploy the GolferMesageBoard application to the Windows Azure staging environment, then move it to the production environment.
To deploy your application to the Windows Azure staging environment, follow these steps:
1. Assuming that you made some changes to your application in testing, you will need to rebuild the deployment package created by the Windows Azure Command-Line Tools. To do this, in a command prompt navigate to the Windows Azure Command-Line Tools directory and execute the following command (without line breaks):
--cleanRebuild
The command above will create the following directory: c:\workspace\GolferMessageBoard_Build. That directory will contain two directories:GolferMessageBoard and GolferMessageBoard_WebRole. You will need the GolferMessageBoard.cspkg file and ServiceConfiguration.cscfg file in the GolferMessageBoard directory for deployment.
2. Return to the Windows Azure portal and click on New Hosted Service. In the resulting dialog, do the following:
a. Choose a subscription.
b. Enter a name for your service.
c. Enter a unique URL for your service.
d. Select Create or Choose an affinity group. Select the affinity group that you created when creating a storage account.
e. Select Deploy to stage environment.
f. Make sure that Start after successful deployment is checked.
g. Enter a deployment name.
h. For Package location, browse to the GolferMessageBoard.cspgk file that you created in the previous step.
i. For Configuration file, browse to the ServiceConfiguration.cscfg file that you created in the previous step.
j. Click OK.
It will take several minutes for your application to upload, deploy, and start. When the portal indicates that your application is in the ready state, you can access it at the URL provided in the DNS name property for the deployment (on the right hand column of the portal).
To promote the application to production…
Note: Some DNS services take longer to replicate the records. If you get a page not found error, you might need to try browsing to the URL again in a few minutes.
Congratulations! You have completed the Using the Windows Azure Web Role and Windows Azure Blob Service with PHP tutorial.