How to install HHVM, Nginx, MariaDB in Ubuntu 14.04 LTS

It is just a record for reference.

0) I have my Ubuntu 14.04 installation done on my VirtualBox

512MB memory

1 CPU.

Install Ubuntu 14.04 server with openssh server only. No other server software installed.

1) Install Nginx

Nginx can be found in the Ubuntu repository, but it is often outdated. To get the latest stable version, add the following PPA to system.

sudo add-apt-repository ppa:nginx/stable
sudo apt-get update
sudo apt-get install nginx
sudo service nginx start

The latest version of Nginx as of this post is 1.6.2.

nginx-version

Enter “http://your-ip/” into web browser see the default welcome page.

nginx-welcome

2) Install MariaDB

MariaDB is better than MySQL.

sudo apt-get install software-properties-common
sudo apt--key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xcbcb082a1bb943db
sudo add-apt-repository 'deb http://sfo1.mirrors.digitalocean.com/mariadb/repo/10.0/ubuntu trusty main'
sudo apt-get update
sudo apt-get install mariadb-server
sudo service mysql start

Enter the root password when installation prompt it.

3) Install PHP and HHVM

Install php5-fpm as a fallback for HHVM.

First php5-fpm

sudo apt-get install php5-fpm php5-mysql php5-curl

Then install HHVM

wget -O - http://dl.hhvm.com/conf/hhvm.gpg.key | sudo apt-key add -
echo deb http://dl.hhvm.com/ubuntu trusty main | sudo tee /etc/apt/sources.list.d/hhvm.list
sudo apt-get update
sudo apt-get install hhvm

After HHVM is installed, proceed to configure it for Nginx with the command:

sudo /usr/share/hhvm/install_fastcgi.sh

Last, restart HHVM

sudo service hhvm restart

Right now the Nginx and HHVM are working together.

Put a test file under Nginx root folder.


sudo vi /var/www/html/phpinfo.php

and paste the following line:

<?php

phpinfo();

Load the url: http://your-ip/phpinfo.php and see the infomation.

phpinfo of HipHop

4) Configure sites to run in Nginx and HHVM with php5-fpm fallback

Now it is the most important part of this post. Create a virtual host to run the website.

Create a new config file that holds website detail:


sudo vi /etc/nginx/sites-available/my-site

And paste following snippet:

server {
listen 80;
listen [::]:80;

root /var/www/html;

# Add index.php to the list if you are using PHP
index index.html index.htm;

server_name mywebsite.com;
include hhvm-with-fallback.conf;

location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location @fallback {
# include snippets/fastcgi-php.conf;
#
# # With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}

Server_name is the name or domain name. and root folder is the location where my web files to be served.

HHVM has some bugs of crashing occasionally without restarting itself; this will cause the website to fail with a 500 error. In this case, create a fallback system whereby php5-fpm will take over when HHVM fails. Notice the location @fallback block in the config above.  Then create the “hhvm-with-fallback.conf” file which is a modified version of the hhvm.conf file.


sudo vi /etc/nginx/hhvm-with-fallback.conf

location ~ \.(hh|php)$ {
    proxy_intercept_errors on;
    error_page 500 501 502 503 = @fallback;
 
    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;
}

Enable this site, then test Nginx settings and restart it.


sudo nginx -t

sudo service nginx restart