How to Setup HHVM on the Ubuntu VPS?


My initial VPS (Virtual Private Server) only had 512M RAM, 1 core and 10G HDD. Later I upgraded to 2 cores, 1G RAM and 20G HDD because of an out-of-memory incident. However, a few days ago, the traffic grows (major of them are from bots), the VPS could barely handle and that is why I upgraded again to 3 cores and 2GB RAM.

The PHP version at VPS is 5.5 and thus it has a inbuilt accelerator, OpCache which caches PHP opcode. The Opcache is introduced to replace APC (Another PHP Cache), which is a open source project that is mainly used to speed up PHP code before version 5.4.x or earlier versions. The HHVM works in a similar way. It compiles the PHP code into assembly at first time and then the assembly code if found and still valid will be executed without re-interpreting the same PHP code. This idea is very similar to the .NET platform where the .NET languages such as C# are translated and compiled to native code at runtime (JIT = Just In Time) compiler. That is why the .NET applications run slow at first but tend to speed up later because they are compiled at runtime to native code and cached.

Of course, there are other new features. The new language [Hack] is based on PHP. We know the PHP language is a loosely type language meaning that you don’t have to specify a type (in fact there are no ways to explicitly do this) for any variables. The types are used when they need to be. The Hack language is a gradual type language meaning that the first time you define a type, and the type cannot be changed. This helps promote robust code and improves the development efficiency. This is rather a big topic that should be introduced in a separate post.

The installation of HHVM on Linux (in my case, Ubuntu) is rather simple.

1
2
3
4
5
6
root@uploadbeta:/var/www/helloacm.com/htdocs/hhvm# lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 14.04.1 LTS
Release:        14.04
Codename:       trusty
root@uploadbeta:/var/www/helloacm.com/htdocs/hhvm# lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 14.04.1 LTS
Release:        14.04
Codename:       trusty

You could either download the pre-compiled binaries or the source code and compile it locally. The following installs from a pre-compiled binary repository.

wget -O - http://dl.hhvm.com/conf/hhvm.gpg.key | apt-key add -
echo deb http://dl.hhvm.com/ubuntu saucy main | tee /etc/apt/sources.list.d/hhvm.list
apt-get update
apt-get install hhvm

You then can run the following command to configure the apache or ngix server.

/usr/share/hhvm/install_fastcgi.sh

In fact, after you install hhvm, you can use it at command line to execute the PHP source code, like this:

hhvm hello-world.php
hhvm-help How to Setup HHVM on the Ubuntu VPS? apache server optimization php programming languages ubuntu Virtual Private Server web programming webhosting

HHVM command line parameters

We can disable temporarily the Apache server:

sudo service apache2 stop

And run the hhvm server, by listening on port 80 (so you have to stop other servers that listen to the same port):

hhvm -m server

And then you can test the PHP code in browser, which is passed to HHVM server.

If you experience a 404 not found error after installation and configuration, you probably need to add the following line to /etc/hhvm/server.ini

hhvm.server.fix_path_info = true

And restart hhvm using sudo service hhvm restart.

I have tested and monitored the server for a little while and to be honestly did not see any difference probably because of the unnoticeable traffic. However, the processes listed by command htop changed from apache2 -k start to hhvm

We can also create a PHP file that has the following content and test if HHVM is working in the browser:

<?php
  echo  defined('HHVM_VERSION')?'Using HHVM':'Not using HHVM';
?>

The wordpress sites are runnable using HHVM but HHVM seems not supporting GB2312 encoding (and maybe others as well) because it messes up with the characters. The photo gallery PHP code showed a blank page using HHVM and I still haven’t figured out the cause.

I still decided to get rid of HHVM, the current package: Apache2 + PHP5.5 + Mysql works quite well. Maybe I will bring HHVM back in the future if the VPS experience huge traffic again.

sudo service hhvm stop
sudo apt-get remove hhvm

We need to restore the apache2 settings after HHVM uninstallation.

sudo a2dismod proxy_fcgi
sudo a2dismod proxy
rm /etc/apache2/mods-enable/hhvm*.conf
sudo service apache2 restart

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
970 words
Last Post: How to Add a Share URL automatically at the end of each post and page for WordPress?
Next Post: How to Login to WordPress when [Away Mode] is enabled by iThemes Security Plugin?

The Permanent URL is: How to Setup HHVM on the Ubuntu VPS?

Leave a Reply