PHP Revolution: HHVM and WordPress 3.9

By , Updated: August 29, 2016

hhvm and wordpress

What is HHVM?

There’s this small(ish) site that a couple of people visit every single day and spend hours there, you might have heard about it: it’s called Facebook. It is written in PHP and about four years ago when it really took off, the computational power that was needed to serve all requests was just too much because of the inefficiencies of  PHP execution engines’ that were available at that time. So they came up with the idea that instead of keep buying more server boxes, maybe they could solve the problem on a software level and created a PHP to C++ compiler named HPHPc. The original PHP code was compiled into executable binaries (sometimes file sizes reaching as much as a gigabyte!) and it was run instead of being compiled to opcodes and interpreted.

This gave about six times the performance which, to this day, is still a huge deal and one of the reasons we’re eagerly watching the development of the technology – and have been waiting for the mainstream adoption of it. Six times faster may not sound much when talking about general computer stuff, but if you look at it the following way, it might help put it in perspective for you: instead of having to buy 600 server machines to power a web service you only need 100. That’s a lot of savings just because you changed the way you run the code!

However, as you can imagine, running and maintaining separate developer and debugger versions of HipHop (called HPHPi and HPHPd, respectively), plus distributing gigabytes of executables to each machine after just a couple of changes in the code (think bug fixes), would quickly become tiresome and challenging.

This is why in 2013 Facebook decided to deprecate HPHPc but recycle the code and the experiences they gained from having it in production for at least three years and launched the HipHop Virtual Machine (HHVM). This engine converts PHP to bytecode which in turn is further converted into 64bit machine code at runtime by a just-in-time (JIT) compiler. The results? For some time now HHVM has outperformed the original HPHPc, in fact, Facebook’s production version is completely run on HHVM since Q1 2013. While benchmarks vary quite a bit (as always, they depend on a lot of factors), the results speak for themselves: 2 to 20 times performance increase when using HHVM compared to PHP-FPM.

HHVM and WordPress

As with all new software that’s set out to change the world, HHVM had quite a few problems running basically anything else other than the Facebook codes, which isn’t really a surprise, after all, it was written to do exactly that and just that!

If you tried to run WordPress with the help of HHVM either as a web server on its own (now a deprecated feature) or as a FastCGI replacement, you could have run into various problems, like having to manually edit wp-db.php as HHVM doesn’t support case-insensitive constants.

In the past year the HHVM open source project has really started to grow up and a result of countless of hours of work by its and WordPress’s developers, starting with version 3.9 of WordPress*, it will be running all WP codes without any hiccups (fingers crossed). This is the time to make the change to HHVM!

* If you didn’t follow 3.9’s development: Improve HHVM compatibility by eliminating some of our last remaining create_function() calls and making OBJECT a case sensitive constant. [27373] [27374] [27465] #14424 [27377] #27231

I’m pretty sure DevOps is an antidepressant, so why should I care?

You might not be managing your own server, but rest assured your hosting company is running some kind of a PHP processor. If it’s not HHVM or if the company is not planning to change it anytime soon, then your site’s performance will suffer. You surely don’t want that, as a one second additional page load time results in 7% decrease in conversion rates. If your site could be loaded in 1.5 seconds and you could maximize profits by not doing anything (well, except using a great hosting company cough Kinsta! :) ) you could be missing out on 7 to 21% of revenue. Can you afford to do that?

Okay you got my attention, where do I start?

By signing up for a Kinsta account and letting us know that you’d like to be HHVM’d! :) Right now I don’t know about any competitors who are offering HHVM. If you are a hosting company and have HHVM, please let us know in the comments and I’ll include you in this article! Yes, we do link to competitors. :)

If you have your own server, however, you’ll have to install it from a pre-built package or compile it yourself and set it up.

Installing or Compiling HHVM on Ubuntu

Ubuntu 14.04

wget -O - http://dl.hhvm.com/conf/hhvm.gpg.key | sudo apt-key add -
echo deb http://dl.hhvm.com/ubuntu trusty main | sudo tee /etc/apt/sources.list.d/hhvm.list
sudo apt-get update
sudo apt-get install hhvm

If you are getting segfaults

sudo apt-get install hhvm-dbg

Living on the edge (rebuilt everyday, unstable)

sudo apt-get install hhvm-nightly

Segfaults in the nightly

sudo apt-get install hhvm-nightly-dbg

Compile on Ubuntu 14.04

You must be running a 64-bit OS to compile & install HHVM.

sudo apt-get install autoconf automake binutils-dev build-essential cmake g++ git \
  libboost-dev libboost-filesystem-dev libboost-program-options-dev libboost-regex-dev \
  libboost-system-dev libboost-thread-dev libbz2-dev libc-client-dev libldap2-dev \
  libc-client2007e-dev libcap-dev libcurl4-openssl-dev libdwarf-dev libelf-dev \
  libexpat-dev libgd2-xpm-dev libgoogle-glog-dev libgoogle-perftools-dev libicu-dev \
  libjemalloc-dev libmcrypt-dev libmemcached-dev libmysqlclient-dev libncurses-dev \
  libonig-dev libpcre3-dev libreadline-dev libtbb-dev libtool libxml2-dev zlib1g-dev \
  libevent-dev libmagickwand-dev libinotifytools0-dev libiconv-hook-dev libedit-dev \
  libiberty-dev libxslt1-dev ocaml-native-compilers \
  php5-imagick

Download the latest source code:

git clone git://github.com/facebook/hhvm.git
cd hhvm
git submodule update --init --recursive
cd ..

Compile and install:

cd hhvm
cmake .
make
sudo make install

Installation Links for Other Systems

Debian Installation
Mint Installation
Fedora Installation
Other Linux Installations
Mac OS X 10.9 Mavericks (Mac OS X installation is currently EXPERIMENTAL and UNSUPPORTED.)
HHVM for Windows is not available at this time, however the team is working hard to release a compatible version later in 2014.

Using HHVM with Nginx

Add the following options to your Nginx virtual hosts file:

location ~ \.(hh|php)$ {
    fastcgi_keep_conn on;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include        fastcgi_params;
}

For a more complete guide on how to install HHVM, Nginx and Laravel (yes, that can be exchanged for WordPress, just disregard the Laravel parts) read this great tutorial on Fideloper!

Update 21, Apr 2014:

A couple of tests with HHVM :)

(to be updated with more!)

What do you guys think, is HHVM ready for everyday usage with WordPress? Will it revolutionize PHP? Please share in the comment sections below!