Installing PHP on Windows Azure leveraging Full IIS Support: Part 3

Update: The Windows Azure PowerShell CmdLets have added the necessary bootstrap information in order to set up PHP on Windows Azure Cloud Services. Download the Windows Azure PowerShell CmdLets.

In my last two posts in this series [Part 1, Part 2] we have created a Start-up script to install PHP on our Windows Azure Web Role, we have created the necessary Cloud Service Definition and Cloud Service Configuration files.

In this post we’ll look at how to use the command-line tools for Windows Azure to Package our Deployment and how to deploy that package to the Windows Azure Platform Portal.

Packaging a Windows Azure PHP Deployment

I know what you’re thinking “Hold up, we need to package our deployment? What ever happened to good old FTP?”

FTP works great for a single point of access, but we’re looking to be able to rapidly deploy our application across many servers in a matter of minutes. Microsoft uses these packages for just that purpose, rapid deployment.

What is the Cloud Service Package (.cspkg)

The Cloud Service Package is essentially a Compressed file which holds your application, the Cloud Service Definition and a number of other files required by Windows Azure. These files are encrypted by the tool incase the package is intercepted when being transported over HTTP.

You are able to leave the cspkg unencrypted using one of two methods. I would suggest using the Environment Variable [setting _CSPACK_FORCE_NOENCRYPT_ to true], mostly due to the fact that there is a good chance you won’t be using MS Build for your PHP Application.

Setting an Application Variable

Open the Control Panel. Find and Click on System.

set environment variables

Click on the Advanced System Settings link in the Right hand Menu. This will open the System Properties Dialog.

image

Click on Environment Variables…

image

Then on New…

image

Creating a Build Script to Automate Deployment Packaging

As a developer, it is a good idea to optimize repetitive tasks in order to be more productive. One of these optimization points that you should be looking at is Build Automation. With PHP being a Scripting Language and does not need to be compiled this is a very simple thing to accomplish. Here is how simple your “Build Script” can be.

cspack ServiceDefinition.csdef

In our previous post we added the Windows Azure Command-Line Tool Path to our System Path. This enables the above script to execute the tools and create the Cloud Service Package.

To make your build script more powerful, you could add some additional functionality like executing a powershell script which leverages the Windows Azure Powershell CommandLets to deploy your latest Build to your Staging Environment in Windows Azure.

Deploying your Service Package to Windows Azure

There are many different means to getting your Windows Azure Project in to the Cloud. Once you’ve had a chance to evaluate the different methods I would suggest using the method that you find most suitable for you.

Here is a list of different methods of Deploying to Windows Azure:

Note: Barry Gervin and I have put together a video on how to deploy using the Windows Azure Platform Portal.

  • Deploying using Visual Studio

Test Startup Scripts Locally using RDP

As developers we all know that testing will always win out over trial and error, this is no different when creating a startup script for Windows Azure. Be sure to save yourself from pulling out your hair by testing you’re the executable files that will be called by your start-up tasks by RDPing into a Compute Instance.

I’ve created a few blog posts that will aid you in setting up RDP into Windows Azure:

Note: Installers that have a “Quiet Install” option (/q) in their command-line switches, are your absolute best friend. Otherwise, trying to mimic a users acceptance to a Eula or other dialog is rather difficult. WebPI allows us to accept a Eula with a switch (/AcceptEula)

The Ultimate Windows Azure Development VM

As a Software Developer we have many options when it comes to the tools that we put in our tool belt. One thing that I’ve found exceptionally useful over the years are Virtual Machines, not only Virtual Machines, but having a tailored environment to what you’re Developing.

With my focus on the Cloud I thought it would be useful to continue the trend of Building out a Virtualized Environment that’s Tailored to my work that I’m doing in the Windows Azure Platform. I’ve compiled a list of the Tools and SDKs in which I have found the most useful while working on projects for Windows Azure.

Operating System

  • Windows 7 [SP1]
  • Windows Server 2008 R2 [SP1]

Note: Windows Server 2008 R2 is a Handy OS to have on a Virtual Machine within your environment if you expect to have to use the VM Role in Windows Azure.

Desktop Backgrounds

Windows Add-Ons

Development Environment

IDE

SDKs

Extensions

Assessment & Optimization

Management & Debugging

Free

Paid

Code Samples

Bookmarks

Installing PHP on Windows Azure leveraging Full IIS Support: Part 2

Update: The Windows Azure PowerShell CmdLets have added the necessary bootstrap information in order to set up PHP on Windows Azure Cloud Services. Download the Windows Azure PowerShell CmdLets.

In the last post of this Series, Installing PHP on Windows Azure leveraging Full IIS Support: Part 1, we created a script to launch the Web Platform Installer Command-line tool in Windows Azure in a Command-line Script.

In this post we’ll be looking at creating the Service Definition and Service Configuration files which will describe what are deployment is to consist of to the Fabric Controller running in Windows Azure.

Creating a Windows Azure Service Definition

Unfortunately there isn’t a magical tool that will create a starter point for our Service Definition file, this is mostly due to the fact that Microsoft doesn’t know what to provide as a default. Windows Azure is all about Developer freedom, you create your application and let Microsoft worry about the infrastructure that it’s running on.

Luckily, Microsoft has documented the Service Definition (.csdef) file on MSDN, so we can use this documentation to guide us through the creation of our Service Definition. Let’s create a file called ‘ServiceDefinition.csdef’ outside of our Deployment folder. We’ll add the following content to the file, and I’ll explain a few of the key elements below.

Defining a Windows Azure Service

<?xml version="1.0" encoding="utf-8"?>
<ServiceDefinition name="PHPonAzure" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition">
	<WebRole name="Deployment" vmsize="ExtraSmall" enableNativeCodeExecution="true">
		<Startup>
			<Task commandLine="install-php.cmd" executionContext="elevated" taskType="background" />
		</Startup>
		<Endpoints>
			<InputEndpoint name="defaultHttpEndpoint" protocol="http" port="80"/>
		</Endpoints>
		<Imports>
			<Import moduleName="RemoteAccess"/>
			<Import moduleName="RemoteForwarder"/>
		</Imports>
		<Sites>
			<Site name="PHPApp" physicalDirectory=".\Deployment\Websites\MyPHPApp">
				<Bindings>
          				<Binding name="HttpEndpoint" endpointName="defaultHttpEndpoint" />
        				</Bindings>
      			</Site>
		</Sites>
	</WebRole>
</ServiceDefinition>

We will be using a Windows Azure WebRole to Host our application [remember WebRoles are IIS enabled], you’ll notice that our first element within our Service Definition is WebRole. Two Important pieces of the WebRole Element are the vmsize and enableNativeCodeExecution attributes. The VMSize Attribute hands off the VM Sizing requirements to the Fabric Controller so it can allocate our new WebRole. For those of you familiar with the .NET Stack the enabledNativeCodeExecution attribute will allow for FullTrust if set to true, or MediumTrust if set to false [For those of you that aren’t familiar, Here’s a Description of the Trust Levels in ASP.NET]. The PHP Modules for IIS need elevated privileges to run so we will need to set enableNativeCodeExecution to true.

In Part one of this series we created a command-line script that would initialize a PHP installation using WebPI. You’ll notice under the Startup Element, we’ve added our script to a list of Task Elements which defines the startup Tasks that are to be run on the Role. These scripts will run in the order stated with either limited [Standard User Access] or elevated [Administrator Access] permissions. The taskType determines how the Tasks are executed, there are three options simple, background and foreground. Our script will run in the background, this will allow us to RDP into our instance and check the Logs to ensure everything installed properly to test our deployment.

In the Service Definition above we’ve added some additional folders to our deployment, this is where we will be placing our website [in our case, we’re simply going to add an index.php file]. Within the Deployment Folder, add a new folder called Websites, within the new Websites folder, create a folder called MyPHPApp [or whatever you would like it named, be sure to modify the physicalDirectory attribute with the folder name].

Create a Websites Folder in the Deployment FolderCreate a MyPHPApp Folder within the Websites Folder

Now that our directories have been added, create a new file named index.php within the MyPHPApp folder and add the lines of code below.

<?php

phpinfo();

?>

Creating a Windows Azure Service Configuration

Now that we have a Service Definition to define the hard-requirements of our Service, we need to create a Service Configuration file to define the soft-requirements of our Service.

Microsoft has provided a way of creating a Service Configuration from our Service Definition to ensure we don’t miss any required elements.

If you intend to work with Windows Azure Tools on a regular basis, I would suggest adding the Path to the tools to your System Path, you can do this by executing the following script in a console window.

path=%path%;%ProgramFiles%\Windows Azure SDK\v1.3\bin;

We’re going to be using the CSPack tool to create our Service Configuration file. To Generate the Service Configuration we’ll need to open a console window and navigate to our project folder. Then we’ll execute the following command to create our Service Configuration (.cscfg) file.

cspack ServiceDefinition.csdef /generateConfigurationFile:ServiceConfiguration.cscfg

After you run this command take a look at your project folder, it should look relatively close to this:

Project after running CSPack to Create Configuration File

You’ll notice that executing CSPack has generated two files. First, It has generated our Service Configuration file, which is what we’re interested in. However, the tool has also gone and compiled our project into a Cloud Service Package (.cspkg) file, which is ready for deployment to Windows Azure [we’ll get back to the Cloud Service Package in the next post in this series]. Let’s take a look at the Configuration file.

<?xml version="1.0"?>
<ServiceConfiguration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" serviceName="PHPonAzure" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration">
  <Role name="Deployment">
    <ConfigurationSettings>
      <Setting name="Microsoft.WindowsAzure.Plugins.RemoteAccess.Enabled" value="" />
      <Setting name="Microsoft.WindowsAzure.Plugins.RemoteAccess.AccountUsername" value="" />
      <Setting name="Microsoft.WindowsAzure.Plugins.RemoteAccess.AccountEncryptedPassword" value="" />
      <Setting name="Microsoft.WindowsAzure.Plugins.RemoteAccess.AccountExpiration" value="" />
      <Setting name="Microsoft.WindowsAzure.Plugins.RemoteForwarder.Enabled" value="" />
    </ConfigurationSettings>
    <Instances count="1" />
    <Certificates>
      <Certificate name="Microsoft.WindowsAzure.Plugins.RemoteAccess.PasswordEncryption" thumbprint="0000000000000000000000000000000000000000" thumbprintAlgorithm="sha1" />
    </Certificates>
  </Role>
</ServiceConfiguration>

Where did all of this come from? Let’s look at a simple table, that matches up how these settings relate to our Service Definition.

Service Definition Snippet

Service Configuration Snippet

<ServiceDefinition name=”PHPonAzure”/> <ServiceConfiguration serviceName=”PHPonAzure”/>
<WebRole name=”Deployment”/> <Role name=”Deployment”/>
<Import moduleName=”RemoteForwarder”/> <Setting name=”RemoteForwarder.Enabled”/>
<Import moduleName=”RemoteAccess”/> <Setting name=”RemoteAccess.Enabled”/>

<Setting name=”RemoteAccess.AccountUsername”/>

<Setting name=”RemoteAccess.EncryptedPassword”/>

<Setting name=”RemoteAccess.AccountExpiration”/>

<Certificate name=”RemoteAccess.PasswordEncryption”/>

For more information on the RemoteAccess and RemoteForwarder check out the series that I did on RDP in a Windows Azure Instance. These posts will also take you through the instructions on how to provide proper values for the RemoteAccess and RemoteForwarder Elements that were Generated by the Import statements in the ServiceDefinition.

  1. Upload a Certification to an Azure Hosted Service
  2. Setting up RDP to a Windows Azure Instance: Part 1
  3. Setting up RDP to a Windows Azure Instance: Part 2
  4. Connecting to a Windows Azure Instance via RDP

There are two additional attributes in which I would recommend adding to the ServiceConfiguration Element, osFamily and osVersion.

osFamily="2" osVersion="*"

These attributes will change the underlying Operating System Image that Windows Azure runs to Windows Server 2008 R2 and sets your Role to automatically update to the new image when available.

You can control the number of instances of your Roles are deployed by changing the value of the count attribute in the Instances Element. For now we’ll leave this value at 1, but keep in mind that Microsoft’s SLA requires 2 instances of your role to be running in order to guarantee 99.95% uptime.

Great Resources

Conclusion

In this entry we created both a Service Definition and a Service Configuration. Service Definitions provide information to the Fabric Controller to create non-changeable configurations to a Windows Azure Role. The Service Configuration file will provide additional information to the Fabric Controller to manage aspects of the environment that may change over time. In the next post we will be reviewing the Cloud Service Package and Deploying our Cloud Service into the Windows Azure Environment.

Happy Clouding!

Golden Eggs of Windows Azure ‘Cloud’ Best Practices

Remembering back to when I was in the Audience in the Community, the one thing I always wanted to find out about was Best Practices for the Particular Technology I was listening about. I’m sure a number of people feel the same way.

Best Practices are hard to introduce in a talk as you’re typically only speaking to 10% (if that) of your audience and leaving the other 90% scratching their heads. For this reason, I am providing this blog post as a resource to find allow people interested in Best Practices to proactively seek them out with a little bit of Guidance.

Getting in the Know

Let’s face it, you may want to know the Best Practices from the beginning as you think you’re doing yourself a favour by knowing how to be a smooth operator. STOP, Let’s take a moment to step back and thing about this.

Just because something has been outlined as a best practice doesn’t guarentee that it is for your particular situation. Part of Identifying a Best Practice is knowing your options for a particular situation. Once  you know you have the right fit for your particular situation then you can extend your implementation to leverage a Best Practice to Guarentee that you’ve solidified your feature in it’s Best Possible Functioning Implementation.

My First Tip for seeking out best practices is to Know your Platform & Your Options.

There are a number of resources for getting to know Windows Azure on my blog [which I’ve recently installed Microsoft Translator to provide great content for all to read] and the Windows Azure Team Blog.

Further Research is Necessary

As good as the content is that you read online, you will want to turn to a number of printed [or electronic] books. Here are a few books that I would suggestion.

Manning Books – Azure In Action

AzureInAction

This book is not about Best Practices. However, it provides the best explanation of the Windows Azure Internals to date. The first few chapters provide insight into the Fabric Controller and the Windows Azure Fabric.

I would consider this an initial “Deep Dive” Starter read to get into Microsoft’s Cloud Computing initiative and an understanding of Windows Azure’s offerings in the Platform as a Service (PaaS) Cloud Space.

 

Microsoft Patterns & Practices

cat

My most recent read was Moving Applications to the Cloud by the Microsoft Pattern and Practices Team. This book was very insightful as to some of the Practices that Microsoft has been implementing while moving into the cloud, obviously obfuscated through a hypothetical company Adatum and their Expenses Tracking System aExpense.

This book got me thinking about a number of great architecture concepts and some great value add code that can be re-used over a number of Projects.

 

Developing-Applications-for-the-Cloud-on-Windows-Azure

I enjoyed the previous book so much that I will be picking up the other guidance book from Microsoft, Developing Applications for the Cloud.

I’m going out on a limb to say that based on the previous book, I’m betting that this book will be rather insightful, hopefully providing more guidance on Architecting Applications for Windows Azure.

 

 

The Cloud Developer Tool Belt Reference Guide

This next resource might not be something you will read end to end. I would say that this is definitely an item you should be referring to when designing your Cloud Architecture.

AzureScope: Benchmarking and Guidance for Windows Azure is an all encompassing guide for Best Practices, Benchmarks, Code Samples next to which my Essential Guide for Getting Started with Windows Azure post look like one of those rings you pick up at the cash register of your local dollar store.

XCG_Sharepoint_Header

Conclusion

Hopefully this post will help those in my audiences that I am unable to reach out to at this point with that Best Practice Deep Dive. I have no doubt that Cloud Computing is the future of Application Deployment and Hosting. I also believe that Microsoft is putting forth a very strong offering with Windows Azure. Regardless your technology, the Best Practices provided in this document will provide you with some thought provoking reading material which after all is most likely one of the main contributing factors in choosing Software Development as a Career.

Happy Clouding!

Installing PHP on Windows Azure leveraging Full IIS Support: Part 1

Update: The Windows Azure PowerShell CmdLets have added the necessary bootstrap information in order to set up PHP on Windows Azure Cloud Services. Download the Windows Azure PowerShell CmdLets.

Considering this blog post is about an open source language (PHP), I’m intentionally avoiding my trusty development tool Visual Studio. Even without using Visual Studio it will be necessary to download the Windows Azure Tools & SDK 1.3, this will provide us with some necessary command-line tools. That’s right, Windows Azure is Console Ready!

Context is Everything

Over the next three blog posts, I am going to be describing how to Install PHP on Windows Azure.

With the release of the 1.3 release of the Windows Azure SDK, Microsoft has enabled Full IIS (Internet Information Services) support in Windows Azure [this provides support for IIS Modules] and Start-up Scripts [which allow you to run command-line or powershell scripts on your deployment while the role is starting].

We will be leveraging start-up scripts to execute the [new] WebPI Command-line tool to install and configure PHP in IIS within a Windows Azure Web Role.

There has been an update to the WebPI Tools, download the WebPI v4 Command-line tool

We need a few things to Help us along the Way

  1. Web Platform Installer [WebPI] Command-line Tool [Any CPU]
  2. Windows Azure Tools & SDK 1.3
  3. Your Favourite Text Editor [I like Notepad++]

Creating your Start-up Scripts

Before we can even write our first start-up script there is one thing we need to get out of the way first and that’s where we create them. To understand what we’re doing lets do a quick break down on how deployments work in Windows Azure.

Breaking down a Windows Azure Deployment

Windows Azure requires only two files when deploying an application to the Cloud.

First, is a Cloud Service Package file which is essentially a Zip file which contains a number of encrypted files. Amongst these encrypted files are:

  • A Cloud Service Definition file which defines the fixed resources for our Windows Azure Compute Instance. The Service Definition is responsible for setting up Web Roles, Worker Roles, Virtual Machine Roles and Network Traffic Rules. These settings are then relayed to the Fabric Controller which selects the appropriate resources for your Compute Instance from the Fabric and begins provisioning your Deployment.
  • Your Application, which can consist of many Roles. Considering we’re using the Platform as a Service Model that Windows Azure offers, there are two main types of Roles: Web Roles and Worker Roles. A Web Role is like a typical Web or Application Server which runs IIS and Serves up Web Content. A Worker Role is a continuously running process which basically mimics a Windows Service.

Second, is the Cloud Service Configuration file which builds on top of what the Service Definition file provides to the Windows Azure Fabric Controller, only these values can be changed without the need to redeploy the Service Package. The Service Configuration is where you control the number of Instances your application is distributed across, as well as Windows Azure Storage Service Connection Strings and other values which may need to get changed over an Applications Lifespan.

That’s Great, but why did you tell me this?

Windows Azure is using the Convention over Configuration approach when it comes to start-up script location. You will be configuring where your application is actually located on your physical machine, but the Azure Tools are going to be looking for your scripts in ‘AppRoot/bin’. The AppRoot is determined by the name of your Role within the Service Definition file.

For now, lets stick with a very simple directory structure and we’ll talk about the Service Definition in the next post. In a directory that you’ve created for your deployment create a ‘Deployment’ directory and within that create a bin folder. We will be adding our start-up scripts to the bin folder.

Folder Structure for a Custom Windows Azure Deployment

Show me Teh Codez Already!

Fire up Notepad++ we’re going to create our start-up scripts to enable PHP within IIS in Windows Azure.

[Update: The Windows Update Script is no longer required by WebPICmdLine]

The first script that we need to create will enable Windows Update on the Windows Azure Web Role Image. The WebPI tool uses the Windows Update Process to install the Items that have been downloaded. Create a file, ‘enable-windows-update.cmd’ and paste the following script.

Script for enabling Windows Update

All Windows Azure Instances run on 64bit Processors, so you can possibly get rid of the Conditional logic.

Our next script is going to leverage the WebPI Commandline tool which you would have downloaded from the resource list above. This download is also required as part of the Cloud Service Package that we will be creating in a future post. Within the Deployment directory, create a folder called ‘Assets’ and another folder within ‘Assets’ called ‘WebPICmdLine’. Copy the WebPI binaries into the newly created WebPICmdLine folder.

Note: The WebPI tool is very powerful tool and can do much more than just install PHP. You may want to read the documentation found on the IIS Blogs and on MSDN.

Create a new file, ‘install-php.cmd’ and paste the following script.

Script for installing PHP with WebPI

PHP 5.2
PHP 5.3

The last line of that Script is a call to another script that needs to be run after PHP is actually installed. This isn’t a necessary step, however the ‘install-php-azure’ script [provided below] will place the ‘php_azure.dll’ in the php/ext folder and add the extension within the php.ini file. Adding the dll from the Open Source Project Windows Azure SDK for PHP available on CodePlex gives you the ability to leverage Blobs, Tables, Queues and other Azure API features.

You will need to add the php_azure.dll file that you download from CodePlex to the Assets directory [for consistency create a directory called ‘Windows Azure SDK for PHP’]. Create a file, ‘install-php-azure.cmd’ and paste the following code.

Script for Installing Windows Azure SDK for PHP

After you have completed creating these three files your folder structure should look like this:

image image

 

Download all Files

 

Until Next Time…

We have completed creating the scripts required to install PHP on Windows Azure. In my next blog post in this series I will explain how to create the Cloud Service Definition and Cloud Service Configuration files. We’ll start to get a better understanding as to how our Deployment fits together in the Windows Azure Platform. In the Third Part of this series we will package the Deployment using the command-line tools for Windows Azure and finally deploy our application to the cloud.

A Walk Through of a Windows Azure Data Center

Note: This is by no means anything new, but I’ve been referring a lot of people to it lately so I thought that I should share it with my readership on my blog. The Following video was recorded by Scott Hanselman at PDC09.

Have you ever wondered what a Windows Azure Data Container looked like? Perhaps you’ve been curious how Microsoft keeps their services cool in these vast shipping containers we all know an love as the Windows Azure Cloud.

Patrick Yantz a Cloud Architect from Data Center Services at Microsoft gives follow Program Manager Scott Hanselman a tour of a Proof of Concept (PoC) Data Center.

A Tour of a Cloud

The video is about 16 minutes long and provides a lot of information about the Technologies Microsoft Leveraged to create their Data Centers.

Building a Cloud

Microsoft has released another video that shows how the Data Containers are built. Just incase you’re curious, here is the video that shows the production of a Windows Azure G4 Data Container.

Get Microsoft Silverlight

The video is about 3 minutes in length.

Happy Clouding!