How to install NGINX and HHVM with PHP5-FPM Fallback on Ubuntu 16/14

Written by diegorbaquero | Published 2017/01/19
Tech Story Tags: ubuntu | nginx | hhvm | php | fpm

TLDRvia the TL;DR App

In this tutorial I will be guiding you with steps to install NGINX stable, HHVM 3.9 and PHP 5 FPM.

  • Nginx is a great webserver or reverse proxy that comes with a default configuration much faster than Apache 2.4 with its default MPM (Prefork).
  • HHVM is a virtual machine that uses JIT (Just in time) compilation aproach.
  • PHP-FPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation with some additional features useful for sites of any size, especially busier sites.

A little setup background:

  • Clean KVM Machine with Ubuntu 16/14 LTS with 1GB RAM and 1 CPU Core from Vultr (Less than 1GB RAM will cause you trouble if you bundle MySQL)
  • Root access (Else you must do sudo [command])

First, Nginx:

add-apt-repository ppa:nginx/stableapt-get update && apt-get upgradeapt-get install nginx

Second, HHVM:

apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0x5a16e7281be7a449add-apt-repository "deb http://dl.hhvm.com/ubuntu $(lsb_release -sc) main"apt-get updateapt-get install hhvmupdate-rc.d hhvm defaults/usr/bin/update-alternatives --install /usr/bin/php php /usr/bin/hhvm 60/usr/share/hhvm/install_fastcgi.sh

We want to run HHVM on a Unix Socket, so let’s edit it’s configuration:

nano /etc/hhvm/server.ini

Comment “hhvm.server.port = 9000” with ; and add “hhvm.server.file_socket = /var/run/hhvm/hhvm.sock”

It should look like this:

; php optionspid = /var/run/hhvm/pid

; hhvm specific;hhvm.server.port = 9000hhvm.server.file_socket = /var/run/hhvm/hhvm.sockhhvm.server.type = fastcgihhvm.server.default_document = index.phphhvm.log.use_log_file = truehhvm.log.file = /var/log/hhvm/error.loghhvm.repo.central.path = /var/run/hhvm/hhvm.hhbc

Save with Ctrl + X and then ‘Y’

Now PHP-FPM:

apt-get install php5-fpm

This should install FPM and listen on /var/run/php5-fpm.sock by default

We are done installing! Let’s configure things now. Let’s start by creating a upstream conf file for nginx:

nano /etc/nginx/conf.d/upstream.conf

Add the following and save:

upstream php {server unix:/var/run/hhvm/hhvm.sock;server unix:/var/run/php5-fpm.sock backup;}

As you can see, PHP-FPM will be a backup in case HHVM fails.

Now let’s edit Nginx’s HHVM conf file:

nano /etc/nginx/hhvm.confg

Replace “fastcgi_pass 127.0.0.1:9000;” with “fastcgi_pass php;” . It should look like this:

location ~ \.(hh|php)$ {fastcgi_keep_conn on;fastcgi_pass php;fastcgi_index index.php;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;include fastcgi_params;}

Restart the services with:

service hhvm restart && service php5-fpm restart && service nginx restart

Test with:

curl -I localhost/test.php

Should be a 404 (As the actual test.php doesn’t exist) with these headers:

Server: nginx/1.8.0X-Powered-By: HHVM/3.9.0

And that’s all! We have latest stable Nginx with latest HHVM in fastcgi with php-fpm fallback!

Hacker Noon is how hackers start their afternoons. We’re a part of the @AMIfamily. We are now accepting submissions and happy to discuss advertising &sponsorship opportunities.

To learn more, read our about page, like/message us on Facebook, or simply, tweet/DM @HackerNoon.

If you enjoyed this story, we recommend reading our latest tech stories and trending tech stories. Until next time, don’t take the realities of the world for granted!


Published by HackerNoon on 2017/01/19