Installing CakePHP in the Microsoft Azure Preview Portal

A while back, last year actually, I wrote a blog post on how you can Install CakePHP on Windows Azure Web Sites using the App Gallery. At Build 2014 we introduced a new Preview Portal which enables much more to an application owner including in-place billing information, application analytics and a whole new way to visualize your cloud experience.

In this thread, I’ll show you how to create a new CakePHP application via the Preview Portal.

If you’re an experienced CakePHP Developer, you might want to check out Sunitha Muthukrishna blog post on using CakePHP’s Bake Console Tool on Microsoft Azure Websites.

Install CakePHP on Azure Web Sites

From the Start Board, select the Azure Gallery.

image

This will open the App Gallery Blade, where you can select from a list of categories. Select Web, then CakePHP.

image

This will start the CakePHP installation, select Create. Thus begins your Journey.

image

You’ll need to create a new resource group. Enter a name for your Resource Group, then click on the Website configuration

image

You’ll need to select your Hosting Plan. For this demo, I created a free site.

image

Then configure the application, by clicking on Web App Settings. Set the Security Salt and Cipher Seed.

image

Then select the datacenter location you’d like to deploy your application to.

image

Click OK to finish the Web Site Configuration and move on to create the Database.

image

Select Database.

image

Accept the Terms for the ClearDB database.

image

Select a different subscription, if required. Then click Create.

image

Your site has started to deploy and should be ready for you to start creating within seconds.

image

You can monitor your application, change settings, or set up source control from your new site.

image

Enjoy!

How to backup a Web Site running on Microsoft Azure Web Sites

Keeping regular backups is important for anything on the web, nay technology, especially for mission critical applications, enterprise applications, or keeping your meme generator application from Build 2014 [not sure what I’m talking about? Watch the Day 2 Keynote].

In this example, I’m actually going to outline how I keep a backup of my blog, yes the one you’re currently reading right now. It is running on WordPress and represents a good portion of my journey into a career in technology, that means it’s countless hours of my time that I continuously have the opportunity to read what I’ve done in the past after doing a quick Bing search on something I’m currently working on.

Take a Backup of a Web Site.

In the Microsoft Azure Management Portal select the Web Site you wish to backup.

image

As you can see in the image below, I run my site in a Shared Web Site. This provides me with enough resources for people navigating my blog to get an excellent experience without it being too heavy on my pocket book.

image

The backup feature of Web Sites only works in Standard, so for now, I’m going scale my site to Standard. This is as simple as clicking on the Standard Button, then clicking on Save in the command bar at the bottom of the screen.

image

Once I click on the Save button, I am prompted to let me know that scaling to standard will increase my costs, but I’m not too worried as I’ll be scaling back down to shared again shortly.

image

After the scaling task finishes, I’ll be able to use the form in the Backups navigation to select my storage account I wish to have my backups save to, the frequency in which they are saved as well as a database which is linked to my Web Site as a Linked Resource in the previous tab.

image

So I’ll select my favourite storage account.

image

And include my ClearDB database which is linked to my site to be backed up as well.

image

Then I’m only one click away from knowing all my archived hard work is saved for me in my storage account.

image

After the backup is done, pay attention because this is important, I go back into the Scale tab and scale my site back down from Standard to Shared. This moves me back down into the lower billing range that I am comfortable running my site in.

What does Microsoft Azure Web Sites Backup?

In the image below you can see two files which identify a backup. The first which is an xml file describing the site that was backed up at a high level including the custom domain, web site service domain as well as the name of the database which was backed up. The second file is a zip file which contains a backup of your site which I will outline in more detail below.

image

Here is a quick snapshot of the contents of the zip file: a fs folder, a temp folder, a meta file and a file named the same as your database.

image

What is in the Azure Web Site Backup zip folder

FS – if you haven’t already guessed it, FS stands for File System. This retains a snapshot of the file system of your web site at the time the backup was taken. This includes both the site and logFiles folders so you have access to anything you would need.

Temp – My temp folder was unused.

Meta – This is an xml file which describes all aspects of your website including but no limited to Custom Domains, Configured SSL Certificates, App Settings, Connection Strings, Default Document settings, Handler Mappings (for custom FastCGI Handlers); Remote Debugging, Web Sockets. I could go on, but I believe you get the picture, if it’s something you set in the portal for your web site, it’s backed up in this file.

Database Name – In my case, I had a MySQL database selected, so this file is a MySQL dump file. This can be used to completely restore my database from schema to data.

Automating Environment Creation using Microsoft Azure PowerShell

During preparations for the Web Demo, presented by Mads Kristensen at Build 2014 [watch the day 2 keynote], it was necessary to stand up multiple environments to do development, testing and dry runs of the presentation. The quickest way to accomplish this was to script out the environment provisioning with PowerShell and the Microsoft Azure PowerShell and Cross Platform Command Line Tools.

Download the Tools

In order to automate your Microsoft Azure Environments, it’s helpful to download and install the following tools.

Microsoft Azure PowerShell Tools

Microsoft Azure Cross Platform Tools

Environment Automation Script

Considering the size of our environment, the number of stamps required and the need to be agile to changes in the environments it was necessary to build out an environment setup script for our Azure resources.

Our environment consists of:

  • 3 Azure Web Sites, 1 Staging Slot
  • 1 Azure Storage Account, 2 Storage Containers
  • 1 Traffic Manager Namespace, 2 Traffic Manager endpoints

Storage and Web Sites are easy to automate with PowerShell or the Cross Platform Command Line Tools. However, there isn’t currently a way to automate Traffic Manager without programming directly against the Microsoft Azure Service Management API.

To be able to support the creation of multiple assets, using Hash tables enables a unique value for the Web Site or Storage account, while still being able to store the region for creation. This fulfills the minimum requirements for creating an Azure Web Site, or an Azure Storage Account.

Leveraging a PowerShell cmdlet, the previous environment is deleted in the begin function, then the environment is rebuilt in the process function. Service creation is an asynchronous action, so we took advantage of Jobs in PowerShell to handle creation in background threads. If additional configuration is needed on a service, the Job is waited upon until completion, before additional service calls are preformed on the service.

Here is the script we called for each stamp of our environment:

Stamp Automation Script

The Environment Automation Script takes care of the heavy lifting, while the Stamp automation script is responsible for describing the resources required in the Environment.

First we must register the environment automation script so that it can be called within the same process that is executing our stamp automation script, then describe the stamp in variables then pass it to the environment setup script.

Conclusion

We generated custom scripts to suit our needs, but this isn’t something that you would need to do. Introduced in the VS 2013 Update 2 release and the Azure SDK 2.3 tools, you can have PowerShell scripts generated for your Web Application directly within Visual Studio File New Project dialogue (for more details read Using Windows PowerShell Scripts to Publish to Dev and Test Environments)

Building a Windows Azure Boot Camp (Training) Resource Kit

Before joining Microsoft, I used to run a local user group (monthly), a geeky social hour (weekly) and you’d often find me doing some (free-as-in-beer) public training sessions on Windows Azure. One of the hardest things about running an event is needing to rely on the internet connection of whatever space you could find to host your event. No matter how well you plan, unleashing hundreds of geeks on a WiFi connection at a venue is bound to cause some issues.

One important part of an organizers tool belt is providing an offline install option for those attendees who filled out their contact details with an auto-form tool and didn’t read your carefully crafted prerequisites list. Making one of these handy resources isn’t very straight forward, so I thought I would help out anyone looking to host an event.

If you’d like to host or attend an event, keep an eye out for the Global Windows Azure Boot Camp.

Recently, we’ve been making some changes to how we deliver the Windows Azure Training Kit, Web Camps Training Kit, Data Camps Training Kit & Enterprise Developer Training Kit. This is done through the Web Platform Installer which provides an excellent way to configure IIS and download tools & frameworks. This will allow us to provide dependencies for the training kits alongside the content making it easier to ensure your machine is configured to be able to use the content. This blog post will act as an interim step to providing alongside dependencies, but will also be useful after the point we include the dependencies as part of the training kit install.

Make a WebPI Offline Package

First you will need to create an offline backup of the contents required for your boot camp. This is easily done by using the WebPICmd.exe tool which is included in the Web Platform Installer installation directory. The /offline switch will download the installer files and required metadata for the /Products to the specified /path.

image

Offline Install Flavours

I’ve created a few scripts that will help attendees install the tools that they need. Here is a list of the offline installation scripts which will help attendees install the required tools.

image

The total size of this package is approximately 2GB (a perfect use for those USB keys you collect from conferences).

Changing the Training Kit Install Directory

Warning If you plan on updating the Install Directory of the Windows Azure Training Kit, be sure to copy the USB key to your local drive before making any modifications.

I’ve heard a lot of feedback around customizing the install directory of the Training Kit. This can be achieved by updating the offline product list found at feeds\latest\WebProductsList.xml after running OfflineMaster.cmd. We use the %HomeDrive% environment variable to select an install location, simply replace %HomeDrive% with a specific location to customize the install directory.

Clearing the WebPI Caching

Sometimes it’s necessary to clear the WebPI cache, so I’ve made a simple PowerShell script to help clear the cache automatically.

Note: It’s only necessary to clear the WebPI cache if for some reason you are not getting refreshed content that you know is available.

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.

Deployment Time Dependency Management for PHP with Composer on Windows Azure Web Sites

Windows Azure Web Sites provides the ability to manage dependencies on deployment for .NET using NuGet, and Node.js using npm. This functionality is facilitated by an open source project called Kudu which is built and maintained by the Windows Azure Web Sites team [it can also be installed on Windows Server 2012].

If you’re a PHP Developer, you should know about Composer. Wouldn’t it be great if you could use Composer on Windows Azure Web Sites to fetch your dependencies during the deployment of your PHP Application? I thought so too!

To get started, I would recommend following this tutorial to create a php web site using the windows azure command-line tools for mac and linux. It’s ok, I’ll wait… Oh, back so soon? You should now have a Windows Azure Web Site with Git Deployment enabled.

Customizing a Windows Azure Web Sites Deployment for PHP

The Windows Azure Cross Platform Command Line tools expose an extended part of the Kudu functionality called KuduScript. KuduScript can be used to generate a set of files (.deployment, deploy.cmd) which hooks a Kudu deployment, enabling a custom script to be run at deployment time.

To generate a deployment hook with the Cross Platform Tools, run the following command:

azure site deploymentscript --php [-t bash]

This will generate a deployment hook for a PHP application, you could optionally pass in –t bash which would output the script in bash instead of the default batch.

Now that we have a customized deployment script, let’s add a few customizations; one to download composer to our Windows Azure Web Site, the second to run composer to fetch our dependencies specified in our Composer.json file. Add the following two lines above the Deployment section of the deploy.cmd.

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Download Composer
:: -----------------

echo Downloading Composer

IF NOT DEFINED COMPOSER_VENDOR_DIR (
	SET COMPOSER_VENDOR_DIR=%DEPLOYMENT_TARGET%\vendor

	echo Downloading Composer

	curl -sS https://getcomposer.org/installer | php

	IF !ERRORLEVEL! NEQ 0 goto error
)
php -d extension=php_intl.dll composer.phar install --prefer-dist --no-dev --optimize-autoloader

Debugging Deployment Issues

Now that you’ve added a process to your deployment that has an external dependency there will most likely come a time where you will need to diagnose issues with your custom deployment. Not to worry, Windows Azure Web Sites includes the output of the deployment script in the Windows Azure Management Portal. To view the deployment log:

  1. Click into the details view of your Windows Azure Web Site
  2. Click on the DEPLOYMENTS tab
  3. Click on the top item in the Deployment History section
  4. An arrow will appear to the right of the entry, Click the Arrow.
  5. Click on the View Log link

waws-deployment-hook-debug

Moving the Web Root in Windows Azure Web Sites

A common practice in PHP is to have the vendor directory which is created by running composer be a sibling of the web root directory. This provides a level of security as the vendor directory is not stored in a publicly accessible location avoiding public calls into third party code downloaded by composer.

This can be achieved in Windows Azure Web Sites by changing the Virtual Directory of the web root from within the Windows Azure Management Portal. Assuming my application serves it’s public content from a folder called web contained in the root of my source control, the following steps will move the web root to the web directory:

  1. Click into the Details of your Windows Azure Web Site
  2. Click on the CONFIGURE tab
  3. Scroll to the bottom of the CONFIGURE screen until you find virtual applications and directories
  4. Change the record for / from site\wwwroot\ to site\wwwroot\web
  5. Click the Save button in the Command Bar

waws-virtual-directory

Production Web Site Guidance

In a production environment setting, having an external dependency as part of your deployment process can impede the ability to deploy your application. It is up to you to understand the non-functional requirements of your application which includes the acceptable deployment time. It may be required to ensure that the latest version of your code be available in production in a moments notice. In cases such as these it is important to keep a backup of your dependencies which could either be in your production branch of source control, or maintained independently of your project. It may be necessary to stage your deployment in a staging environment before pushing to a production server. In my next post, I’ll cover how you can deploy to a Windows Azure Web Sites staging slot and swap that staging deployment into production.