Using Guzzle to Interact with the Windows Azure Management API

Guzzle is a very simple abstraction over cURL which provides a great HTTP client for working with web services. This provides a great way of interacting with the Windows Azure Management API with PHP. In this example, I’m going to show how you can enable and disable an SSL Certificate in a Windows Azure Web Site using the Windows Azure Management API.

The majority of Windows Azure Web Site tasks can be automated using either the Windows Azure PowerShell cmdlets or the Windows Azure Cross Platform Command Line tools. This includes the ability to upload an SSL Certificate for your Web Site, however at this point there is no ability to bind the SSL Certificate to the Web Site itself. That’s where Guzzle and the Windows Azure Management API come into the picture.

You can do the following exercise from the Windows Azure Management Portal by following the instructions in the article Enable HTTPS for a Windows Azure web site. This entry is to demonstrate how you can achieve SSL Certificate binding as part of an automated environment script.

Automating SSL Certificate Upload to Azure Web Sites

Before we start interacting with the Management API let’s get the Windows Azure Web Site ready by adding the SSL Certificate to the Environment. This task is incredibly simple to accomplish using the Windows Azure Cross Platform tools.

SSL certificates can be uploaded only in Standard mode. Learn more about configuring custom domains.

azure site cert add [ssl-cert-path].pfx --key [cert-password] [web-site-name]

That’s it! The cert will now be added to the Windows Azure Web Site.

Create and Upload Export a Windows Azure Management Certificate

You could very well create and upload a management certificate of your own by creating one with OpenSSL and uploading it via the Windows Azure Management Portal (Settings > Management Certificates). However, there is a much easier way to achieve this without having to generate your own certificates, by using the Windows Azure Cross Platform Tools.

azure account cert export [--subscription]

The optional subscription parameter can be either the name of the subscription name or id from the azure account list command output.

Create a PHP Application with Guzzle to interact with the Windows Azure Management API

This is where things get fun! Let’s start by creating a composer file to acquire Guzzle.

Next we’ll want to take a look at the documentation for how to Enable or Disable SSL in Windows Azure Web Sites with the Management API. This gives us the information for the rest endpoint, http verb and xml/json payload to enable or disable the SSL Certificate.

Warning! At the time of writing SSL Certificates are only available for upload/binding if the Web Site is in Standard Mode. If the site isn’t in Standard Mode your requests will return with the status code HTTP 400 Bad Request.

Enable SSL JSON Playload

Disable SSL JSON Playload

PHP Source Code

To enable or disable SSL in a Web Site you will need to make a PUT request against the management API pass in your SubscriptionId, the webspace of the site, the site name and the Client Certificate. In Guzzle the request is constructed by the client, which you can pass an array of values into for replacement when you create the request by calling the PUT method.

Conclusion

This is how simple it is to make calls to the Windows Azure Management API from PHP using Guzzle. This blog post covers the usage of JSON for the request payload, however, there is a full example available as a Gist if you’d like to see how this can be done with XML.

Installing CakePHP from the Windows Azure App Gallery

In my previous post Application Frameworks now part of the Windows Azure Web Sites Gallery I introduced the concept of ready to install application frameworks which can be provisioned as part of a new Windows Azure Web Site. The benefits include having a ready to go starting point for building out your application with a tested and fully capable package which includes common Application Settings as well as the proper configuration for IIS.

Today, I’m happy to announce a new addition to the App Frameworks section of the Windows Azure App Gallery, CakePHP. Let’s take a look at the steps to get CakePHP running in Windows Azure Web Sites using the new Gallery item.

Setup CakePHP on Windows Azure Web Sites

To follow along with the example below, sign up for a Windows Azure Free Trial.

In order to provision a Web Site from the Windows Azure App Gallery we must first sign into the Windows Azure Management Portal. Once you’ve logged into the portal you will see the command bar at the bottom of the browse, click New.

drawer-waws-new

This will open the drawer and enable you to select from a variety of different services which you can provision, for this example we’re interested in Web Sites. Select ComputeWeb Sites > Gallery.

drawer-waws-fromgallery

The gallery modal will pop up and allow you to select from a number of well-known open source CMS, Frameworks and other tools.  Select App Frameworks > CakePHP or scroll through the list to find CakePHP.

At the time of writing this article the CakePHP version in the Gallery was 2.3.6.

windowsazure-websites-gallery-appframework

Once CakePHP is selected, click the next arrow at the bottom of the screen to advance to the Configuration page in the wizard.

websites-configure-cakephp

In order to configure a secure deployment of CakePHP, the Cake Foundation suggests including a Security Salt and Cipher Seed. The Web Sites team has conveniently surfaced those settings to the Configuration page, which will get injected into the proper configuration setting in the app/Config/core.php file.

During the configuration, you can select an existing MySQL Database or create a new MySQL Database with your new site. If you needed to create a new MySQL Database, you will receive this screen which will ask for the database name, a region to deploy the database in [Note: It’s a good practice to deploy your database in the same region as your application to reduce latency], you will also need to accept the terms from our partner ClearDB who supplies the MySQL Databases in Windows Azure.

websites-mysql-cakephp-database

That’s it! After clicking the checkmark to complete the wizard your new Web Site will start to provision which includes a configured version of CakePHP which is a great starter point to your next CakePHP Application.

windowsazure-website-site-listing

Once the Status changes to Running, you’re ready to view your new site. You can do this as simply as clicking the BROWSE icon in the Command Bar.

drawer-waws-browse

Congratulations, you have yourself a brand new CakePHP Installation in Windows Azure Web Sites.

WindowsAzure-WebSites-CakePHP

CakePHP is setup, Now what do I do?

Now that the CakePHP Framework is in place it’s time to start building an application. To do so, you’re going to need the source on your local machine.

Download your Web Site files with Git

With Windows Azure Web Sites you can deploy using many source control repositories, this includes Git. However, Git isn’t enabled by default when you create a site from the gallery, so lets find out how to enable Git Deployment (and in our case, a remote repository to clone from).

If you click into the details of your new Web Site in the Windows Azure Portal, you will see a Quick Glance section on the DASHBOARD. Click on Set up deployment from source control to create a new repository in your Windows Azure Web Site.

 

websites-set-up-source-control

Like I said above, there are many options for source control systems, select Local Git Repository then the next arrow to continue.

websites-setup-source-control-git

It may take a few seconds for your repository to be created, so this notification will keep you amused in the meantime.

websites-create-git-repo

Once the repository is created, the portal should redirect to the DEPLOYMENTS tab. Copy the Git URL, you’ll need this to clone your repository on your local machine.

websites-copy-git-url

On your local machine, open Git Bash or your favourite Git command line utility, type the following pasting the git url you copied above in place of <git-url> below, then hit enter.

git clone <git-url>

local-clone-website-repo

You now have a local git repository of your CakePHP Application.

You may have noticed both the Vendor and Plugins directories are missing, this is because they are empty and Git does not track empty folders. Simply create the folder locally and place a file in the directories in order to upload third party code or plugins, respectively.

Download your Web Site files with FTP

To download your Web Site files with FTP you will need your favourite FTP tool, this could be a number of different tools including the command line. Here are the steps you will need to take to configure FTP downloads from your site, I’ll leave the actual downloading up to you.

On the DASHBOARD screen in the portal under the Quick Glance section, you will need to set (or reset, incase you forgot) your deployment credentials. Click on the link which allows you to set the deployment credentials.

websites-setup-deployment-credentials

Fill out the form providing your username, password and confirm password.

websites-set-deployment-credentials

Once you’ve set your credentials, scroll a down the Quick Glance section until you find your FTP or FTPS Host Name.

websites-ftp-links

Notice that your FTP User is a combination of the web site name and the user name provided above, the password is the password which was created when you set your deployment credentials.

Enjoy developing with CakePHP!

New Windows Azure SDK for PHP Feature: RunPHP (on Windows)

If you are developing PHP on Windows for use on Windows Azure, there is a fancy new feature which may catch your attention. First things first, let’s run through the basics of installing the Windows Azure SDK for PHP in order to set up your environment to get ready for PHP development for Windows Azure.

This new feature is part of the Windows Azure PowerShell and Windows Azure Emulators components which is downloadable through the Web Platform Installer.

Install Windows Azure PowerShell and Windows Azure Emulators for PHP

  1. Open Web Platform Installer via the Windows Azure PowerShell and Windows Azure Emulators deep link.
  2. Click Run.

    image

  3. Accept UAC prompt.

    image

  4. Click Install.

    clip_image001

  5. Click I Accept.

    clip_image001[6]

  6. Go to the kitchen and grab a cup of coffee.

    clip_image001[9]

  7. Click Finish.

    image

How To: Use RunPHP to test a PHP Application on Windows

In order to understand how to use runphp, let’s quickly build a sample PHP application to show off the functionality of runphp.

Now that we have a simple application set up, let’s open a command prompt to use runphp. I will use Windows PowerShell to execute commands, in order to use runphp, PowerShell will need to be run as Administrator.

To run PowerShell as Administrator, right click on the PowerShell icon and select Run as Administrator.

Navigate to the path which contains your application, then execute the command runphp.

image

There are a number of things going on when executing RunPHP, let’s enumerate the high level steps:

  1. Copies an apphost.config file to the current directory
  2. Runs appcmd.exe from IIS Express
    1. Creates a new Website based on the current directory
    2. Configures a HTTP Handler to map *.php files to php-cgi.exe interpreter
  3. Starts IIS Express and binds the website to an open port.

To view and test your website, open a browser visit the localhost address with the appropriate port which the website was bound to during the runphp process.

http://localhost:8080

Once you finish testing, you can shut down IIS Express by opening the PowerShell window and pressing the ‘Q’ key.

Now that you’ve finishing testing your application on your local machine, you can deploy to Windows Azure Web Sites.

Application Frameworks Now Part of the Windows Azure Web Sites Gallery

A recent addition to the Windows Azure Web Sites Gallery has me very excited, support for installing Web Application Frameworks. Installing a Web Application Framework from the Windows Azure Web Sites Gallery provides the benefit of speed and simplicity during setup by means of implicit configuration for Windows Azure Web Sites.

Let’s take a look at how we can set up Django, a Web Framework for Python applications, using the Windows Azure Management Portal.

Setting up Django on Windows Azure Web Sites

This article makes the assumption that you already have a Windows Azure Subscription. If this is not the case sign up for the 90 day free trial of Windows Azure.

  1. Log into the Windows Azure Management Portal.
  2. Click on the + New button
    drawer-waws-new
  3. Select Compute > Web Site > From Gallery
    drawer-waws-fromgallery
  4. Select App Frameworks
    gallery-appframeworks
  5. Click Next
    gallery-appframeworks-django
  6. Click Complete
    waws-django
  7. Click Browse
    drawer-waws-browse
  8. Start Building!

    Did you know there are Python Tools for Visual Studio?

    waws-django-works

Why is this useful?

Having these packages installable from the Windows Azure Web Site Gallery ensures that you’re receiving a great experience when using the Application Framework of your choice. Once the site is configured, simply download the files using FTP to begin development, or upload your application specific files from your local installation of the same framework.

Windows Azure Training Kit – December 2012

Good News! The world didn’t end, which is good for two reasons, first we’re all alive, but also my team has been working hard to provide you another great Windows Azure Training Kit release.

What’s new in this release?

Back in October, you may have heard of a massive on campus Microsoft event known as Build. Build was a great event filled with an absolutely everything you need to know about Windows 8, Windows Phone 8 and Windows Azure Development. If you haven’t already done so, check out the day 2 keynote which highlights all of the news around Windows Azure.

In this release we’ve packaged up the day two Keynote demos for you to be able to look at the code or deliver a similar session using the very same demos.

Event Buddy Demo

eventbuddy-session-list

In this demo you will start with a disconnected application that manages events and sessions to later connect it utilizing Windows Azure Mobile Services to provide structured storage for events and sessions. In order to use authentication within the application, you will add Twitter (or Facebook) to your application and services. Following this you will upload session decks to SkyDrive and finish by sending Live Tiles using push notifications every time an attendee rates a session.

BUILD Clips Demo

upload-video

In this demo, we will show how to build and deploy an ASP.NET web site that enables users to browse, play, and upload their own personal videos. We will then extend the web site to include Web APIs that power a Windows 8 experience. Finally, the web site project will be deployed to Windows Azure Web Sites and scaled using multiple paid shared instances.

Windows Azure Mobile Services – Scheduler

We have updated the Windows Azure Mobile Services Presentations, Demos and Hands-on Labs to include the new scheduler functionality. If you want some more information about the new scheduling functionality in Windows Azure Mobile Services, check out Scott Guthrie’s post.

Updated Repositories

You can download the offline training kit (Windows only) or grab the files from GitHub.

Provisioning a MySQL Database from the Windows Azure Store

The Windows Azure Store is available as part of the Windows Azure Management Portal, a convenient resource for all of your Windows Azure needs, which can be accessed from a variety of Devices including your favorite iDevice, Surface, Windows Phone, Mac or PC.

At the time of writing, the Windows Azure Store is currently only available in the US.

Click on the + New in the Windows Azure Management Portal Taskbar, then select STORE.

CustomCMS-Store-TaskbarDrawer

The Store opens in a modal dialogue, either scroll down or filter to APP SERVICES to find ClearDB MySQL Database, then click the [next] arrow.

CustomCMS-Store-AppService-ClearDB

Select an appropriate database size (or stay with the Free plan and upgrade later once the site is live) and select the Subscription to charge. Provide a name for the Database (can be left with the default, a name will be assigned for automatically), select the region to provision the database in (whenever possible, try to provision the Web Site and database in the same region to avoid latency). Click the [next] arrow.

CustomCMS-Store-ClearDB-Create

The following screen will provide an overview of the monthly charges of the new MySQL Database. Be sure to review the terms of use and privacy statement, then click the [purchase] checkmark.

CustomCMS-Store-ClearDB-Purchase

After the Add-on has been provisioned, click on the Connection Info button in the Taskbar.

CustomCMS-Store-ClearDB-ConnectionInfo

Copy the connection string for use in your application. Alternatively, a newly created database can be added as a Linked Resource to an existing Windows Azure Web Site, the credentials will be surfaced under the connection string section of the CONFIGURE tab.

Brian Swan has an interesting solution for parsing a connection string from the connection string settings found in the CONFIGURE section of a Windows Azure Web Site in his blog entry getting database connection information in Windows Azure Web Sites

CustomCMS-Store-ClearDB-ConnectionInfo-Details

Stay Cloudy my Friends…