PHP - - By Bruno Skvorc

Up and Running with the Fastest PHP Framework on PHP7 in 5 Mins

You may remember our past infatuation with Phalcon, the fastest PHP framework.

In this post, we’ll go through the process of getting it up and running in 5 minutes on one of our Homestead Improved instances. If you’re not interested in why or what Phalcon is, just skip ahead to “Setting it up”.

Phalcon PHP logo

Recap

If you’re not familiar with it, Phalcon is a PHP framework written in Zephir, a language mid-way between C and PHP that helps in developing PHP extensions without having to know C. It’s a golden middle ground which lets PHP developers develop highly optimized modules for their programming language environment without the overhead of learning a completely new language, because Zephir compiles into C, which is what PHP extensions are made of.

Originally, Phalcon was an extension written in pure C – which made it incredibly fast. However, this also introduced massive overhead in fixing bugs or inspecting what’s going on under the hood when something doesn’t work the way it should. It also took ages to add new features, since developing in C is so much more difficult. In version 2, Phalcon was rewritten in Zephir, but as PHP 7 was looming on the horizon and announced a dramatic departure from the extension API of PHP 5+, Zephir could not compile code to PHP 7 compatible extensions and was left behind in PHP 5 land.

We try to walk the bleeding edge here, and there was no more room for Phalcon in our arsenal. With our Homestead Improved box up and running and powering all our tutorials, and with PHP 7’s performance advances making up for any kind of speed gains Phalcon may have offered in PHP 5 time, the cost of installing Phalcon was no longer worth it in any way.

Recently, things have changed: Phalcon went 3.0 LTS.

Apart from following Semver from now on, supporting the 3.0 version for the next 3 years, and various new features and changes you can read about in the post, the biggest update by far is the fact that Zephir now supports PHP 7, and allows for not only the compilation of Phalcon, but also the compilation of any other Zephir code into PHP 7 as well. You can now easily port your custom extensions to Zephir, and have them compiled into PHP 7 in a jiffy. A compiled framework combined with PHP 7’s performance gains offers unparalleled advantages in terms of speed and resource conservation.

Setting it Up

We’ll now go through the process of setting up Phalcon 3 on a PHP 7 powered Homestead Improved instance. You can use any kind of environment you feel comfortable in, but the instructions below will be specific to an Ubuntu 16.04 instance with PHP 7+ installed, and things like Git and Wget present. We’ll also use the Homestead Improved way of defining new sites, via the Homestead.yaml file.

Installing Phalcon

sudo apt-get install software-properties-common
sudo apt-add-repository ppa:phalcon/stable
sudo apt-get update
sudo apt-get install php7.0-phalcon
sudo phpenmod -v 7.0 -s ALL phalcon
sudo service php7.0-fpm restart

The second to last command is a shortcut command to enable a PHP extension. The -v flag tells it which PHP version to activate it for (we want only 7.0 here) and which SAPI (command line, FPM, or both – we want both).

If this tool is not available on your system, then just copy the ini file into the extensions folder like so:

sudo cp /etc/php/7.0/mods-available/phalcon.ini /etc/php/7.0/fpm/conf.d/20-phalcon.ini
sudo cp /etc/php/7.0/mods-available/phalcon.ini /etc/php/7.0/cli/conf.d/20-phalcon.ini

The 20 prefix indicates priority, in case some extensions need to be loaded before others. This is not the case here.

We’ve now unleased the beast. Looking at phpinfo(), it’s there.

Phalcon is active in phpinfo

Demo Phalcon App

We can test things on a demo Phalcon application – the finished Invo app.

Configuring Nginx for Phalcon

We need to add our app into the Sites block of Homestead.yaml:

    - map: phalcon-tut.app
      to: /home/vagrant/Code/phalcon-tut/public

Make sure phalcon-tut.app or what ever vhost name you choose is added to your host OS /etc/hosts file in order to resolve to the VM’s IP address.

Of course, we also need to run vagrant provision outside the VM in order to apply this Nginx setup.

Finally, we edit Nginx’s location block, and change from

location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

to

location / {
        try_files $uri $uri/ /index.php?_url=$uri&$args;
    }

We need to restart Nginx to apply the changes:

sudo service nginx restart

This takes care of the server end of things.

Bootstrapping the app

cd /home/vagrant/Code
git clone https://github.com/phalcon/invo phalcon-tut

Once the app has been cloned, we need to initialize its database, as per instructions. We’ll update those to be a bit more modern, by using utf8mb4 instead of utf8.

echo 'CREATE DATABASE invo CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci' | mysql -u homestead -psecret
cat schemas/invo.sql | mysql -u homestead -psecret invo

There’s one more step which isn’t described in the app’s repo – changing the baseUri in the configuration. We have to go into app/config/config.ini and change baseUri to /, otherwise assets (JS/CSS) will fail to load.

After doing that, sure enough, INVO is up and running.

Invo screenshot

Conclusion

In this tutorial, we quickly configured Phalcon to run on Homestead Improved and PHP 7, and got a demo app up and running. With installation instructions significantly simplified compared to what they were, and with ready-made apps to test it out thoroughly, why not go for a Phalcon test run and tell us what you think?

Now that Phalcon is available on PHP 7, will you be using it for any of your new projects? How do you feel about Zephir – would you use it to close-source or hyper-optimize any of your microservices / modules, or do you still feel like that’s too much overhead?

Sponsors