How to install HHVM with Nginx in Ubuntu 16.04/Debian 8 – DebYum

share on:
install hhvm with nginx in ubuntu 16.04

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.

let’s start this tutorial on how to install HHVM with Nginx in Ubuntu 16.04.

Steps to install HHVM with nginx in Ubuntu 16.04 are as follows:

 

I’m assuming here that you have installed a fresh minimal Ubuntu 16.04/Debian 8 setup.

 

Install Latest Nginx in Ubuntu 16.04/Debian 8.

Update your system with apt-get update

shell> apt-get update

 

Download the key and then add the key as it is necessary to sign the nginx packages and repository to the apt program keyring.

shell> wget http://nginx.org/keys/nginx_signing.key
shell> sudo apt-key add nginx_signing.key

 

 

To download Pre-Built Packages for Stable version of Nginx.

Append the following lines to /etc/apt/sources.list.

For Ubuntu

deb http://nginx.org/packages/ubuntu/ codename nginx
deb-src http://nginx.org/packages/ubuntu/ codename nginx

 

For Debian

deb http://nginx.org/packages/debian/ codename nginx
deb-src http://nginx.org/packages/debian/ codename nginx

 

 

To download Pre-Built Packages for Mainline version of Nginx

Append the following lines to /etc/apt/sources.list.

For Ubuntu

deb http://nginx.org/packages/mainline/ubuntu/ codename nginx
deb-src http://nginx.org/packages/mainline/ubuntu/ codename nginx

 

For Debian

deb http://nginx.org/packages/mainline/debian/ codename nginx
deb-src http://nginx.org/packages/mainline/debian/ codename nginx

 

Replace codename” with Ubuntu/Debian distribution’s codename.

Debian:
Debian Version         Codename
Debian 7.x                   wheezy
Debian 8.x                   jessie

 

Ubutnu:
Ubuntu Version         Codename
Ubuntu 12.04               precise 
Ubuntu 14.04               trusty 
Ubuntu 16.04               xenial

 

For example, if you are using Ubuntu 16.04, Add following lines at the end of /etc/apt/sources.list.

deb http://nginx.org/packages/mainline/ubuntu/ xenial nginx
deb-src http://nginx.org/packages/mainline/ubuntu/ xenial nginx

 

vi /etc/apt/sources.list

nginx-hhvm-ubuntu-16-04-add-repo

 

Start the Nginx service

shell> systemctl status nginx
shell> systemctl enable nginx
shell> systemctl start nginx

 

 

Install and Configure HHVM.

Add the HHVM repository to your system.

shell> sudo apt-get install software-properties-common
shell> sudo apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0x5a16e7281be7a449
shell> sudo add-apt-repository "deb http://dl.hhvm.com/ubuntu $(lsb_release -sc) main"
shell> sudo apt-get update
shell> sudo apt-get install hhvm

 

nginx-hhvm-ubuntu-16-04-install-hhvm

 

Check if HHVM is working.

nginx-hhvm-ubuntu-16-04-hhvm-finish

 

Add this file into your document root.

shell> echo -e "<?php\n\nphpinfo();\n\n?>" > /usr/share/html/test.php

 

Restart nginx and HHVM server to apply these changes.

shell> systemctl restart nginx
shell> systemctl restart hhvm

 

Finally, Enter this address in your Browser.

http://server-ip-address/test.php

hhvm-working

 

 

HHVM Advanced Usage configuration: FastCGI

HHVM has built-in support for two server types: Proxygen and FastCGI.

HHVM-FastCGI works in the same way as PHP-FPM. HHVM, when running in FastCGI mode, is started independently of the web server which is Nginx in this case.

We can configure HHVM to listen on a TCP socket (default port 9000) or a Unix socket. However,  you should configure HHVM to listen on a Socket instead of Port for better performance and CPU and memory consumption.

Another important point is, Nginx needs to be configured to know where your PHP files are and how to forward them to HHVM to execute.

The /usr/share/hhvm directory contains few scripts that can help us in this situation. The  script  /usr/share/hhvm/install_fastcgi.sh will configure nginx correctly for stock installs. The script when executed, will direct nginx to take any file that ends in .hh or .php and send it to HHVM via FastCGI.

shell> cd /usr/share/hhvm/
shell> bash install_fastcgi.sh

 

More Info:

This script works only with a default Nginx configuration without any FastCGI configurations. This script adds the configuration file /etc/nginx/hhvm.conf to the default Nginx server block configuration /etc/nginx/sites-enabled/default.

Although, If you are using custom FastCGI configuration in your server block then you will have to manually add new FastCGI configuration in your nginx configuration.

Additionally, If you are using Mainline version of Nginx then install_fastcgi.sh may not be able to configure FastCGI setup for your server. In that case, you may need to manually add following setting in your nginx.conf settings.

 vi /etc/nginx/conf.d/default.conf
.....
.....
location ~ \.(hh|php)$ {
 root /usr/share/nginx/html;
 fastcgi_pass 127.0.0.1:9000;
 # fastcgi_pass unix:/var/run/hhvm/sock;
 fastcgi_index index.php;
 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
 include fastcgi_params;
 }

 

Also, make sure HHVM is listening on port 9000.

shell> cd /etc/hhvm

 

 vi server.ini

; php options

pid = /var/run/hhvm/pid

; hhvm specific

hhvm.server.port = 9000
hhvm.server.type = fastcgi
hhvm.server.default_document = index.php
hhvm.log.use_log_file = true
hhvm.log.file = /var/log/hhvm/error.log
hhvm.repo.central.path = /var/run/hhvm/hhvm.hhbc

 

To use HHVM as a PHP alternative.

shell> sudo /usr/bin/update-alternatives --install /usr/bin/php php /usr/bin/hhvm 60

 

Once again, restart nginx and HHVM server to apply these changes.

shell> systemctl restart nginx
shell> systemctl restart hhvm

 

 

Configure HHVM to listen on UNIX Socket instead of TCP SOCKET.

If yow want HHVM to listen on a Socket instead of Port for better performance and CPU and memory consumption. Then follow these steps.

vi /etc/hhvm/server.ini

; php options

pid = /var/run/hhvm/pid

; hhvm specific

# hhvm.server.port = 9000
 hhvm.server.file_socket=/var/run/hhvm/hhvm.sock
 hhvm.server.type = fastcgi
 hhvm.server.default_document = index.php
 hhvm.log.use_log_file = true
 hhvm.log.file = /var/log/hhvm/error.log
 hhvm.repo.central.path = /var/run/hhvm/hhvm.hhbc

 

Also, make changes in FastCGI configurations of your Nginx.

 location ~ \.(hh|php)$ {
 root /usr/share/nginx/html;
 # fastcgi_pass 127.0.0.1:9000;
 fastcgi_pass unix:/var/run/hhvm/hhvm.sock;
 fastcgi_index index.php;
 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
 include fastcgi_params;
 }

 

Restart Nginx and HHVM server to apply these changes.

shell> systemctl restart nginx
shell> systemctl restart hhvm

 

 

Tweaking /etc/hhvm/php.ini for HHVM.

The default value of memory limit in php.ini for HHVM is equal to 17179869184 bytes (17 GB) which are more than enough to make your VPS unusable/unresponsive if it has only few GB of RAM.

So if you have 2GB (at least) of RAM then it is safe to dedicate around 50-55% of RAM to HHVM. Add this new variable after the ; php options section in /etc/hhvm/php.ini.

Here are few other useful settings.

; php
memory_limit = 1200M
upload_max_filesize = 64M
post_max_size = 64M
max_execution_time = 300
max_input_time = 300
max_file_uploads = 20

 

 

Problems while Running HHVM with Latest version of Nginx in Ubuntu 16.04 and Debian 8.

If you see errors like below in /var/log/nginx/error.log.

connect() to unix:/var/run/hhvm/hhvm.sock failed (13: Permission denied) while connecting to upstream, client:...

 

then you need to make sure nginx is able to write to the php5-fpm or hhvm Unix socket.

Check the ownership of hhvm.sock

shell> ls -l /var/run/hhvm/hhvm.sock
srwxrw---- 1 www-data www-data 0 Nov 28 18:00 /var/run/hhvm/hhvm.sock

 

Check Name of your Nginx user.

shell> cat /etc/nginx/nginx.conf | grep user
user nginx;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'"$http_user_agent" "$http_x_forwarded_for"';

 

As you can see that Nginx is running as user nginx whereas www-data is the owner of /var/run/hhvm/hhvm.sock.

To make this all work, you need to add Nginx in the group www-data

shell> usermod -aG www-data nginx

 

Once again, restart Nginx and HHVM server to apply these changes.

shell> systemctl restart nginx
shell> systemctl restart hhvm

 

To conclude, HHVM can really make a difference in your server’s performance and can help you increase your Page load speed. I have tried to be as brief as possible while covering this topic,

Still, if I have missed anything please update us through comment box. I will keep updating the same based on feedback’s received. Good Day.????

 

Resources.

 

share on:
engy

engy

Hello there, My name is Rishi Guleria and I work as a Linux system administrator. I have created this blog to share what I have learned so far and to learn new things. Don't forget to leave the feedback. Have a great day. :)

Leave a Response

share on: