2014-01-04
Intrigued by the HHVM team's claim that Laravel's test suite passed 100% when run on HHVM, I decided to get a basic Laravel application running on HHVM.
First a history, lifted fromĀ The Great Book of the Internet:
In the beginning PHP was not a programming language, but a set of CGI Perl scripts used to maintain Rasmus' personal homepage. One thing lead to another and eventually the core PHP interpreter was rewritten and called theĀ Zend Engine. If, like me, you've always wondered what "Zend" means, now you know.
A bunch of websites are now written in PHP. One you might have heard of is Facebook. And Facebook, being one of the biggest websites in the world, had a problem: how can we take all this PHP code that we have, and make it as fast as possible?
The programmer in you should be jumping up and down saying "I know! I know! Compile it to C(++)!" That is exactly what Facebook did with a project they calledĀ HipHop for PHP, or HPHP for short. Using the HPHP compiler Facebook could compile all their PHP code in to a binary, ship it off to their servers, and massively increase their performance. In fact, Facebook realized up toĀ 6x the speed of Zend PHP. Awesome!
Unfortunately the "ship it off to their servers" part wasn't too awesome. The compiled binary exceeded 1GB. Even worse, maintaining the HPHP compiler as well as the needed HPHPi (development environment) and HPHPd (debugging environment) was a significant undertaking. Perhaps the worst problem was that HPHPc, being a compiler for an interpreted language, could not support some of the features of PHP such as eval() and create_function().
To get around this Facebook deprecated HPHPc and created a PHP virtual machine calledĀ HHVM. HHVM converts PHP code into machine readable bytecode, similarly to the JVM. This bytecode is then translated into x64 machine code at runtime using a just-in-time (JIT) compiler. Now Facebook doesn't have a lengthy build step, and can ship only changed files to their servers on deploy instead of a 1GB binary, while retaining the performance advantage of HPHPc.
Sweet! Let's run Laravel on it.
I first installed HHVM on my OS X 10.9 Macbook Air, but it didn't seem to workĀ quiteĀ right. When I tried to access the server over HTTP it would peg the CPU and just sit there.
Then I installed HHVM on my Ubuntu 13.10 x64 server, using
theĀ instructions on the HHVM
wikiĀ which
worked beautifully. After that I ended up with the hhvm binary located
here:
~/dev/hhvm/hphp/hhvm/hhvm
A clunky directory hierarchy, but whatever. Feel free to symlink it in
to /usr/local/bin if you want to make it easier to acccess.
Now comes the Laravel specific sauce. Taylor Otwell postedĀ the config that he has used on TwitterĀ so I used that. Unfortunately, laravel pastebin is down at time of writing so I have mirrored it as a gist.
wget that down into your home directory: (apologies for the terrible formatting, not sure what is wrong here)
~$ wget https://gist.github.com/jazzdan/8262454/raw/5d190c4de5252399cb421f344a789aeb56bb4ebd/config.hdf
Now let us get Laravel installed.
Install composer if you haven't already:
~$ curl -sS https://getcomposer.org/installer | php
~$ sudo mv composer.phpar /usr/local/bin/composer
~$ sudo apt-get install php5-mcrypt
Check to make sure mcrypt is there now.
php -m | grep mcrypt
Install laravel:
~$ composer create-project laravel/laravel hhvm-test --prefer-dist
:$ cd hhvm-test
~$ composer install
Once that has completed you should be able to run Laravel on HHVM with this command:
~$ cd ~/dev/hhvm/hphp/hhvm
:$ sudo ./hhvm -m server -c~/config.hdf
mapping self...
mapping self took 0'00" (214686 us) walltime
loading static content...
searching all files under source root.source..
analyzing 5 files under source root...
..loaded 25 bytes of txt firstles
loaded 25 bytes of static content in total
loading static content took 0'00" (15735 us) wall time
page server started
all servers started
If you see output that looks like that, then you should be good good to go!
I have stepped through all of theĀ Laravel QuickstartĀ on HHVM and everything appears to work, including MySQL database access. I'll report back as I progress in to actually developing a Laravel app on it.
Many eyes make for shallow bugs, or so I'm told. Please try this out on your more developed Laravel projects and report relevant bugs to the Laravel and HHVM teams. I envision a future in which HHVM enables everyone to have more performant, more easily deployed PHP applications.