Organizational Research By

Surprising Reserch Topic

How To Install HHVM with Nginx on Ubuntu 14.04


Installation

For Ubuntu 14.04 there is an officially supported HHVM repository. To add this repository you have to import its GnuPG public keys with the command:

 
  • sudo apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0x5a16e7281be7a449

After that you can safely install HHVM's repository with the command:

 

Once you have the repository added you have to make apt, Ubuntu's software manager, aware that there are new packages which can be installed with it. This can be done by updating apt's cache with the command:

 
  • sudo apt-get update

Finally, you can install HHVM with the command:

 
  • sudo apt-get install hhvm

The above command installs HHVM and starts it for the first time. To make sure HHVM starts and stops automatically with the Droplet, add HHVM to the default runlevels with the command:

 
  • sudo update-rc.d hhvm defaults

Configuration

HHVM comes with a script which makes the integration with Nginx very easy. Provided you have a default Nginx installation, you can run the script without any arguments like this:

 
  • sudo /usr/share/hhvm/install_fastcgi.sh

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

If you have already modified your default server block with custom FastCGI configuration, such as the one for PHP-FPM, then you will have to manually replace your previous FastCGI configuration with this one:

/etc/nginx/sites-enabled/default

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

The above means that Nginx should use HHVM to process any .php or .hh (hack) requested file.

You will also need to restart Nginx to enable the change:

 
  • sudo service nginx restart

It also assumes that you haven't changed the default HHVM configuration that makes the service accessible at 127.0.0.1:9000. This setting comes from the main HHVM configuration file/etc/hhvm/server.ini which contains:

/etc/hhvm/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

In the above configuration you may notice the variable hhvm.server.port which determines that HHVM will be listening on TCP port 9000. Furthermore, unless otherwise specified, it will listen on localhost by default.

HHVM is considered for environments under heavy load so the first configuration change you can do is to make HHVM listen to a socket instead of a TCP port. Thus, the communication between Nginx and HHVM will require less CPU and memory.

To configure HHVM to listen on a socket, open the file /etc/hhvm/server.ini in your favorite editor such as with vim:

 
  • sudo vim /etc/hhvm/server.ini

Then remove the line starting with hhvm.server.port, and in its place add the following one:

/etc/hhvm/server.ini

hhvm.server.file_socket=/var/run/hhvm/hhvm.sock

Save the configuration file, and restart HHVM with the command:

 
  • sudo service hhvm restart

Next, you have to make Nginx aware of this change. For this purpose open the file/etc/nginx/hhvm.conf for editing:

 
  • sudo vim /etc/nginx/hhvm.conf

In this file make sure that the fastcgi_pass directive points to the HHVM socket and looks like this:

fastcgi_pass unix:/var/run/hhvm/hhvm.sock;

You will have to restart Nginx too for this change to take effect. For this purpose use the command:

 
  • sudo service nginx restart

The script /usr/share/hhvm/install_fastcgi.sh can save you some time, but there are things you have to adapt manually, especially in regards to your Nginx server blocks. For example, the default server block configuration opens as index files only index.html and index.htm files while directory listing is forbidden. This is one thing you should change for sure and include index.php files as index files too. To do this open again the configuration file for the default server block with your favorite editor:

 
  • sudo vim /etc/nginx/sites-enabled/default

Then go to the server part and add index.php at the line with indexes so that it looks like this:

/etc/nginx/sites-enabled/default

index index.html index.htm index.php;

Restart again Nginx for this setting to take effect:

 
  • sudo service nginx restart
asked Aug 30, 2015 in LINUX by rajesh
0 votes
24 views



Related Hot Questions



Government Jobs Opening


...