HHVM is an open source virtual machine designed for executing programs written in Hack and PHP. The advantage of HHVM is an increase in web request throughput and reduction in memory consumption compared to traditional PHP hosting.It was developed and open sourced by Facebook.

For this tutorial, I’m using Ubuntu 14.04 x64 with 512 MB from Digital Ocean. It only costs $5 per month!

Note: HHVM doesn’t support 32 bit operating systems.

Installing HHVM

Firstly, let’s update and upgrade:

sudo apt-get update && sudo apt-get upgrade

Next, we’ll install HHVM:

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

HHVM should have been successfully installed, to make sure run the following command:

hhvm --version

The above command should show you the HHVM version number.

You may encounter the following errors: libgmp and libmemcachedutil. If this happens, run the following command for an libgmp error:

sudo apt-get install libgmp-dev

and the following command for an libmemcachedutil error:

sudo apt-get install libmemcached-dev

We can also create a Hello World PHP script and test it from the command line using HHVM:

vi test.php

Insert the following (shift+i):

<?
echo "nn Hello World nn";
?>

Then save and exit:

:wq!

Afterwards, run the following command:

hhvm test.php

Hello World should show in the command prompt.

Apache/Nginx Using HHVM

Apache

Execute the following command:

/usr/share/hhvm/install_fastcgi.sh

Nginx

If you are using Nginx with PHP-FPM, you’ll need to modify the configuration file which is typically located at /etc/nginx/sites-available/default and make sure the following section is all commented like so:

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#       location ~ .php$ {
#       fastcgi_split_path_info ^(.+.php)(/.+)$;
#       # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
#
#       # With php5-cgi alone:
#       fastcgi_pass 127.0.0.1:9000;
#       # With php5-fpm:
#       fastcgi_pass unix:/var/run/php5-fpm.sock;
#       fastcgi_index index.php;
#       include fastcgi_params;
#}

If you are not using PHP-FPM or after you completed the above steps if you are using PHP-FPM on Nginx, run the following command:

/usr/share/hhvm/install_fastcgi.sh

Testing with a script

Let’s make sure Apache/Ngnix is using HHVM:

Go to (cd) /var/www for Apache or /usr/share/nginx/html for Nginx. If the public folder is somewhere else, go there.

vi test.php

Add the following:

<?php
echo  defined('HHVM_VERSION')?'Using HHVM':'Not using HHVM';
?>

Save and exit.

Go to the script like so: http://<ip address>/test.php (where <ip address> is your server’s IP address.)

You should get Using HHVM

Congratulations: HHVM is now installed on your server and you now have the ability to use HHVM with Apache or Ngnix!