Table of Contents OverviewObjectivesPrerequisitesUnderstanding the ArchitecturePart 1: Prepare Your SQL Database AccountPart 2: Create Your Windows Azure/SQL Database PHP Project Configure Storage Information (storageConfig.php)Create Functionality for Sending a Message (sendMessage.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 EmulatorsPart 3: Deploy Your Application to Windows Azure Deploying to the Staging EnvironmentDeploying to the Windows Azure Production Environment
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. When a golfer posts a new message, the Web role sends message information to SQL Database for permanent storage. The Web role also renders message information in the browser so golfers can view the content of messages.
Note: The tutorial code has been tested on Windows Azure SDK versions 1.3 and 1.4.20227.1419, the Microsoft Drivers for SQL Server for PHP v2.0, and the Windows Azure Command-Line Tools for PHP Developers March 2011 Update.
This tutorial builds an application with functionality that is the same as the application built in this tutorial: Using the Windows Azure Web Role and Windows Azure Table Service with PHP. However, in this tutorial, SQL Database is used as the back-end data store instead of the Table service. Completing the application that uses the Table Service is not a prerequisite for this tutorial.
Note: This tutorial is not intended to show you how to choose between SQL Database and the Table service, but to show you how to use SQL Database . For information on SQL Database and Windows Azure Table service comparison, see SQL Database and Windows Azure Table Service at, and Understanding Data Storage Offerings on the Windows Azure Platform.
In this tutorial, you will learn...
Note the following requirements before you begin this lesson:
In order to complete the deployment portion in thistutorial, you must sign up for a Windows Azure account and purchase a subscription. For more information, see Get Started with the Windows Azure Platform and Provisioning Windows Azure.
Download and install the Windows Azure Command-Line Tools for PHP Developers. Instructions for doing so can be found in the Setup Guide.
Note: The command line tools can also be installed with the Web Platform Installer. If you choose to use the Web PI, you can install PHP and configure IIS to handle PHP requests at the same time.
Download install the Microsoft Drivers for SQL Server for PHP. Installation instructions are here: http://msdn.microsoft.com/en-us/library/cc296173.aspx.
Configure IIS to run PHP applications.
Note: It is possible to use Apache to handle PHP requests in Windows Azure, but this tutorial will use IIS.
You must have a Windows Azure Platform subscription. For more information on creating a subscription, see Getting Started with SQL Database using the Windows Azure Platform Management Portal.
To create a SQL Database server
Administrator Username
MyAdmin
Administrator Password
<your password>
Retype Password
Note: An administrator account is a master account used to manage the new server. You should avoid using this account in connection strings where the username and password may be exposed. To simplify the tutorial instructions, this tutorial uses the administrator account. The default administrator username is MyAdmin, and the default password is pass@word1. If you change the username and the password in this step, you must change them accordingly in the rest of the tutorial.
Note: The password policy requires that this password contain at least one number, one character, one letter, and one symbol. In addition, the password cannot be less than six characters nor contain three consecutive characters from the username.
Rule name
The dev box
IP range start
(type the IP address of the computer you are using. The IP address is listed on bottom of the dialog.)
IP range end
(type the IP address of the computer you are using.)
Note: SQL Database has two types of access control: firewall and SQL authentication. You must configure the SQL Database firewall settings to allow connections from your computer(s). You must also allow connections from Windows Azure, because the golfer message board application is hosted in Windows Azure. For more information on SQL Database security, see Security Guidelines for SQL Database.
Important: In addition to configuring the SQL Database server-side firewall, you must also configure your client-side environment to allow outbound TCP connections over TCP port 1433. For more information, see Security Guidelines for SQL Database.
Login
Password
Allow other Windows Azure services to access this server
(selected)
You can use either SQL Server Management Studio or Windows Azure Platform Management Portal to manage your SQL Database database. To connect to SQL Database from SQL Server Management Studio, you must provide the fully qualified domain name of the server. In this tutorial, you will use the New Windows Azure Platform Management Portal.
Note: The SQL Server Management Studio from SQL Server 2008 R2 and SQL Server 2008 R2 Express can be used to access, configure, manage and administer SQL Database. Previous versions of SQL Server Management Studio are not supported.
To create a database
Server
(use default value)
Database
master
On the upper right corner of the tab, it lists the database server, database name, login user, and other information. Notice, the database is the master database.
SELECT * FROM sys.databases GO CREATE DATABASE GolferMessageBoardDB GO SELECT * FROM sys.databases; GO
Make sure the query is executed successfully. You should see two sets of results; the first set shows the master database, and the second set shows the master database and the GolferMessageBoardDB database.
The Golfer Message Board application has one table for storing the messages.
To create a table
GolferMessageBoardDB
pass@word1
Note: Make sure the database name is GolferMessageBoardDB. The default name is master. After you login, the server name, the database name, and the login username is shown on the upper right corner of the windows.
CREATE TABLE [Messages]( [MessageID] [int] IDENTITY(1,1) NOT NULL PRIMARY KEY, [GolferName] [nvarchar](100) NOT NULL, [GolferMessage] [nvarchar](1000) NOT NULL, [Timestamp] [datetime] NOT NULL )
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 5 files: index.php, sendMessage.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 information necessary for establishing a connection to SQL Database is stored in the storageConfig.php file. Add the following code to the storageConfig.php file:
Note the following about the code above:
To add functionality for retrieving messages from the GolferMessageBoardDB database, 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:
<body onload='getMessages()'>
<form method="post"
action="./index.php"
id="frmMessageBoard"
onsubmit='return validateInput()'>
<div class="general">
<div class="title">
<h1>Golfer Message Board</h1>
</div>
<div class="inputSection">
<dl>
<dt>
<label for="NameLabel">Name:</label>
</dt>
<dd>
<input name="txtName" type="text" id="txtName" class="field" />
</dd>
<label for="MessageLabel">Message:</label>
<textarea name="txtMessage"
rows="2"
cols="20"
id="txtMessage"
class="field"></textarea>
</dl>
<div class="submitSection">
<input type="submit" name="btnSend" value="Send" id="btnSend" />
<div id="upMessageBoard">
<table id='dlMessages'
cellspacing='0'
style='border-collapse:collapse;'>
<div id="tablerows">
</table>
</form>
</body>
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 by running it locally in the Windows Azure Compute Emulator. Note that messages will not be stored in the Windows Azure Storage Emulator, but will be stored in your live SQL Database GolferMessageBoardDB database. 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 ofextension_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 be able to access your running application at this URL: http://127.0.0.1:81/.
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 andServiceConfiguration.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 Choose a region. Choose the region closest to you.
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 Table Service with PHP tutorial.
A complete resource, with good explanation, images, etc. Fantastic!