Setting up a Hacklang server using DigitalOcean and nginx

| 21 Comments

Last week at php[tek] I was inspired by Elizabeth Smith to experiment with Facebook’s new programming language Hack. But first, I needed an environment that I could work in. I had to jump through a few different tutorials to do it and I thought it may save others time if I compiled what I did in one place. In this post I’ll show you how to get a DigitalOcean server going with nginx and HHVM so we can write some Hack code. Don’t worry, it only takes about 20 minutes.

HHVMHACKNGINXDOMG… HUH?

Ok, let’s take a step back. What exactly do all these words and acronyms I’m throwing around mean? For the full history, I’d recommend checking out the in-depth wikipedia article on HHVM. But I’ll give you the tl;dr version – Hack is an open-source programming language created by Facebook for HipHop Virtual Machine (HHVM). Hack was released in March of 2014 and is based on PHP but gives you the ability to use both dynamic and static typing. Nearly the entire Facebook codebase has been migrated from PHP to Hack. HHVM itself provides performance gains for your Hack (or PHP) code by using a just-in-time compilation approach.

DigitalOcean is a service that we’ll use to quickly launch a new server (or to use their terminology – droplet) and we’ll be nginx as our web server so we can view our Hack content on the web. Now that we’ve got out of the way, let’s get started!

You get a server! You get a server! You get a server!

Currently, you can run HHVM on Linux and OSX (Windows support is not available but according to the docs is being actively worked on). HHVM on OSX is experimental and doesn’t include JIT compilation. Which leaves us with Linux. But Asking “How long does it take to compile HHVM?” is much like asking “How many licks does it take to get to the center of a tootsie pop?”.  The good news is there are some pre-built packages you can use. The bad news is they are only available 64-bit versions of Ubuntu or Debian. I didn’t have a server that met this criteria so I decided to set up a DigitalOcean droplet.

The DigitalOcean process makes it easy.  After you’ve created an account you can just hit the big “create” button to create a new droplet. Since we’re using this server for development, we can leave almost everything as they default. The one thing we’ll change is during “Select Image” section. We want to pick “Ubuntu 13.10 x64”.

 

Once you’ve created your droplet you can use these instructions to get the initial permissions foundation in place.

Go Go Gadget Virtual Machine

Now that you have your server ready, let’s install HHVM using the following commands:

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

After that completes, let’s test that hhvm installed correctly by running the following command:

hhvm --version

Prior to version 3.0.0 HHVM included a built-in webserver and you’ll find many tutorials that direct you to start by running the command `hhvm -m server`. Since we’ll be using a newer version of HHVM this will give you an error. Don’t believe me? Go ahead and give it a try. You should’ve just gotten a very informative error which includes instructions to “use your own webserver (nginx or apache) talking to HHVM over fastcgi.” That’s what we’ll be doing next.

Installing nginx

Recent W3techs data shows that nginx has passed Apache as the most used web server among top 10,000 sites so let’s go with nginx. The first thing we need to do is in install it. Run the command:

sudo apt-get install nginx

Then let’s start the server running the following command:

sudo service nginx start

Open up your web browser of choice and go to the IP of DigitalOcean server. You should see this page:

We have both HHVM and nginx installed and running but we need to allow them to communicate between each other. We’ll be using a protocol called FastCGI to facilitate this communication. Luckily, there’s a bash script that makes this really easy to set this up. Just run:

sudo /usr/share/hhvm/install_fastcgi.sh

One Last Thing

It’s almost time to write some Hack code. The last thing we need to do is update permissions on the html folder so we can write to it:

sudo usermod -a -G www-data youruser
sudo chown -R www-data:www-data /usr/share/nginx/html
sudo chmod -R 775 /usr/share/nginx/html
newgrp www-data

Stop, HammerCode Time!

We did it! We can now sling some Hack together. Let’s (hip)hop into our html folder and create a new file called info.php. I’ll be using vim but feel free to use your text editor of choice:

cd /usr/share/nginx/html/
vim info.php

Instead of starting with a <?php tag, Hack files start with <?hh. We’re going to write a basic file that echos the result of phpinfo():

<?hh
 echo phpinfo();

Save your file and then visit  http://yourDOip/info.php. If all goes well you should see:

Whose world is this? It’s yours!

You now have a server all set up to play with Hack. Not sure what to do? I’d recommend checking out the Hack cookbook. Have any questions or issues? Hit me up on twitter (@rickyrobinett) or e-mail (ricky@twilio.com).