Simply Fast WordPress [7] 1000x Faster WordPress Tuning – HHVM

This is a series of articles explaining how to speed up WordPress, the use of which is growing rapidly for CMS-based business sites and media sites. In the next few articles, I will offer techniques to tune WordPress’ speed by 1000x. --Kengyu Nakamura, Prime Strategy Co., Ltd.

In the previous article, we installed PHP 7 to get double the performance of PHP 5, and sped up WordPress 12.5x the default environment.

In this article, we are going to continue tuning by installing Facebook's PHP-compatible HHVM (HipHop Virtual Machine).

WordPress Japanese version
WordPress Japanese version

This technique continues from previous tuning, so please refer to earlier articles to get to this point.

Install HHVM

HHVM (HipHop Virtual Machine) is a PHP processor developed by Facebook compatible with any PHP execution environment. Its features include using byte-code compatible with OPCache and APCu that we have installed with PHP 5.6 and PHP 7. It also has a JIT compiler which allows it to produce native code that can be understood by the CPU by compiling frequently used code at the time of execution.

HHVM site

Both PHP 5 and PHP 7 convert PHP source code into byte-code once, then send that byte-code to the virtual machine to execute. In order to cache this byte-code and speed up the process, PHP accelerators like OPCache and APC are used.

So what's different about HHVM? It still converts source code into byte-code and stores the cache, but the major difference is that it changes often-used code into native code to be executed.

In order to do that, HHVM uses the cache and a JIT compiler. On first access to  the server, there won't be a performance difference from PHP 5 or PHP 7, but as the JIT compiler warms up, it begins to execute faster. In a normal operational environment, this causes no issues, but I will note that your performance will falter whenever you initialize HHVM or restart it.

Also, HHVM has a high compatibility with PHP 5 and PHP 7, but that doesn't mean all of its features and modules are. In particular, it will be necessary to edit themes and plugins to guarantee compatibility. This is something you need to consider for your own site.

Anyway, let's install HHVM. Please refer to Facebook's GitHub wiki and follow the guide "Building and installing hhvm on CentOS 7.x" to install HHVM 3.12 from an rpm package. In my example, installing HHVM may rely on Remi repositories used for PHP 5.6 or PHP 7.

First, let's download the packages needed to install HHVM.

[root@ip ~]# yum update -y
[root@ip ~]# yum install cpp gcc-c++ cmake git psmisc {binutils,boost,jemalloc,numactl}-devel \ {ImageMagick,sqlite,tbb,bzip2,openldap,readline,elfutils-libelf,gmp,lz4,pcre}-devel \ lib{xslt,event,yaml,vpx,png,zip,icu,mcrypt,memcached,cap,dwarf}-devel \ {unixODBC,expat,mariadb}-devel lib{edit,curl,xml2,xslt}-devel \ glog-devel oniguruma-devel ocaml gperf enca libjpeg-turbo-devel openssl-devel \ mariadb mariadb-server {fribidi,libc-client}-devel make -y
[root@ip ~]# yum install inotify-tools -y

Some of the needed EPEL repositories will be installed if you follow the above guide, so here I assume you already have them and have omitted them. One library is missing, which is why we use the inotify-tools command.

Next, install HHVM 3.12.

[root@ip ~]# rpm -Uvh http://mirrors.linuxeye.com/hhvm-repo/7/x86_64/hhvm-3.12.0-1.el7.centos.x86_64.rpm

With this, installation is complete. Check to make sure everything is error free with the following command.

[root@ip ~]# hhvm --version
HipHop VM 3.12.0 (rel)
Compiler: tags/HHVM-3.12.0-0-g440e6a096b2db8a449b9690a72f2f1792530e86a
Repo schema: 033b8dedf4af99b892f2488b103a8d67a58ff816

Next, we will edit systemd and HHVM settings files. We will change HHVM's server port from 9001 to 9000 and set a timezone.

First, copy the systemd service file into a preferred directory.

[root@ip ~]# cp -p /usr/lib/systemd/system/hhvm.service /etc/systemd/system/

In your copied file, delete the port number from the Service directive. The file should look like this:

1. [Unit]
2. Description=HHVM HipHop Virtual Machine (FCGI)
3. [Service]
4. ExecStart=/usr/local/bin/hhvm --config /etc/hhvm/server.ini --user nginx --mode daemon -vServer.Type=fastcgi
5. [Install]
6. WantedBy=multi-user.target
7. /etc/systemd/system/hhvm.service

[Line 4 is modified]

Once that file has been edited, restart systemd to have it recognize the file.

[root@ip ~]# systemctl daemon-reload

Next, edit HHVM's settings file, "/etc/hhvm/server.ini", in the following manner:

1. ; php options
2. ;pid = /var/log/hhvm/pid
3. ; hhvm specific 
4. hhvm.pid_file = "/var/log/hhvm/pid"
5. hhvm.server.port = 9000
6. hhvm.server.type = fastcgi
7. (omitted)
8. [date]
9. date.timezone = UTC
10.  default_socket_timeout = 120
11.  memory_limit = 256M

[Lines 5 and 9 are modified]