Installing HHVM
Throughout the following recipes, we will install the latest HHVM 3.9.x version on Apache and NGINX. HHVM has gained large popularity over the last couple of year and is well-known for its high performance on Magento. While Magento 1 was not supported by default, Magento 2 is. This recipe will show you how to do it.
Tip
HHVM (also known as the HipHop Virtual Machine) is a virtual machine for PHP developed by Facebook, with an associated just-in-time (JIT) compiler.
Deploying HHVM on a Magento 2 website should lead to performance improvements across your web shop.
Getting ready
For this recipe, you will need a preinstalled Apache or NGINX setup. No other prerequisites are required.
How to do it...
For the purpose of this recipe, let's assume that we need to create an HHVM hosting environment. The following steps will guide you through this:
- First, we will start installing HHVM on Apache; later, we will do the same with NGINX.
- Next, we will install HHVM on Apache2 using the official prebuilt package by Facebook. Run the following command on the shell:
echo "deb http://dl.hhvm.com/ubuntu wily main" | sudo tee -a /etc/apt/sources.list.d/hhvm.list
- Before we can install HHVM, we need to authorize the package by installing a signed key:
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 0x5a16e7281be7a449
- Now we will update our Ubuntu system using the latest HHVM packages:
apt-get update && apt-get -y install hhvm
- Once every HHVM package is installed, we can check whether everything is in order by running the following command on the shell:
hhvm --version
The output of this command is as follows:
root@mage2cookbook:~# hhvm --version HipHop VM 3.10.1 (rel)
- Since the setup of PHP-FPM, we have started using the internal port 9000 to handle all traffic on PHP. Now we will use the same port for HHVM. Stop PHP-FPM as it is still running and start HHVM using the following command from the shell:
service php7.0-fpm stop && service hhvm start
- If you need to run dedicated HipHop code such as Hack (the default HipHop programming language) in the future, you may want to change your Apache configuration. Restart your Apache and HHVM server:
ProxyPassMatch ^/(.+\.(hh|php)(/.*)?)$ fcgi://127.0.0.1:9000/var/www/html/$1
- Now, let's check whether HHVM and Apache are working. Create a PHP file called
hhvm.php
in the/var/www/html
directory:<?php if (defined('HHVM_VERSION')) { echo "HHVM"; } else { echo "PHP"; }
If you have followed steps 1 to 8, you will be able to see if HHVM works with your web server. Go to your favorite browser and search using your
yourdomain.com/hhvm.php
. You should now see a HHVM notice on your screen.In newer versions of HHVM, the default
phpinfo();
tag works just fine on your screen. Go to your favorite browser and search using youryourdomain.com/phpinfo.php
: - If everything is working fine, we have completed the Apache HHVM setup. Now let's do the same for the NGINX and HHVM setup.
As we have already installed the HHVM packages in steps 1 to 5, we now need to combine them with NGINX.
- Before we continue, it is important to use either a clean DigitalOcean Droplet with NGINX running or you can disable the Apache server using the following command from the shell:
service apache2 stop && service nginx start
The following is the content of NGINX configuration file:
server { listen 80; server_name yourdomain.com; root /var/www/html; index index.php index.html; access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; location ~ \.(hh|php)$ { fastcgi_index index.php; fastcgi_pass 127.0.0.1:9000; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } location ~ /\.ht { deny all; } }
In the preceding example, we altered the location for PHP and Hack support:
location ~ \.(hh|php)$ {
- You can now restart your NGINX and HHVM server to connect them using the follow command from the shell:
service nginx restart && service hhvm restart
- Now let's check whether HHVM and NGINX are working. Create a PHP file called
hhvm.php
or use the one in the previous Apache HHVM setup in the/var/www/html
directory:<?php if (defined('HHVM_VERSION')) { echo "HHVM"; } else { echo "PHP"; }
If you have followed steps 9 to 11, you will be able to see if HHVM works with your web server. Go to your favorite browser and search using your yourdomain.com/hhvm.php
.
You should now see a HHVM notice on your screen, but you can also use yourdomain.com/phpinfo.php
.
How it works…
Let's recap and find out what we did throughout this recipe. In steps 1 through 4, we use the official HHVM repository and install the software. The installation process is similar to the PHP-FPM setup. The important difference between HHVM and PHP-FPM is that HHVM does not have additional modules to install.
In step 7, we change ProxyPassMatch
a little bit, and add the .hh
extension parameter only. By default, the .hh
extension is used only when creating HipHop (Hack)-dedicated code.
In step 10, we do the same procedure for NGINX. The only thing that we change is the .hh
extension in the location. As HHVM runs by default on port 9000, we do not change anything here.
There's more…
If you want to check whether HHVM is running fine, use one of the following commands:
service hhvm status netstat -anp | grep hhvm
To check which server is running, check the headers by running the following command:
curl -I http://mage2cookbook.com/hhvm.php
The output of this command is as follows:
root@mage2cookbook:~# curl -I http://mage2cookbook.com/hhvm.php HTTP/1.1 200 OK Server: nginx/1.9.6 X-Powered-By: HHVM/3.10.1
Once we switch back to Apache, we will see the following:
service nginx stop && service apache2 start root@mage2cookbook:~# curl -I http://mage2cookbook.com/hhvm.php HTTP/1.1 200 OK Server: Apache/2.4.17 (Ubuntu) X-Powered-By: HHVM/3.10.1