Nginx, PHP5.5 and Phalcon on OpenShift

It seems like cloud deployment environments are all the rage these years – from AWS to Google App Engine, Windows Azure to DigitalOcean, and more popping up every month. With all these environments, it's important to be able to deploy the software you need.

In this tutorial, adapted from the step by step trial and error procedure of duythien, and with his full blessing, we'll cover the installation procedure of Phaclon on yet another environment: OpenShift. The procedure is bloody – OpenShift does not make it easy for us to deploy custom environments – but the rewards are well worth it.

Note: The repo used in this example was originally cloned from Boekkooi. He keeps his Openshift starter repo up-to-date, so if you need to get a custom PHP+Nginx Openshift environment up and running fast, use his original, then just plug the Phalcon procedure into the mix.

Starting out

Make an account on OpenShift. They offer a free tier we'll be taking advantage of.

As is usually the case with cloud environments, OpenShift has their own tools too. Follow the RHC tools installation procedure. I chose "spphalcon" as the namespace during the final part of the rhc setup step.

Under https://openshift.redhat.com/app/console/applications select "Create your first application now" and choose "Do It Yourself" – the option should be at the bottom of the screen.

Give the app a name – at this point you'll notice the namespace you previously defined in the rhc setup is reflected in the public URL of your app.

Note: a "server" on OpenShift is called a cartridge.

Creating and cloning an application

After you click "Create Application" and wait a few minutes, the next screen will show you instructions on how to download and modify the code for your app.

Clone the code using these instructions now.

A helper repo

When the cloning is done, enter the folder with cd phalcon and clone another repository inside the first one:

git clone https://github.com/duythien/openshift-diy-nginx-php

Enter this newly created folder, and copy the .openshift folder to the parent folder – the folder which contains your main application code you previously cloned. You might be asked to overwrite some files – accept all overwrites.

cd openshift-diy-nginx-php
cp -R .openshift ../

We also need to refresh permissions, as per instructions on the original helper repo:

git update-index --chmod=+x -- $(git ls-files .openshift/action_hooks/*)

You can remove the openshift-diy-nginx-php directory, we no longer need it. We only needed its .openshift subfolder.

Now add the changes, commit, and push:

git add -A
git commit -am 'Added .openshift folder'
git push

What gets initiated after this step is called a build process, and your DIY app will be stopped in order to read the contents of the .openshift folder and react accordingly. In our case, it's about to construct an nginx server environment. This build process can take a while, with some people reporting over an hour long wait. This is because all the software we're installing is being built from source rather than by package managers.

There have also been reports of OpenShift disconnecting during the build due to a timeout. If this happens, simply make an arbitrary change to a README file or something equally insignificant, commit, and push again.

When the build is complete, you can make sure everything works by creating a web folder in your app's root (locally), and an index.php file inside it with the contents:

<?php
phpinfo();

Commit, push, and you should be able to open your app's URL in the browser and see the PHP info screen.

Phalcon

Now comes the hard part, some hardcore hackery follows to get Phalcon up and running.

SSH into your cartridge by following the steps on your application's dashboard, or by finding out the SSH URL with rhc domain show.

Then enter app-root/runtime/repo and clone the cphalcon repository in that folder.

cd app-root/runtime/repo
git clone http://github.com/phalcon/cphalcon.git

Find the location of the PHP bin directory. It's usually in $OPENSHIFT_RUNTIME_DIR/php5/bin. Try cd-ing into it to see if it works. If everything is fine, enter the build directory of the previously cloned cphalcon.

cd && cd app-root/runtime/repo/cphalcon/build

Open the install file with a text editor like vi or vim, and replace all references to phpize with $OPENSHIFT_RUNTIME_DIR/php5/bin/phpize or whichever location you found for your own app, if not identical to mine. The phpize references should be at the bottom of the file. Also, add the option --with-php-config=$OPENSHIFT_RUNTIME_DIR/php5/bin/php-config to the configure line. Basically, you should be replacing this block:

#Clean current compilation
if [ -f Makefile ]; then
        make clean
        phpize --clean
fi

#Perform the compilation
phpize && ./configure --enable-phalcon && make && make install && echo -e "\nThanks for compiling Phalcon!\nBuild succeed: Please restart your web server to complete the installation"

with this

#Clean current compilation
if [ -f Makefile ]; then
        make clean
        $OPENSHIFT_RUNTIME_DIR/php5/bin/phpize --clean
fi

#Perform the compilation
$OPENSHIFT_RUNTIME_DIR/php5/bin/phpize && ./configure --enable-phalcon --with-php-config=$OPENSHIFT_RUNTIME_DIR/php5/bin/php-config && make && make install && echo -e "\nThanks for compiling Phalcon!\nBuild succeed: Please restart your web server to complete the installation"

What we're doing here is making sure the phpize from the freshly installed php5.5 is being run by explicitly stating we want that one. This way, cphalcon gets built for our version of PHP, with our PHP configuration.

After that, just run ./install. Sudo is not permitted on OpenShift cartridges, but the user you SSH with is about as close to root as you can get.

Let's add it to the php.ini file now.

cd $OPENSHIFT_RUNTIME_DIR
vim etc/php5/php.ini

Find the block where extensions are listed. Use vim's pattern finder: press forward slash (/) and type "extension=" and Enter, this will take you to the block in the screenshot below. Add "extension=phalcon.so" to the end of the block, and while you're there, scroll a bit further down and add your timezone as well.

We need to restart our cartridge now to reload these changes. You do this by punching in ctl_app restart while SSHed to the box.

Edit: you might encounter a problem after this saying something like

"PHP Warning: PHP Startup: Unable to load dynamic library '/path/to/extensions/phalcon.so' - /path/to/extensions/phalcon.so: undefined symbol: output_globals in Unknown on line 0".

.

To fix it, recompile Phalcon following the instructions provided here, or in other words, do the following:


cd cphalcon/build/64bits
make clean
phpize --clean
/path-to-php/bin/phpize
./configure --with-php-config=/path-to-php/bin/php-config
make && make install

Thanks to Alexey Bakulin for this tip!

Re-check your app's URL, and you should see Phalcon in the PHP info screen now.

Configuring Nginx

The last step is configuring Nginx for URL rewrites, so we can deploy a Phalcon app. We'll be deploying the Phalconphp.com site, but feel free to modify these settings to apply to your own app.

vim $OPENSHIFT_RUNTIME_DIR/nginx/conf/nginx.conf

Find the server block with the pattern finder (the pattern to search for is "server {"). Under root, change the //web part to be //website/public. Under index, put index.php as the first option, instead of the last. Scroll down a bit, and as per Nginx installation instructions under location @rewrites replace

location @rewrites {
    rewrite ^(.*)$ /index.php/$1 last;
}

with

location @rewrites {
    rewrite ^/(.*)$ /index.php?_url=/$1;
}

Clone the Phalconphp.com website into the repo folder, and when it's done, restart the cartridge again:

cd && cd app-root/runtime/repo
git clone http://github.com/phalcon/website
ctl_app restart

Visit the app's public URL and voila, you have a running Phalconphp.com clone on OpenShift.

Conclusion

Installing Phalcon (or any other PHP extension) on OpenShift is obviously neither easy nor fun – but seeing as their DIY apps are only version 0.1, the process is bound to become much easier. For now, I hope this introduction to custom OpenShift environments whetted your appetite enough to play around on your own. Open the .openshift folder and dissect the action hooks – there's lots to learn, and if you manage to automate the process we just went through with a hook, do let me know and I'll update the article with full credit.

Leave your feedback in the comments below!

Free book: Jump Start HTML5 Basics

Grab a free copy of one our latest ebooks! Packed with hints and tips on HTML5's most powerful new features.

  • http://jaf.ar.com/ Jafar

    Nice Tut… I love openShift

    • http://www.bitfalls.com/ Bruno Skvorc

      Thanks!

  • http://www.bitfalls.com/ Bruno Skvorc

    Could not reproduce this with PHP 5.5, thanks for the heads up though!

    • Alexey Bakulin

      This issue reproduces with php 5.5.0 and 5.5.7 for me.
      But anyway, thanks for article:) It is great!

      • http://www.bitfalls.com/ Bruno Skvorc

        Cheers! :) I’ll make sure I include your fix in the article as a solution to a potential problem, thanks again!

  • http://greenhousenovels.com/ ubuntu

    The article is very good.

  • http://www.bitfalls.com/ Bruno Skvorc

    Thank you for sharing this info with us!