Using Custom PHP Extensions in Windows Azure Web Sites
I’m happy to announce that with the most recent update to Windows Azure Web Sites, you can now easily enable custom PHP extensions. If you have read any of my previous posts on how to configure PHP in Windows Azure Web Sites (Windows Azure Web Sites: A PHP Perspective and Configuring PHP in Windows Azure Web Sites with .user.ini Files), you probably noticed one glaring shortcoming: you were limited to only the PHP extensions that were available by default. Now, the list of default extensions wasn’t bad (you can see the default extensions by creating a site and calling phpinfo() ), but not being able to add custom extensions was a real limitation for many PHP applications. The Windows Azure Web Sites team recognized this and has been working hard to add the flexibility you need to extend PHP functionality through extensions.
If you have used Windows Azure Web Sites before to host PHP applications, here’s the short story for how to add custom extensions (I’m assuming you have created a site already):
1. Add a new folder to your application locally and add your custom PHP extension binaries to it. I suggest adding a folder called bin so that visitors of your site can’t browse its contents. So, for example, I created a bin folder in my application’s root directory and added the php_mongo.dll file to it.
3. Navigate to your site’s dashboard in the Windows Azure Portal, and click on CONFIGURE.
4. Find the app settings section, and create a new key/value pair. Set the key to PHP_EXTENSIONS, and set the value to the location (relative to your application root) of your PHP extensions. In my case, it looks like this:
5. Click on the checkmark (shown above) and click SAVE at the bottom of the page.
That’s it. Any extensions you enabled should now be ready to use. If you want to add multiple extensions, simply set the value of PHP_EXTENSIONS to a comma delimited list of extension paths (e.g. bin/php_mongo.dll,bin/php_foo.dll).
Note: I’m using the MongoDB extension as an example here even though MongoDB is not available in Windows Azure Web Sites. However, you can run MongoDB in a Windows Azure Virtual Machine and connect to it from Windows Azure Web Sites. To learn more about how to do this, see this tutorial: Install MongoDB on a virtual machine running CentOS Linux in Windows Azure.
If you are new to using Windows Azure Web Sites to host PHP applications, you can learn how to create a site by following this tutorial: Create a PHP-MySQL Windows Azure web site and deploy using Git. Or, for more brief instructions (that do not include setting up a database), follow the How to: Create a Website Using the Management Portal section in this tutorial: How to Create and Deploy a Website. After you have created a website, follow the steps above to enable custom PHP extensions.
Enabling custom extensions isn’t the only nice thing for PHP developers in this update to Windows Azure Web Sites. You can now publish from GitHub and you can publish to a Windows Azure Web Site from a branch other than master. For more information on these updates, see the following videos:
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)