Composer Support for Azure App Service Web App via Site Extension

In a previous post I outlined how to enable deployment time dependency management with Wep App by hooking the deployment with Kudu. In this post, I will explain how I iterated on the techniques used  in that previous post, to enable Composer in Web App via a Site Extension which lowers the barrier to use Composer with Web App.

Cool, but what exactly is a Site Extension?

Site Extensions are a means of extending the Azure App Service platform. The best part about Site Extensions are that they can be created by anyone, it is as simple as creating a Website, WebJob or IIS Module, an applicationHost.xdt file (applicationHost.config Transform), and possibly some supporting files to help install or setup the Site Extension components. These components are then packaged with NuGet and uploaded to the Site Extension Gallery

More information is available on how to create a Site Extension is available in the Kudu Wiki.

Now, How do I use Composer in Web App?

To get started with Composer on Web App, you’ll need to install the Site Extension, luckily I wrote a post which explains how to enable a site extension. You’ll find Composer in the list of Site Extensions.

Creating a web site which uses Composer

Composer has a number of different ways that it can be leveraged, you can run it as a tool from the command line directly to have it build files or you can manually create a composer.json file. I’ll refer you to the documentation to figure out how you’d like to use it.

For my application, I crafted the composer.json file by hand using Packagist as a reference for my file dependencies. I wanted to create an application which lists files from a particular container in Azure Storage, this means I would require the Microsoft Azure SDK for PHP and it’s dependencies. Here is my composer.json file.

{
  "require": {
      "pear-pear.php.net/HTTP_Request2": "*",
      "pear-pear.php.net/Mail_Mime": "*",
      "pear-pear.php.net/Mail_mimeDecode": "*",
      "microsoft/windowsazure": "*"
  },          
  "repositories": [
      {
          "type": "pear",
          "url": "http://pear.php.net"
      }
  ],
  "minimum-stability": "dev"
}

The composer.json file should be placed in the root of the repository. The Composer Site Extension has the COMPOSER_VENDOR_DIR set to d:\home\site\vendor which is out of the public wwwroot directory so that the dependencies cannot be addressable publicly.

Now that we have our dependencies taken care of simply build a PHP application which requires the autoloader which is created for us by composer, then write our code. Here is my index.php file which iterates over items in my storage container.

<?php

require_once "../vendor/autoload.php";

use WindowsAzure\Common\ServicesBuilder;
use WindowsAzure\Common\ServiceException;

$blobRestProxy = ServicesBuilder::getInstance()->createBlobService(getEnv('CUSTOMCONNSTR_BlobConnectionString'));

try {
    // List blobs.
    $blob_list = $blobRestProxy->listBlobs(getenv('container'));
    $blobs = $blob_list->getBlobs();

	echo "<ul>";
    foreach($blobs as $blob)
    {
        echo "<li><a href=".$blob->getUrl().">".$blob->getName()."</a></li>";
    }
	echo "</ul>";
	
} catch(ServiceException $e){
    $code = $e->getCode();
    $error_message = $e->getMessage();
    echo $code.": ".$error_message."<br />";
}

Deploy! and Configure

Now we’re ready to deploy the application and configure a few of the settings to make the application come to life. Click the button below to deploy the application into your Microsoft Azure Subscription:

This Deploy to Azure button could be configured to set these App Settings automatically on deployment, If you see a azuredeploy.json file in the repository you can disregard the following steps. As I have found the time to make the Azure Resource Manager template to hydrate these values.

Add the following App Settings to the App Settings list in the Portal:

  • container – This is the name of the container in your storage account that you’d like to list the values of. This is referenced in the code above by the call to getenv(‘container’) which reads this value from an environment variable
  • DEPLOYMENT_SOURCE – Typically, I would add a .deployment file to the respository to change the project config setting to web as this will change the deployment directory and only deploy the items in the web folder. Due to how the Composer site extension is hooking the deployment, the .deployment file doesn’t work so this setting is needed to let Kudu know that the contents of web should be copied to the wwwroot directory

Add the following Connection String to the Connection Strings list in the Portal:

  • BlobConnectionString – This should contain the connection string for your azure storage account. Set the type to Custom.

There is an assumption here that you actually have a few items in the storage account. If you don’t upload a few now then refresh the website.

That’s it! You have successfully deployed a PHP application which uses composer!

Comments are closed.