Updating PHP Settings in Windows Azure

I came across this question on Twitter last week: “How can I turn display_errors on for an application that is running in Windows Azure?” I have to admit that I was stumped. The only thing I could think of was to re-deploy the application with an updated php.ini file. But, I happened to mention this question to Ben Lobaugh who suggested a very simple idea: Store your PHP settings in some durable store external to your application as key-value pairs, then loop through the settings and update them with the ini_set function when a page loads. An example will help make this clear, but first, a couple of caveats:

Caveat 1: This is a hack. This is only a hack. This is nothing but a hack. (But it works.) My intent is simply to throw this idea out there and hope that others will improve on it.

Caveat 2: This idea might be useful if you are learning Windows Azure and you want to be able to change settings on a nearly-ready-for-production application. I’ll assume that your heavy-duty testing has been done in the Compute Emulator prior to deployment. I wouldn’t necessarily suggest that the idea presented here be used on a production application.

Ben suggested that I use Windows Azure Table storage for storing my key-value pairs (which I’ll do in my example), but you could really use any store that is external to your application…

Suppose you have deployed an application to Windows Azure with display_errors off, but (for one reason or another) now you want display_errors on. Without putting any forethought into being able to change settings, you are stuck (unless you are willing to re-deploy the application with a new php.ini file, which isn’t really an attractive option). But, if you have put some forethought into changing settings, you might have added a file like this to your application:

<?php
require 'Microsoft\WindowsAzure\Storage\Table.php';

class Setting extends Microsoft_WindowsAzure_Storage_TableEntity
{
    /**
     * @azure value
     */
    public $value; 
}

function updateSettings()
{
    $storageAccount = 'Your storage account name';
    $accountKey = 'Your account key';
    $client = new Microsoft_WindowsAzure_Storage_Table('table.core.windows.net',
                                                       $storageAccount,
                                                       $accountKey);
    $query = "PartitionKey eq 'settings'";
    $settings = $client->retrieveEntities("phpinitable", $query, "Setting");
    foreach($settings as $setting)
    {
        ini_set($setting->getRowKey(), $setting->value);
    }
}
?>

 

A few things to note about the code above:

  • It requires the Windows Azure SDK for PHP (It’s free and easy to install. More info here: Accessing Windows Azure Table Storage from PHP.)
  • It reflects my Azure Table structure. The Setting class allows my settings to be materialized as Setting objects, and it reflects that my Table has a property called value. The partition key for each Table entry is settings and the row key for entry is the name of the PHP setting. So, each Table entry looks like this:

Partition Key: settings
Row Key: [name of PHP setting]
value: [value of setting]

Now, if I call the above file updateSettings.php, then I simply need to add this to the top of pages in my application:

include 'updateSettings.php';
updateSettings();

To turn on the display_errors setting, I simple need to create a Table entry that looks like this:

Partition Key: settings
Row Key: display_errors
value: 1

An easy way to create Azure Tables and entries is to use the Windows Azure Storage Explorer for Eclipse (more on that here: Using the Windows Azure Storage Explorer for Eclipse). And, I can create as many Table entries as I have settings to update.

Of course, as I mentioned earlier, you don’t have to use Azure Table storage…you could use any store that is external to your application and that you can update easily.

I’d be interested in hearing variations on this idea. If you’ve got other ideas, let me know!

Thanks.

-Brian

Share this on Twitter

Comments

  • Anonymous
    April 11, 2011
    The comment has been removed
  • Anonymous
    April 11, 2011
    One of the questions I asked (several people) when writing this was similar to your question: I wanted to know if PHP could start using a php.ini file that was located in a Windows Azure blob? (I think that is a more narrow version of what you are suggesting.) However, I think this would mean needing to restart PHP to incorporate any new settings...Still lots to think about/work to do when running PHP in Azure.-Brian
  • Anonymous
    July 01, 2014
    It's totally just hacking dear , and may creates problem in next module or update arises so not work for me..By the way nice trick ..<a href="growthwell.com/.../">web design services in Mumbai</a>