Installing HHVM with Apache 2.4 on CentOS 6.5 (64 bit)

This is for those of you who love being on the bleeding edge of technology like me. This will be a brief introduction to installing Apache 2.4 and Facebook’s HHVM (Hip Hop Virtual Machine) in order to increase web application performance.

Apache 2.4 is the newest iteration of Apache’s web server with multiple performance improvements and Facebook’s HHVM is the new kid on the block that brings massive performance gains to PHP. HHVM achieves this by compiling PHP into bytecode, which is then translated into machine code by HHVM’s JIT (Just in Time) compiler. When using HHVM, this also gives the ability to use Facebook’s new statically typed language called HACK.

HHVM also works with most of PHP’s popular frameworks. If you work with popular enterprise level frameworks like Symfony 2, this can be an amazing asset.


First we need to prepare the install process by adding a few repositories for yum to use. Since CentOS 6.5 (as of the time of this post) doesn’t contain Apache 2.4 in their base or epel repositories, we need to look for another option. We could compile from source, however Remi Collet and Jan Kaluza from Red Hat have created a RHEL repo httpd24-epel that contains Apache 2.4 that is perfect for our needs. This repo installs Apache 2.4 in /opt. View why here.


For HHVM we have the same couple of options. We could either build it from source, or we can use Naresh Kumar’s wonderful repo called the Hop 5 Repo.

Using these repos is the best choice since we can manage our Apache 2.4 and HHVM installs with our package manager and not have to worry about future problems.


— PREPARATION —

Go to the repo directory

cd /etc/yum.repos.d

Add the Remi Repo

sudo wget http://repos.fedorapeople.org/repos/jkaluza/httpd24/epel-httpd24.repo

Add the Hop 5 Repo

sudo wget http://www.hop5.in/yum/el6/hop5.repo


— INSTALL PROCESS —

Install Apache 2.4

sudo yum install httpd24-httpd httpd24-httpd-devel httpd24-mod_ssl

This will place Apache 2.4 under /opt.
See why here

Install HHVM

sudo yum install hhvm

hhvm_install_f


— CONFIGURATION PROCESS —

Configure Apache
This article uses a VHost and assumes you already know how to setup one for Apache, however a Virtual Host is not necessary for HHVM.

Edit httpd.conf

cd /opt/rh/httpd24/root/etc/conf
vim httpd.conf

Ensure this line is not commented out

IncludeOptional conf.d/*.conf

Configure a Virtual Host for Apache

Go to the Apache 2.4 config directory

cd /opt/rh/httpd24/root/etc/conf.d

Create a Virtual Host config file as root (if non existent)

vim httpd24-vhosts.conf

Add the following lines

<VirtualHost *:80>
  ServerName hhvmtest.dev
  DocumentRoot /var/www/html/hhvmtest
  ErrorLog logs/hhvmtest.dev-error_log
  CustomLog logs/hhvmtest.dev-access_log common

  # where HHVM is running   # use either Proxy Pass or ProxyPassMatch   # ProxyPass routes all traffic to FastCGI   ProxyPass / fcgi://127.0.0.1:9000/var/www/html/hhvmtest   # ProxyPassMatch regular expression routes only PHP files   ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9000/var/www/html/hhvmtest/$1
  <Directory /var/www/html/hhvmtest> # relaxed permissions for testing     AllowOverride all     Options -MultiViews     Require all granted   <Directory> </VirtualHost>


— TESTING —

Start HHVM as a server in FastCGI mode in the background (run as root or sudo it)

/usr/bin/hhvm --mode server -vServer.Port=9000 -vServer.Type=fastcgi &

Verify HHVM is running and PID

ps aux | grep hhvm

hhvm-running

Start Apache

sudo service htttpd24-httpd start

Create Document Root

cd /opt/rh/httpd24/root/var/www/html
mkdir hhvmtest

Create index.php

cd /opt/rh/httpd24/root/var/www/html/hhvmtest
vim index.php

Add the following lines


 <?hh echo 'test ';
  $test = 2 + 2;
  echo  $test;
  echo phpinfo();

Create index.hh

vim index.hh

Add the following lines


 <?hh echo 'test ';
  $test = 2 + 2;
  echo  $test;
  echo phpinfo();


Now open your browser and go to

http://hhvmtest.dev/index.php

If everything goes correctly you should see the string

test 4 HipHop

Now open your browser and go to

http://hhvmtest.dev/index.hh

If everything goes correctly you should see the string

test 4 HipHop

When you output

phpinfo();

You should see the string 'HipHop'.

HHVM returns the string HipHop when you output phpinfo.



Also verify HHVM is working for php tags by changing in both index.php and index.hh

<?hh

to

<?php 


If you want to run HHVM on system boot. Either create an init bash script or just add the above HHVM line to rc.local

Go to rc.local

cd /etc/rc.local
vim rc.local

Add the following line to the bottom of rc.local

/usr/bin/hhvm --mode server -vServer.Port=9000 -vServer.Type=fastcgi &

That’s it enjoy! 😀

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top