Jesse's Software Engineering Blog
Ubuntu HHVM Install
Facebook’s HHVM is a Virtual Machine which runs PHP, and Hack Lang, through a just-in-time (JIT) compiler to “achieve superior performance” in executing PHP code. From HHVM’s website:
Rather than directly interpret or compile PHP code directly to C++, HHVM compiles Hack and PHP into an intermediate bytecode. This bytecode is then translated into x64 machine code dynamically at runtime by a just-in-time (JIT) compiler. This compilation process allows for all sorts of optimizations that cannot be made in a statically compiled binary, thus enabling higher performance of your Hack and PHP programs.
As of HHVM 3.0, it is recommended to run HHVM using FastCGI. From the HHVM docs:
HVM-FastCGI works much the same way as PHP-FPM. An application server (HHVM running in FastCGI mode) is started independently of the web server (Apache, Nginx, etc.). It listens on a TCP socket (by convention this is localhost:9000). The web server listens on port 80. When a new request comes in, the web server makes a connection to the application server (or reuses one of the previously open connections) and communicates with it using FastCGI protocol. The web server decodes HTTP protocol and supplies application server with information such as the path of the file to be executed, request headers and body. The application server computes the response and sends it back to the web server, again using FastCGI protocol. The web server is then in charge of sending back the HTTP response back to the client.
The HHVM application server gets traffic forwarded from the web server, compiles the PHP code (via the JIT compiler) and passes the response back to the server. The HHVM compilation process is faster than the native PHP Zend engine + Opcache.
HHVM Installation on Ubuntu 14.04 is simple:
# install web server (Apache, Nginx) and PHP first sudo apt-get install apache2 sudo apt-get install php5 php5-common php5-cli # get the key, update the repos 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 # install HHVM sudo apt-get update sudo apt-get install hhvm # install fastcgi sudo /usr/share/hhvm/install_fastcgi.sh # enable HHVM on boot up sudo update-rc.d hhvm defaults
As noted above it is recommended to run HHVM in FastCGI mode. The install_fastcgi.sh script, which is installed along with HHVM, will automatically detect the web server, Apache or Nginx, and set up the correct modules for the web server/application communication to work. For Apache, HHVM uses the ProxyPassMatch directive to forward all .php, and .hh, traffic to port :9000, which HHVM is listening to. This can be seen in the hhvm_proxy_fcgi.conf file:
vi /etc/apache2/mods-available/hhvm_proxy_fcgi.conf ProxyPassMatch ^/(.+\.(hh|php)(/.*)?)$ fcgi://127.0.0.1:9000/var/www/$1
mkdir /var/www/test vi /var/www/test/index.php <?php phpinfo();
When loading the test the phpinfo() output should read:
Note: Notice the root for the fcgi port is /var/www. It is important to place HHVM project folders there and not in html/
HHVM Command Line
Now that HHVM has been installed, PHP scripts can be executed via the HHVM compiler not only via a web server but also command line:
There are numerous run time options that can be passed into the command line compiler:
hhvm -v Eval.Jit=true -v Http.SlowQueryThreshold=30000
It’s important to note that by default the command line scripts do not use the JIT. JIT compilation is meant for long-running processes where the time and effort needed to compile the native code is paid off by repeated uses i.e. page loads. It is a bit difficult to find information on configuring the JIT but when using HHVM command line it is import to configure the various different JIT ini settings: JitASize, JitAStubsSize, JitGlobalDataSize, etc. I will write more on HHVM configuration in future posts.
Here is an interesting article on using HHVM with PHP’s Composer and PHPUnit.
With the ease of setting up HHVM and the compatibility of running existing PHP code/frameworks, it is definitely worth taking the time to get to know HHVM. Although with improvements to PHP 7 boasting similar speeds to HHVM, the ability to use Hack Lang, Facebook’s version of PHP, will keep HHVM a viable option for large scale, memory intensive programming projects. Also with the announcement of HHVM long term support, Facebook has made it clear it will continue to work on and perfect its PHP JIT compiler. One thing to consider before using HHVM, is HHVM’s support for common PHP libraries.