Using HipHop Virtual Machine (HHVM) on Ubuntu

HHVM is an open-source virtual machine designed for executing programs written in Hack and PHP, developed and open sourced by Facebook to process and execute programs and scripts written in PHP.

Facebook developed HHVM because the regular Zend+Apache combination isn’t as efficient to serve large applications built in PHP.

HHVM uses a just-in-time (JIT) compilation approach to achieve superior performance while maintaining the development flexibility that PHP provides. HHVM runs much of the world’s existing PHP.

According to their website, HHVM has realized over a 9x increase in web request throughput and over a 5x reduction in memory consumption for Facebook compared with the Zend PHP engine + APC (which is the current way of hosting a large majority of PHP applications).

Install HHVM on Ubuntu Server 12.04 LTS (64 bit)

sudo add-apt-repository ppa:mapnik/boost
sudo apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0x5a16e7281be7a449
echo deb http://dl.hhvm.com/ubuntu precise main | sudo tee /etc/apt/sources.list.d/hhvm.list
sudo apt-get update
sudo apt-get install hhvm

Install HHVM on Ubuntu Server 14.04 LTS (64 bit)

sudo apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0x5a16e7281be7a449
sudo add-apt-repository "deb http://dl.hhvm.com/ubuntu $(lsb_release -sc) main"
sudo apt-get update
sudo apt-get install hhvm

To start HHVM automatically on system boot up, run this command:

sudo update-rc.d hhvm defaults

and To use HHVM for /usr/bin/php even if php-cli installed:

sudo /usr/bin/update-alternatives –install /usr/bin/php php /usr/bin/hhvm 60

Configuring with Apache / Nginx
With HHVM comes a nifty configuration script that automatically sets up a CGI handler for either server.

Nginx
With Nginx, make sure to edit your server’s configuration file (by default /etc/nginx/sites-available/default) to disable FastCGI processing. Look for a section like the following and make sure it either does not exist or is entirely commented out (by adding # to the beginning of each line in the section):

# 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;
#}

Restart Nginx
sudo service nginx restart

Apache
With Apache simply run the following script.

sudo /usr/share/hhvm/install_fastcgi.sh

Restart Apache
sudo service apache2 restart

Using HHVM with PHP-FPM Fallback
Note: HHVM doesn’t play with all WordPress themes and plugins. Test your site properly before using this in production.

After Installation of HHVM:
Need to configure our web server to use HHVM,

sudo /usr/share/hhvm/install_fastcgi.sh

You can ignore the WARNING from following message.
# sudo /usr/share/hhvm/install_fastcgi.sh
Checking if Apache is installed
WARNING: Couldn’t find Apache2 configuration paths, not configuring
Checking if Nginx is installed
Detected Nginx installation
Checking for custom Nginx configuration
WARNING: Detected clashing configuration. Look at /etc/nginx/hhvm.conf for information how to connect to the hhvm fastcgi instance.
#

Configuring HHVM:
Editing server.ini:
Open server.ini file:

sudo nano /etc/hhvm/server.ini

If HHVM crashed then site must use PHP-FPM as fallback, so change HHVM server port from 9000 to 8000

hhvm.server.port = 8000

Editing php.ini
Open php.ini file:

sudo nano /etc/hhvm/php.ini

Add following towards end of file:

hhvm.log.header = true
hhvm.log.natives_stack_trace = true

Editing hhvm.conf:
Note: This editing is not required, as we are not using this file, but if have included hhvm.conf in your previous site then you also need to change HHVM port here.

sudo nano /etc/nginx/hhvm.conf

and change HHVM port from 9000 to 8000:

fastcgi_pass 127.0.0.1:8000;

Configuring FastCGI:

Open fastcgi.conf file:

sudo nano /etc/nginx/conf.d/fastcgi.conf

and add following line towards end of file:

fastcgi_keep_conn on;

Use HHVM and adding PHP5-FPM as fallback:

Open Upstream.conf file:

sudo nano /etc/nginx/conf.d/upstream.conf

and make sure it look like this:

# Common upstream settings

upstream php {
# server unix:/run/php5-fpm.sock;
server 127.0.0.1:8000;
server 127.0.0.1:9000 backup;
}

upstream debug {
# Debug Pool
server 127.0.0.1:9001;
}

Restart Services:

sudo service hhvm restart
sudo service php5-fpm restart
sudo service nginx restart

Testing:

Using web browser:
Just point your browser to https://example.com:22222/php/info.php, if you are seeing HipHop then, your server is using HHVM, otherwise it is using FPM

Using CURL:
Use following command to test from command line:

curl -I example.com

If you are able to see line like this:

X-Powered-By: HHVM/3.2.0

then your site is using HHVM, otherwise FPM
Log File:

You should check the HHVM error log file for any issues related to your site.

tail -f /var/log/hhvm/error.log