In this post I am going to show you how to setup PHP in a different way, instead of using the our old friends Apache and mod_php we will use Nginx web server, php-fpm and HHVM stack.

So why should we ditch Apache? I have been using Apache for over 10 years now and it is very good at what it does but for small website being served on small server it’s not so good as it’s using a ton of memory then add to that MySQL server and soon you will find your memory being quickly swallowed by these two.

HHVM (Hip Hop Virtual Machine) is best left to the creators to describe it.

What is HHVM?

HHVM is an open-source virtual machine designed for executing programs written in Hack and PHP. HHVM uses a just-in-time (JIT) compilation approach to achieve superior performance while maintaining the development flexibility that PHP provides.

PHP-FPM (FastCGI Process Manager) comes bundled with PHP 5.5 and above and is a better alternative to PHP FastCGI a full list of FPM feasters are on the site, so without further ado lets install all this.

Install PHP and PHP-FPM

First let install PHP and PHP-FPM, all these are really easy with Ubuntu as they are in the main repositories, so to do this lets install PHP with some standard modules. In the terminal type

When that is done run  php -v and you should get back something like

Now with PHP installed lets move on

Install Nginx

Again this is straight forward as Nginx is in the main repositories so in the terminal enter

To check your web server in a browser navigate to ‘localhost’ if you are installing this on you computer, if you are installing this on a remote server navigate to that address and you should see


Okay so now we will install HHVM

Install HHVM

This is a little more complicated as we will have to add the HHVM repository and Ubuntu doesn’t have it in its the main repository so again in the terminal enter

This will download and install HHVM’s repository key now add the repository to apt by entering

Now update apt

And install HHVM by

Once this has finished you should see at the end this

This tells us we have do some things to get HHVM working so in the terminal run

This will install some defaults in nginx then

This will make sure HHVM will start when the machine is rebooted, now will still have to configure some things before our php script will use HHVM.


So to get HHVM to work we will need to add some rules to the NGINX config so my default nginx site config looks like this

On line 4 we are telling nginx are web root will be in the folder ‘/home/ubuntu/public’ this is where all our files will be served from.

On line 11-14 we are telling nginx to try serving the requested page first, if it’s not there then route all request to our index.php page with all it’s request arguments. This is good for MVC applications.

On line 16-26 we are telling nginx to serve all files that end in ‘.php’ to be served through cgi php5-fpm and using the unix socket  instead of tcp port. Unix socket has slightly better performance.

Ubuntu automatically configures php5-fpm to use the unix socket. It might be different on other linux distros so beware of this. You may want to check the php5-fpm config file and change the setting in the fpm pool  listen = /var/run/php5-fpm.sock

You can check this by adding <?php phpinfo(); to your ‘index.php’ file in your web root and navigating to it in a browser and you should see the php settings info page. If you see nothing then some thing my be wrong so recheck your setting are correct.

Now to add HHVM to run as default and will will putt php5-fpm as a fallback option as HHVM can fail sometimes. So the HHVM cgi run listens by default on a TCP sockets that being so again we will alter our config so we need to change our php location block to

On line 2 we are telling nginx to intercept all error so we can catch them and deal with them.

On line 5 we are telling nginx to send all 500, 501, 502 and 503 server errors to our fallback.

On line 11 we are telling nginx to pass all php requests to to hhvm listening on port and port 9000.

Now we need to add our fallback block so we add

All this does is pass all request to php5-fpm via the unix socket if hhvm gives us a server error.

so our our whole block should like

All that’s need to do is restart nginx with

and that should be it. You should now notice faster execution times with hhvm.

Just a word of warning not all php applications are compatible with hhvm and also hhvm does not give browser errors only a white screen if you have any errors in your code all errors are logged in the hhvm log file.

Anyway that’s all for now with this one, have fun and experiment and if you have question on this use the comments below and I will try to answer them as best as I can.


  • thanks,

    however i am having problems with this, keeps showing me 404 error on firefox only

    • Shaun Freeman

      This error is ‘Page Not Found’ so the best thing to debug this is to check your nginx logs first. Also check the page is in your root directory, and make sure your root directory does actually exists.

      • Hello;

        So i can use the following on my HHVM nginx location block?

        proxy_intercept_errors on;
        proxy_buffer_size 128k;
        proxy_buffers 4 128k;
        #error_page 500 501 502 503 = @fallback;

        fastcgi_keep_conn on;
        fastcgi_buffers 16 128k;
        fastcgi_buffer_size 128k;

        fastcgi_read_timeout 1200;

  • I am actually thankful to the owner of this web page who has shared this enormous post at at this

    • Shaun Freeman

      Thank you.