Phalcon on Windows Azure

Bruno Skvorc
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 introGoogle App Engine and a plea for PhalconZephir – 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!

Frequently Asked Questions (FAQs) about Phalcon on Windows Azure

How do I install Phalcon on Windows Azure?

Installing Phalcon on Windows Azure involves a few steps. First, you need to create a new Windows Azure website. Once the website is created, download the Phalcon DLL file that matches your PHP version and architecture. Then, upload the Phalcon DLL file to the ‘bin’ directory of your Windows Azure website. Finally, add an extension to the ‘web.config’ file of your website to enable Phalcon. Remember to restart your website for the changes to take effect.

What are the benefits of using Phalcon on Windows Azure?

Phalcon on Windows Azure offers several benefits. Phalcon is a high-performance PHP framework that is ideal for creating robust applications. When combined with the scalability and flexibility of Windows Azure, it allows developers to build and deploy applications quickly and efficiently. Moreover, Windows Azure provides a secure and reliable cloud computing platform, ensuring your applications are always available and performant.

Can I use Phalcon with other cloud platforms?

Yes, Phalcon is a versatile PHP framework that can be used with various cloud platforms, including AWS, Google Cloud, and others. However, the installation process may vary depending on the platform. Always refer to the official documentation for specific instructions.

How do I troubleshoot issues with Phalcon on Windows Azure?

Troubleshooting issues with Phalcon on Windows Azure can involve several steps. First, check the error logs for any specific error messages. These logs can provide valuable insights into what might be causing the issue. If the problem persists, try reinstalling Phalcon or updating it to the latest version. You can also reach out to the Phalcon community for help and advice.

What are the prerequisites for installing Phalcon on Windows Azure?

Before installing Phalcon on Windows Azure, you need to have a Windows Azure account and a website created on it. You also need to have the Phalcon DLL file that matches your PHP version and architecture. Additionally, you should have a basic understanding of how to work with Windows Azure and PHP.

How do I update Phalcon on Windows Azure?

Updating Phalcon on Windows Azure involves downloading the latest Phalcon DLL file and replacing the old one in the ‘bin’ directory of your website. Then, restart your website for the changes to take effect.

Can I use Phalcon with Windows Azure’s database services?

Yes, Phalcon can be used with Windows Azure’s database services. Phalcon supports various databases, including MySQL, PostgreSQL, and SQLite. You can connect to these databases using Phalcon’s built-in database adapter.

How do I optimize Phalcon’s performance on Windows Azure?

Optimizing Phalcon’s performance on Windows Azure can involve several strategies. These include enabling caching, optimizing database queries, and using Phalcon’s built-in tools for performance optimization. Additionally, you can leverage Windows Azure’s scalability features to ensure your application can handle increased traffic.

How do I secure my Phalcon application on Windows Azure?

Securing your Phalcon application on Windows Azure involves several best practices. These include using secure connections, implementing proper error handling, and regularly updating Phalcon to the latest version. Additionally, Windows Azure provides several security features, such as firewall rules and threat detection, to help protect your application.

Can I migrate my existing Phalcon application to Windows Azure?

Yes, you can migrate your existing Phalcon application to Windows Azure. The process involves moving your application’s files and database to Windows Azure. You also need to install Phalcon on your Windows Azure website. Always test your application thoroughly after migration to ensure everything works as expected.