Phalcon on Windows Azure

Bruno Skvorc
Share

In my last Windows Azure with PHP article, we went through the detailed process of deploying a PHP+MySQL app on Windows Azure; but every PHP dev knows it makes no sense not to use a framework. Reinventing the wheel is never a good option, not when you have so many excellent wheels to choose from.

In this article we’ll install Phalcon, my personal framework of choice. You might be thinking “But isn’t a framework just a collection of classes? Isn’t just uploading them enough?”. That’s not the case with Phalcon. Phalcon is a PHP extension written in C that needs to be installed into PHP much like any other extension (mcrypt, xdebug, etc) – this makes it unavailable on various shared hosting environments, but this complication also makes sure the people using it are mostly dedicated professionals.

To get introduced to Phalcon, feel free to look at some of our previous articles on the topic:
Phalcon intro
Google App Engine and a plea for Phalcon
Zephir – build PHP extensions without knowing C

Preparing

Use the guidance of the previous article to set up a new empty PHP web app. Alternatively, use the application from the last article, if you’re not too attached to it.

Seeing as Windows Azure is a Windows environment, the PHP extensions for it are .dll files. Download Phalcon.dll. Make sure you grab the 32bit version (x86), because Free Trial Azure accounts don’t support 64 bit environments. Also, grab the file that has the latest PHP version in the name, and which is NTS (non-thread-safe).

Regardless of the system you’re currently on, also make sure you have Phalcon installed in your local environment. We’ll need a place to test our app before deploying it, after all.

Clone the sample app, put it into your web root and see if it works. If everything goes fine, you should see the following screen in your browser:

If you get lost configuring it, the Phalcon docs have pretty good tutorials on setting the app up on every popular web server: http://docs.phalconphp.com/en/latest/reference/install.html#installation-notes

It doesn’t matter which OS or server you use at this time – I’m almost certain you aren’t using IIS – so all that matters is getting the app to work. If you get to the above screen, and the Sign Up Here link works and redirects you to the proper url without any “index.php” artifacts in the browser’s address bar, we’re good to go. If the app doesn’t work like it should, try following along with their tutorial: http://docs.phalconphp.com/en/latest/reference/tutorial.html#creating-a-project

Installing the Phalcon extension

IIS is the server which Azure uses to deploy PHP web apps. Seeing as the Phalcon docs don’t cover setting up the framework on that server, we’ll need to improvise slightly. For now, if you’ve set up an FTP account and your Azure app is up and running, we’re good to go.

First, connect to the app’s FTP server, and change the content of index.php to

<?php
phpinfo();

If you visit your app’s URL in the browser now, you should see the familiar PHP info output, listing installed extensions, environment details, and so on.

Now let’s install Phalcon. First create a bin directory in the root of your app. On Azure, that’s two folders above the one containing index.php – the one that contains the folders LogFiles and site. Upload the previously downloaded Phalcon.dll file into that bin folder, so that your directory structure now looks like this:

Next, we need to tell Azure where to look for the extension (and to look for it at all, for that matter). Go into the “Configure” section of your Azure app, and scroll down to the “App Settings” section. Under KEY, put “PHP_EXTENSIONS” and under VALUE, put ..\..\bin\php_phalcon.dll.Mind the backslashes in the path! Remember, we’re using Windows!

Finalize by clicking Save at the bottom of the screen, and reload your app’s URL. If you search for the string “Phalcon” on the displayed page, you should get an output not unlike the following:

Phalcon has been installed. But we’re far from done.

Uploading and testing

Next, let’s upload our code to Azure. We don’t want all of our app’s code in the wwwroot folder, because that folder is public on the web – everyone can access it, so there are some security implications to consider. Instead, upload the contents of the sample app’s public folder into wwwroot, and upload the sample app’s app folder into the site folder, one level above.

Refreshing the app’s URL should now give you this:

If this page loaded, this means Phalcon was installed. However, if you try accessing the “Sign Up Here” link, you’ll get the “The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.” error message. This happens because we haven’t defined URL rewriting. Seeing as we’re on IIS now, Apache’s .htaccess files won’t work. What we need is a Web.config file – the IIS equivalent.

In the root of your app’s public folder (public locally, wwwroot on Azure), create the file Web.config and give it the following content:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="Main Rule" stopProcessing="true">
                    <match url="(.*)" />
                    <conditions logicalGrouping="MatchAll">
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="index.php?_url=/{R:1}" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

The above can (and should) be tweaked on a per-app basis, but in essence, this is all it takes for a Web.config file to behave just like a .htaccess file and redirect all requests to index.php with the _url GET parameter, as per Phalcon’s instructions.

Try visiting the “Sign Up Here” link now. The page should open successfully, displaying a sign-up form.

Conclusion

Azure is an incredibly versatile environment, allowing for deployment of custom PHP runtimes, extensions, and tweaks of all types. Armed with the knowledge of installing the fastest PHP framework (which should significantly reduce the running costs of your Azure app!), why not go ahead and enter the Push The Web Forward contest with an awesome Phalcon app? There’s still time!

Leave your feedback in the comments below and, as always, please share this article if you liked it!

CSS Master, 3rd Edition