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": {
      "": "*",
      "": "*",
      "": "*",
      "microsoft/windowsazure": "*"
  "repositories": [
          "type": "pear",
          "url": ""
  "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.


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!

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


	echo Downloading Composer

	curl -sS | 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


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


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.