Automating Phalcon development environments with Vagrant (Vagrant with Phalcon)

February 11th 2014

It's no surprise that I like performance in applications, and I've been itching to use Phalcon for some time.

I solely use Vagrant for development environments now. It's a great tool, that gives you precise control over your applications deployment. If used correctly, you can also use your provision script to configure your live server.

For those of you who don't already know, Phalcon is a PHP MVC Framework with a difference. It's compiled from C into an extension and is loaded as an extension, versus loads of overly complex classes.

I recently bought a new Dell XPS 13 Developer Edition laptop (which, by the way is absolutely fantastic to code on). Anyway, my new laptop came with Vagrant pre-installed. It's rolling Vagrant version 1.0.1.

I knew from the second my laptop arrived that I'd be targeting version 1.2.6 of Phalcon. It's the latest stable, and believe me when I say it's VERY fast!

Setting Phalcon up inside of vagrant seemed like it'd be extremely easy, and no surprise it was. Now, I could use ansible, and make things difficult for myself, but I like the good old fashioned provision script (I'm not sure if it's just me, but the setup of the box seems to be faster).

Here's my Vagrantfile

Text Snippet:
# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant::Config.run do |config|
    config.vm.box = "base"
    config.vm.box_url = "http://files.vagrantup.com/precise64.box"
    config.vm.forward_port 80, 8080
    config.vm.provision :shell, :path => "provision.sh"
end

And the code inside my provision.sh file is:

Text Snippet:
#!/usr/bin/env bash

echo "Beginning provisioning"
sudo apt-get update
sudo apt-get install -y vim curl python-software-properties
sudo apt-get install -y git-core gcc autoconf make php5-dev zip unzip
sudo apt-get install -y nginx php5-fpm
sudo apt-get install -y build-essential php-pear php5-dev make

sudo rm /etc/nginx/sites-enabled/default
sudo cp /vagrant/nginx.conf /etc/nginx/sites-enabled/vagrant-site

echo "Configuring nginx"
sudo rm -rf /var/www
sudo ln -s /vagrant /var/www

wget --no-check-certificate https://github.com/phalcon/cphalcon/archive/v1.2.6.zip
unzip v1.2.6.zip
cd cphalcon-1.2.6
cd ./build
sudo ./install
cat << EOF | sudo tee -a /etc/php5/fpm/conf.d/phalcon.ini
extension=phalcon.so
EOF

sudo service nginx restart
sudo service php5-fpm restart

If you're wondering, my nginx.conf file looks like this:

Text Snippet:
server {
    listen   80;
    index index.php index.html index.htm;
    set $root_path '/var/www/public';
    root $root_path;
    try_files $uri $uri/ @rewrite;
    location @rewrite {
        rewrite ^/(.*)$ /index.php?_url=/$1;
    }
    location ~ \.php {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index /index.php;
        include /etc/nginx/fastcgi_params;
        fastcgi_split_path_info       ^(.+\.php)(/.+)$;
        fastcgi_param PATH_INFO       $fastcgi_path_info;
        fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
    location ~* ^/(css|img|js|flv|swf|download)/(.+)$ {
        root $root_path;
    }
    location ~ /\.ht {
        deny all;
    }
    client_max_body_size 100M;
}

I choose to install a few packages that you might not need, but essentially, this is exactly what you need. Just roll with it and give it a whirl.

As long as you've got these files in the root of your project, and you've edited the paths to your public directory, this will work for you.

So there you have it. An automated script to install Phalcon inside Vagrant machines

You can read more about Phalcon here: http://phalconphp.com/en/

And of course Vagrant here: http://www.vagrantup.com/

Hope it helps!