Facebook’s HipHop Engine, When to Use it, and Getting it to Work with CodeIgniter

As some of you may know, I dabble in machine learning (ML) and artificial neural net (ANN) coding to solve problems; often relating to baseball. (The Hardball Times: Getting out of the injury zone, part one) What follows is a brief confession of sorts: I still write a lot of my models and stuff in PHP. While I’ve written a bunch of scripts in Python to do anything from archiving MLB.tv games, to downloading and custom-parsing PITCHf/x data, and other data analysis / data mining tasks, PHP continues to be my go-to language. I also lean on CodeIgniter pretty heavily as a lightweight framework to handle MVC/MVA-type requests.

Why PHP and CodeIgniter

My first web-specific programming language was PHP. Prior to that, I had learned BASIC in grade school, Delphi/Pascal/VB in high school, and C/C++ in college. So a C-style language came naturally to me, and there was very little in the way of server configuration and setup, which I’m notoriously horrible at. Furthermore, I don’t consider myself a software developer, so I don’t really care what the pitchfork-wielding masses on Hacker News and Slashdot think about the language. It works.

As for CodeIgniter, I got started because when I first started off writing PHP for a contract job, I had trouble figuring out how to use CakePHP, and I switched to CodeIgniter as a random guess. Years later and a few patches submitted, and I’m still on the CI bandwagon and loving it. It works well, stays out of my way, and despite a few weird bugs, it gets the job done.

Isn’t PHP slow?

Yes. Dreadfully, actually. However, so is Ruby, and so is Python. So whatever.

And the slowness of any interpreted language has almost nothing to do with why your application has horrible latency. It is way more likely to be your coding style that’s the problem, followed close behind by the database layer and poor server configuration. Except when it isn’t – which is where I found myself a few months ago after writing large ANN/ML apps and big simulators in PHP, where the database layer wasn’t the issue. (My code could certainly stand to be a lot better, but that’s a blog article for another day. Or never, actually.)

When you’re calling millions of iterations of complex code, it might not matter that it takes a few hours to run overnight, but when you’d like it to run a bit faster in production, you start looking for answers. That was the case with two of my latest apps, so I started peeking around Facebook’s HipHop.

What is HipHop?

This is the often-out-of-date wiki on HipHop over at GitHub. It used to convert PHP into C++, which is awesome for people who cheat at software development (like me).

Here’s a quick summary of how it currently works:

  • HipHop Virtual Machine (hhvm) takes cold PHP code and turns it into bytecode/p-code and runs it in a VM
  • Which isn’t that exciting since PHP already does that
  • But hhvm ships with a JIT which turns that bytecode into machine code and executes that
  • man that’s awesome

hhvm can be called to run the optimizer (hhvm-analyze, well, kinda) ahead of time to eliminate the first cold cache reads. hhvm caches code very similar to how APC does. (Here’s a good time to show PHP vs. PHP+APC vs. PHP+HPHPc results – remember HPHPc has been deprecated and hhvm is much faster.)

Basically it makes PHP code run way faster.

Installing it and getting started

I used Ubuntu 12.04 (Precise Pangolin). As such, it’s super easy to install using APT – just follow these instructions.

After that, test your chops by downloading, installing, and configuring WordPress with hhvm.

But remember, I need it to work with CodeIgniter.

Getting it to work with CodeIgniter

The problem is that hhvm uses a weird web server configuration setup that is kinda like apache, but not really. So getting rewriting to work like it does in apache is not exactly straight-forward.

Fortunately, I stumbled upon someone’s nginx-as-proxy methodology that was nearly complete. Here’s what you need to do to get it working:

Install nginx

Fortunately this is pretty simple on Ubuntu 12.04. Just run this command:

sudo apt-get install nginx

Unlike apache, however, nginx doesn’t automatically start when the server boots. So be sure to start/stop/restart it manually.

sudo service nginx start|stop|restart

Configure nginx

You need to set up VirtualHosts to get your server to properly route requests. As such, you should copy the basic vhost file:

sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/mysitenamehere.com

Then edit the file (suck it vi lovers):

sudo nano /etc/nginx/sites-available/mysitenamehere.com

In the file, your server config should look something like this:

Here’s the kicker: Only the directory CI_DIR_HERE is going to work properly with requests. It should be pointed at the root CI directory where your index.php file resides.

This sets up nginx to route PHP files to port 9000, which is where hhvm is going to take over.

Configure hhvm

Now we need to configure hhvm to work in tandem with nginx.

sudo nano /etc/hhvm.hdf

Here’s what my config looks like:

This allows hhvm to listen on port 9000 for web requests and 8080 for administration purposes (cURL localhost:8080 to see options). Again, it points at /var/www/PATH_TO_CI as the CI root (what can I say, I like the apache-style two-level directory).

Configure CodeIgniter

This one’s easy, fortunately. Just open /application/config/config.php and change these lines:

$config[‘index_page’] = ”;
$config[‘uri_protocol’] = ‘HTTP_MY_SCRIPT’;

Set up a firewall/iptables/UFW

I hate server config, I suck at it, and I am not one to give advice on it. So read this DigitalOcean guide about it.

My setup in a nutshell:

  • i5-2500k based desktop with a lot of RAM and some SSDs
  • VMWare Workstation running my Ubuntu 12.04 distro with 4 GB RAM and 2 cores assigned to it
  • Bridged networking on the VM
  • dd-wrt compatible router; I DMZ’d the IP address of the VM
  • Set up UFW, fail2ban, iptables in what I’m sure is not the best configuration
  • Using no-ip as my dynDNS provider (free if you don’t mind a stupid domain)

It seems to work fine for me. Hopefully that helps everyone out as well.

Lastly, what hhvm/hiphop is good for

It’s awesome if you are stubborn like me and refuse to write in compiled languages for CPU-intensive work. It’s also really nice if you want to marry that laziness/stubbornness with the ability to use PHP’s good frameworks and rapid application development methods. This is an area where PHP is way, way ahead of Ruby and Python. It’s not disputable, really, though there are plenty of language evangelists who will pimp out Rails (not easy to configure for non-web devs, sorry) or Django (don’t get me started).

It might be good for a WordPress install that gets a ton of traffic. I don’t really know, my sites get a few thousand hits per day and I haven’t converted them (though I played around with using Redis as an object cache for WP, which was kinda neat though I ultimately abandoned it and just stuck with my APC-based code cache methods).

It’s definitely not necessary for CRUD-style apps where the bottleneck is not the CPU. To specifically state: Unless you are blocked at the CPU level, hiphop/hhvm isn’t for you. And even if you are, learning to parallelize your code should be the first thing you do (if it can be done; often you will have methods that require backwards knowledge). Contrary to popular belief, multithreading can be done in PHP using pcntl_fork() or even specialized classes for it.

But heck, it’s kinda fun to use. So don’t let me tell you what you should or shouldn’t do with it. It’s come a long way since it was HyperPHP/HPHPc, and it’s been fun keeping up with it. Give it a run.

One thought on “Facebook’s HipHop Engine, When to Use it, and Getting it to Work with CodeIgniter

Leave a Reply

Your email address will not be published. Required fields are marked *