Skip to main content

Windows Azure for PHP developers


Since a couple of years the term "cloud" has moved from simple buzz-word to real business opportunities that make a difference within the market. The technology behind "cloud" is not new as we've grown accustomed to server clusters, distributed datacenters and separation of responsibilities, but the "cloud" offers everyone the opportunity to scale and not just the companies who have a huge cash reserve to purchase another rack with servers.

The strange thing is that cloud is being provided by companies you wouldn't expect to serve it. Best known are and that offers a complete set of services you can use to run applications in the cloud, they provide the infrastructure and you have to set up and maintain your platform and infrastructure. Microsoft also jumped on this cloud hype offering a platform in the cloud. And this is exactly the reason it caught my attention and got me interested. I'm a developer and I don't want to fiddle with setting up and maintaining an operating system, basically since I don't have the time for it.

If you are eager to learn more about cloud and what different kind of cloud solutions are out there, I can recommend the book "PHP Development In The Cloud" by Ivo Jansch and Vito Chin. In this book both authors look beyond the buzz of the cloud and discuss what each cloud service provider has to offer for PHP developers and how you can easily consume their services using PHP code. A must read for everyone that has an interest in developing web applications using PHP and cloud technology, as I already posted a book review for this book here on my blog.

Windows Azure

I got in touch with Microsoft's Windows Azure back in 2008 through our local Microsoft Evengalist Katrien De Graeve, who told me Windows Azure is like running a Windows 2008 Server with IIS in the cloud. Along with Maarten Balliauw Microsoft was working out more tools and features to support PHP (amongst other open source technologies) on their new cloud platform.

I was completely sold when Josh Holmes came to Brussels in 2009 and told us more about what Windows Azure has to offer and how perfectly it is to build applications consuming these cloud services, without having to deal with setting up and maintaining the platform the run on. And so, I started playing with it and discovering about what Windows Azure is all about, what kind of services they offer and how I as a PHP developer could make good use out of it. In 2010 PHPBenelux and Microsoft joind efforts to promote Windows Azure by organizing a PHPonAzure contest to develop an application that would run and make use of Windows Azure's services.

Developing for cloud

When developing PHP applications, you mostly develop for a single server setup or maybe one server running your PHP code and one server for the database. This is the easiest way to set up an environment as you don't need to worry about anything except to connect to the correct database.
When developing for the cloud, more things should be considered before you get started. Some things that you take for granted require some additional thoughts when designing the architecture of your application. Fortunately for you, there are only a few things to take into account before you can deploy your latest killer app in the cloud provided by Windows Azure.

Operating System

On a bare metal server or even a virtual server, you know you need to set up your own operating system (or have your hosting service provider or sysadmin do this for you). This means you need to take responsibilty in keeping your OS up-to-date with security fixes and all kinds of software upgrades.
Windows Azure is a platform on it's own, comparable to a fully set up Windows 2008 Server complete with IIS 7. Microsoft takes care of updating and securing their instances you use without compromizing your application.

Data Storage

When creating a normal website session data, logs, caches and file uploads are often stored on the local filesystem. With a cloud solution, you need to consider that multiple instances of your web application might be up and running and don't share each others filesytem. To facilitate filesystem storage, cloud services offer so called "file buckets" or storage endpoints. Windows Azure offers 5 types of storage:

  • Blob storage: simple binary storage for video, images, audio and other types of files
  • Table storage: a structured storage for large amounts of data, behaving like a NoSQL storage
  • Queue: a messaging queue to transfer messages between applications and services
  • Windows Azure Drive: a mounted shared disk partition formatted as NTFS VHD
  • SQL Azure: a true relational database based on Microsoft SQL Server technology

One storage location I need to mention a little separate is Windows Azure Cache, a memory storage. It's a caching layer comparable to Memcached or WinCache, residing completely in memory.

File uploads are easy as you only need to worry where they end up and how you make sure files cannot overwrite existing files, unless specified to do so. Using one of the 5 storage solutions provided by Windows Azure is not difficult using PHP, it only requires correct settings for transferring the uploaded file to the chosen storage. PHP's move_uploaded_file() method can be used in most cases. In other cases, use the Windows Azure SDK for PHP as it offers a rich abstraction layer for the different storage options of Windows Azure.

Sessions in PHP can easily be stored in a database, a storage endpoint or on a memory layer by modifying the session_set_save_handler(). Windows Azure can store session data on the blob storage, table storage, the Windows Azure Drive, in SQL Azure or on it's Cache layer. I prefer to use either the SQL Azure or the Cache, as they both perform really fast, even for session data.

When using logs within your application, you also need to be aware that your log files should reside somwehere central. Best is to either log against Table storage or SQL Azure as it's a fast way to keep track of changes and easily accessible. But, storing logs on the blob storage is perfectly good as well, only know it might grow your storage space too much.

Caching is something best handled by Windows Azure Cache, the memory storage I mentioned earlier. It's fast, very easy to maintain and super easy to scale in or out.

Data itself can be stored in SQL Azure or on Table Storage. SQL Azure is a full featured relational database, comparable to Microsoft SQL Server. It offers all the features you can expect from a relational database, including stored procedures, transactions and separation of responsibilities. Table Storage is a simple key/value storage table, that behaves more like a NoSQL storage and thus capable of storing documents and constructs in an organized way.

Other considerations

Cloud applications should be designed to scale horizontally. This means when your application requires more resources to cope with the usage, more instances of the application will run together behaving as a single application.

Cloud is also "pay as you go". If you use a little, you pay a little. If you use a lot, you pay also a lot but less then when you need to scale with traditional hardware. The following graphs by Josh Holmes explains it best.

Now it doesn't matter if you're a big company or just a start-up. You can launch your product slowly and grow as your usage and budget grows. A perfect match.


Now that I've introduced you to what Windows Azure has to offer PHP developers, or any other technology developer for that matter, I show you how I have a simple Zend Framwork application build on a traditional LAMP stack deployed to Windows Azure. So stay tuned for more.

Related links


Popular posts from this blog

PHP 7 and Apache on macOS Sierra

I posted several talks about compiling PHP from source, but everyone was trying to convince me that a package manager like Homebrew was a more convenient way to install. The purpose of Homebrew is simple: a package manager for macOS that will allow you to set up and install common packages easily and allows you to update frequently using simple commands. I used a clean installation of macOS Sierra to ensure all steps could be recorded and tested. In most cases you already have done work on your Mac, so chances are you can skip a few steps in this tutorial. APACHE AND PHP WITH HOMEBREW I’ve made this according to the installation instructions given on GetGrav. The installation procedures These installation procedures will set up your macOS Sierra with PHP 7.1 and Apache 2.4. Install Xcode command line tools (if not done yet)xcode-select --install Install Homebrew/usr/bin/ruby -e "$(curl -fsSL" Set up for in…

Sessions in PHP 7.1 and Redis

In case you have missed it, PHP 7.1.0 has been released recently. Now you can’t wait to upgrade your servers to the latest and greatest PHP version ever. But hold that thought a second… With PHP 7 lots of things have changed underneath the hood. But these changed features can also put unexpected challenges on your path. Our challenge One of these challenges that we faced was getting PHP 7.1 to play nice storing sessions in our Redis storage. In order to store sessions in Redis, we needed to install the Redis PHP extension that not only provides PHP functions for Redis, but also installs the PHP session handler for Redis. Because we upgraded our servers to PHP 7.1, we were looking to use the latest provided version for this Redis extension: redis-3.1.0. Once installed, we bumped against a nasty problem. Warning: session_start(): Failed to read session data: redis (path: tcp:// Searching the internet for this error, we didn’t got many hits that could point us into a dire…

Speeding up database calls with PDO and iterators

When you review lots of code, you often wonder why things were written the way they were. Especially when making expensive calls to a database, I still see things that could and should be improved.
No framework development When working with a framework, mostly these database calls are optimized for the developer and abstract the complex logic to improve and optimize the retrieval and usage of data. But then developers need to build something without a framework and end up using the basics of PHP in a sub-optimal way.

$pdo = new \PDO( $config['db']['dsn'], $config['db']['username'], $config['db']['password'] ); $sql = 'SELECT * FROM `gen_contact` ORDER BY `contact_modified` DESC'; $stmt = $pdo->prepare($sql); $stmt->execute(); $data = $stmt->fetchAll(\PDO::FETCH_OBJ); echo 'Getting the contacts that changed the last 3 months' . PHP_EOL; foreach ($data as $row) { $dt = new \DateTime('2015-04-…