Installing and using Hack

Installing and using Hack

Today we are going to run through how to install and utilise Hack. So you can install it and begin using it yourself.

Installing HHVM
Installing HHVM is as easy as booting a Vagrant VM, simply place the following in your VagrantFile:
VAGRANTFILE_API_VERSION = “2”

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
# Use Ubuntu 12.04 LTS (64bit)
config.vm.box = “hashicorp/precise64″
config.vm.network “public_network” # Create a bridge network

# Install HHVM
config.vm.provision “shell”, inline: <<-shell
apt-get update
apt-get install python-software-properties -y –force-yes
add-apt-repository ppa:mapnik/boost
wget -O – http://dl.hhvm.com/conf/hhvm.gpg.key | sudo apt-key add –
echo deb http://dl.hhvm.com/ubuntu precise main | sudo tee /etc/apt/sources.list.d/hhvm.list
apt-get update
apt-get install hhvm-nightly -y –force-yes
apt-get install screen vim -y –force-yes
debconf-set-selections <<< ‘mysql-server-5.5 mysql-server/root_password password pa$$’
debconf-set-selections <<< ‘mysql-server-5.5 mysql-server/root_password_again password pa$$’
apt-get install mysql-server -y –force-yes
shell
end
Once you have your VagrantFile ready to go, simply issue a vagrant up command and wait.
After the machine boots you will be able to ssh in using vagrant ssh and play with HHVM.
Running HHVM
Once you have HHVM installed as above, you can simply call it using the hhvm command. For example, assuming our Calculator class is in calc.php we might run:
$ hhvm calc.php
In addition to simple command line usage, there are two ways to run HHVM for the web. The first, is the built-in server — this was the only way to run HHVM until recently, when the second option, FastCGI was added.
To use HHVM as FastCGI, it needs to sit behind a web server like nginx, similar to php-fpm. To install simply:
$ sudo apt-get install nginx
$ sudo mkdir /var/www
$ sudo chown vagrant:daemon /var/www
Next, add a new nginx config file to /etc/nginx/sites-available/hhvm.conf:
server {
server_name hhvm.dev;

root /var/www;
index index.php;

location ~ .(hh|php)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www$fastcgi_script_name;
include fastcgi_params;
}
}
And install it in place of the default:
$ sudo rm /etc/nginx/sites-enabled/default
$ sudo ln -s /etc/nginx/sites-available/hhvm.conf /etc/nginx/sites-enabled/hhvm.conf
$ sudo service nginx restart
Next, we setup HHVM to run as FastCGI by updating the /etc/hhvm/server.hdf (<= 2.4.*) or /etc/hhvm/server.ini (current nightlies, or >= 2.5.0) config file:
server.hdf
Server {
Port = 9000
Type = fastcgi
SourceRoot = /var/www
}

Log {
Level = Error
UseLogFile = true
File = /var/log/hhvm-error.log
Access {
* {
File = /var/log/hhvm-access.log
Format = %h %l %u %t ”%r” %>s %b
}
}
}

Repo {
Central {
Path = /var/log/hhvm/.hhvm.hhbc
}
}
server.ini
; php options

pid = /var/run/hhvm/pid

; hhvm specific

hhvm.server.port = 9000
hhvm.server.type = fastcgi
hhvm.server.source_root = /var/www
hhvm.server.default_document = index.php
hhvm.log.level = Error
hhvm.log.use_log_file = true
hhvm.log.file = /var/log/hhvm/error.log
hhvm.repo.central.path = /var/run/hhvm/hhvm.hhbc
Restart (or start) HHVM:
$ service hhvm restart
Note: prior to the HHVM 2.5/3.0 (or recent nightlies) the init scripts for Ubuntu (at least) were broken. Instead, you should use sudo killall hhvm && sudo service hhvm start to restart.
Finally, add an index.php to the webroot, /var/www and then access the web server (you can get the IP by calling ifconfig).

And in the case of HHVM, this will output simply:
HipHop

BE THE FIRST TO COMMENT

Leave a Reply