Tuesday, February 11, 2014

Configure HHVM with Apache + FastCGI on Centos 6.5

On centos you can run regular fastcgi processes with mod_fcgid package. In order to communicate with HHVM over TCP/IP you need to install mod_fastcgi which is available elsewhere on the internet.

 Follow these instructions to use apache + fastcgi with hhvm.

0. Pre-requisites

Make sure you add the following to /etc/hosts on your server where you are going to use curl/browser test.example.com

1. Install httpd-devel packages

[root@localhost yum.repos.d]# yum install httpd-devel -y

2. Download mod_fastcgi

 [root@localhost ~]# wget http://www.fastcgi.com/dist/mod_fastcgi-SNAP-0910052141.tar.gz

3. Compile and install mod_fastcgi

Make sure to change the top_dir variable in the Makefile to centos specific like below or else package wont compile

top_dir      = /usr/lib64/httpd

[root@localhost ~]# tar zxf mod_fastcgi-SNAP-0910052141.tar.gz 
[root@localhost ~]# cd mod_fastcgi-SNAP-0910052141
[root@localhost mod_fastcgi-SNAP-0910052141]# cp Makefile.AP2 Makefile
[root@localhost mod_fastcgi-SNAP-0910052141]# make
[root@localhost mod_fastcgi-SNAP-0910052141]# make install

4. Start the hhvm server in fastcgi mode

[root@localhost ~]# /usr/bin/hhvm --mode server -vServer.Port=9000 -vServer.Type=fastcgi 
mapping self...
mapping self took 0'00" (34658 us) wall time
loading static content...
loading static content took 0'00" (3 us) wall time
page server started
all servers started

5. Create a dummy apache configuration file

[root@localhost ~]# cat /etc/httpd/conf.d/1.conf 

    ServerName test.example.com
    DocumentRoot /var/www/localhost
    LoadModule fastcgi_module modules/mod_fastcgi.so
    FastCgiExternalServer /var/www/localhost -host

[root@localhost ~]# 

6. Create document root and simple php file

mkdir -p /var/www/localhost && echo '<?php echo "hello world"; ?>' > /var/www/localhost/index.php

7. Start httpd from another terminal

[root@localhost ~]# service httpd restart
Stopping httpd:                                            [  OK  ]
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName
                                                           [  OK  ]
8. Fire a request and see that you get response from HHVM
 nareshv @ localhost $ curl http://test.example.com/index.php ; echo
hello world!
 nareshv @ localhost $
 nareshv @ localhost $ curl http://test.example.com/404.php ; echo
Not Found
 nareshv @ localhost $

9. Final configurations

Once you have proper working setup like above, create a configuration file for hhvm and run it in daemon mode.

Have Fun!