Getting Started with HHVM and WordPress

We teamed up with SiteGround
To bring you up to 65% off web hosting, plus free access to the entire SitePoint Premium library (worth $99). Get SiteGround + SitePoint Premium Now

Only a few months ago, HHVM (HipHop Virtual Machine) was a popular buzzword in the PHP community. Many of us were curious about this technology, especially the reports of improved performance. Previously, Zend’s PHP was the only alternative — until Facebook introduced HHVM to the world.

HHVM and WordPress

Good news, HHVM is no longer a buzzword. Many people (including myself) dislike hyped up technologies, but I’m happy to write this as a post-buzzword article.

So let’s dive in!

In this article, I’ll cover:

  1. What is HHVM?
  2. What is Hack?
  3. What is the difference compared with traditional PHP?
  4. Why is it so important that it exists today (or maybe not)?
  5. What are some benchmarks to see how HHVM differs from PHP 5 and PHP 7?

What is HHVM?

To understand what HHVM is, we first need to cover what Zend Engine is (currently we’re at version 3 with PHP 7). The best explanation on this can be found on this Wikipedia article:

The Zend Engine is the open source scripting engine that interprets the PHP programming language. It was originally developed by Andi Gutmans and Zeev Suraski while they were students at the Technion – Israel Institute of Technology. They later founded a company called Zend Technologies in Ramat Gan, Israel. The name Zend is a combination of their forenames, Zeev and Andi.

Is it the language or an interpreter? It’s a ongoing debate with every scripting language. However, let’s say that PHP the language, is abstract, it’s a blueprint for the interpreter (parser). It’s more of a philosophical debate than a real debate. HHVM is like Zend Engine, but it takes a different approach to parse and run the source code.

In the end both interpreters/parsers can (in theory) execute the same source code and provide the same output with the same side effects. HHVM takes a different approach. The team behind HHVM describes it as a Virtual Machine that uses JIT (just in time) compilation to gain more speed and flexibility than Zend’s way.

One more reason to consider HHVM is speed. Until PHP 5.* benchmarks were on HHVM’s side. I know that benchmarks are also a debated topic, but when done right, they can show some truth. Long story short, HHVM appeared superior to PHP but not as much after PHP 7. The gap started to become more blurry when PHP 7 came out. So that’s why we’ll run some benchmarks today comparing them.

HHVM is still a new, cool, and different technology than Zend. It’s totally different from a technical perspective. Some things start as an experiment and may be part of a daily toolbox for some developers. Did I mention that it’s baked from Facebook and that they also use it in production (although not sure which parts of Facebook). HHVM was built from a real problem that Facebook had – speed (plus some other complicated problems that most developers might never run into).

HHVM also supports Hack. Hack is a programming language for HHVM. However, isn’t PHP the language that HHVM parses? Yes, but there’s also Hack. Basically Hack is PHP plus some other features that are not currently part of PHP or not planned in the near future. So when you write PHP, HHVM can parse and execute it, but you can also use Hack which is PHP with some additional stuff. It’s your choice. But don’t forget that Hack is not 100% compatible with PHP. The specific Hack features don’t work on Zend Engine, they only work on HHVM.

Setting up HHVM and WordPress on Docker

I didn’t want to make the whole article on just my setup, so that’s why I created WP_Dock (WordPress + Docker). If you want to know more on Docker and WordPress make sure to read these articles:

You can choose to install everything on your OS, but using Docker makes it easier for everyone to install on every OS. It also removes the need to install PHP, HHVM, nginx or MySQL on your desktop OS. That being said, you can follow HHVM’s own tutorial on how to install both HHVM and WordPress if you’d prefer.

First you have to download the Docker Toolbox if you haven’t already. Then download or clone this project.

git clone https://github.com/AleksanderKoko/WP_Dock wpdock

Download WordPress, copy it inside the folder and rename it to ‘app’. Navigate to inside the Docker folder, cd wpdock/docker.

By default, nginx runs php-fpm, so you’ll have to change this. On ‘docker-compose.yml’ change nginx ‘links‘ to ‘hhvm‘. It should look something like this:

nginx:
    build: ./nginx
    volumes:
        - ./../storage/logs/nginx:/var/log/nginx
        - ./../app:/var/www/app
    ports:
        - "80:80"
        - "443:443"
    links:
        - hhvm

Also in docker/nginx/config/upstream.conf change ‘php‘ to ‘hhvm‘. It should look like this:

upstream fastcgi-upstream { server hhvm:9000; }

Then execute docker-compose up -d nginx. This builds Docker containers using ‘docker-compose.yml’ as its configuration. Now you’ll know on which IP your containers can be found, make a note of that for the next step.

docker-machine ip default

Now open your browser to the IP noted above and you’ll see the WordPress installation guide. The final step is to change the WordPress database configuration. By default, the credentials are specified in docker-compose.yml. If you look at the MySQL section you’ll see:

mysql:
     build: ./mysql
     volumes:
         - ./../storage/data/mysql:/var/lib/mysql
         - ./../storage/logs/mysql:/var/log/mysql
     ports:
         - "3306:3306"
     environment:
         MYSQL_DATABASE: databasename
         MYSQL_USER: username
         MYSQL_PASSWORD: secret
         MYSQL_ROOT_PASSWORD: root

If you want to change the database name, username and password, change these values and then:

docker-compose down
docker-compose up -d --build nginx

Note: the MySQL host should be the IP of the machine. The one that you used on the browser to see the WordPress installation.

To switch back to PHP first run docker-compose down then change the configuration back to PHP. So remove ‘hhvm‘ as nginx link and add ‘PHP‘. Also update docker/nginx/configuration/upstream.conf to:

upstream fastcgi-upstream { server php:9000; }

Execute docker-compose up -d --build nginx to use PHP instead.

HHVM + WordPress Versus PHP 7 + WordPress

Now for the benchmark, these are the rules. I’ll use the base containers as they are. HHVM can also be used stand alone but I used it as fpm to make the comparison. I know that both can be configured to be more optimized but that is an article for another day. As HHVM needs a warm up to be at its full potential, I should make some requests first, for the HHVM to compile its stuff (we’ll do the same for PHP to make things more even). For the benchmark we’ll use Apache Bench (ab). Check their docs for installation or how to use it if you want to run benchmarks on your machine. Also, Bruno Skvorc has an excellent article on Apache Bench to help get you started.

My bench command is: ab -n 500 -c 100 http://192.168.99.100. In total there are 500 requests with 100 concurrent requests.

The PHP Results

PHP results

The HHVM Results

HHVM results

79 seconds vs 18 seconds sounds like a big promise, even if 79 seconds is PHP 7. But I have to admit that I haven’t optimized PHP or HHVM. Maybe HHVM is more speedy out of the box, so don’t take my word for granted – take it with a grain of salt, test it yourself. In the real world PHP 7 may be even faster than HHVM.

Some Final Tips

HHVM seems like a cool technology (that’s why I packed it on Docker). However, I have to warn you, use it with caution. Zend Engine is a mature, battle-tested tool. PHP in general is battle tested, and doesn’t suffer from hype. That doesn’t mean that you can’t experiment with it. That isn’t the only problem. With WordPress many plugins and themes may not work with HHVM. They were built on PHP, and HHVM is not 100% compatible with PHP.

Conclusion

In this article, we saw what HHVM is and how to setup WordPress with HHVM.

I see HHVM as a positive move from Facebook, not only from the Open Source perspective. In this new age of web where every language is jumping on the server-side train, competition rises. It’s not necessary to increase fragmentation, but it makes the whole scene different and innovative.

PHP itself has borrowed many things from Java, or Laravel from Ruby on Rails. Except being cool, fast and with new approach, the team behind HHVM has also pushed the team behind Zend Engine to become even better. PHP 7 was one of the best updates to hit the PHP community. As PHP itself is so coupled with Zend, competition from other teams, is a good thing. Also, a few may know that HHVM is not the only Virtual Machine in the game. Tagua VM is a new VM written with Rust. Also, their approach is different, where the main idea was to be memory safe.

Being curious, experimenting and production are different most of the time. However, if it happened that you’ve tried HHVM yourself, I want to hear from you. What are your experiences with HHVM? Have you tried to benchmark it on a real site? What (if any) problems have occurred?