Installing, Configuring, and Using the XHP PHP Extension by Facebook

Updated June 23, 2012 to add a doctype

Facebook announced the release of XHP yesterday. It is an extension for PHP which provides enhancements to the PHP language syntax and gives the language some convenient capabilities.

In this post I will provide you links to the essential sources of information about XHP, show some XHP sample code, and discuss the installation process for XHP on Ubuntu with Apache and PHP5.

The Essential Information
Download XHP at GitHub
How XHP Works
Building XHP
Configuring XHP

Example XHP Sample Code:

<?php
require_once('/home/somewhere/source/xhp/php-lib/init.php');
$name  = 'Calvin Freitas';
$title = "{$name} loves XHP!";
$url   = 'https://calvinf.com/';
 
$permalink = 'https://wp.me/pcoLC-4e';
 
#create a <head> element
$head =
  <head>
   <title>{$title}</title>
  </head>;

#create a div
$div =
  <div id="header">
    <h1>{$title}</h1>
  </div>;
 
$footer = 
  <div id="footer">
    Copyright 2012 by <a href={$url}>{$name}</a>
  </div>;

$body =
  <body>
    {$div}
    <div>
      <p>My name is {$name} and you should go visit <a href={$url}>my website</a> now.</p>
      <p>Read my <a href={$permalink}>guide to installing XHP on Ubuntu</a>. It includes the source code for this page. Comments and corrections are welcome.</p>
    </div>
    {$footer}
  </body>;

echo
<x:doctype>
  <html>
    {$head}
    {$body}
  </html>
</x:doctype>;
?>

A few syntax notes
XHP forces you to properly nest tags — make sure you close them properly or it won’t compile and it will throw errors. Because XHP auto-escapes, when you’re creating a link (such as in an a element), you don’t need to put quotes around it.

<a href={$permalink}>guide to installing XHP on Ubuntu</a>

Installing XHP on Ubuntu
Server: Ubuntu 9.10 running on a slice at the Rackspace Cloud.
Web server: Apache 2
Apache installation guide (Slicehost article, but generally applicable)

Below I will go into more detail about the installation process.

There are packages for all the prerequisites in Ubuntu (installation will be similar on Debian, though package names my vary).

sudo apt-get install php5 php5-common php5-cli php5-dev libapache2-mod-php5
sudo apt-get install build-essential gcc flex bison re2c

The package manager will include other prerequisite packages.

After you’ve installed these packages, download the latest tagged release of XHP from GitHub to your server.

Then, you’ll need to make it. Unzip/untar the file, go to that directory, and run the following commands. (Use sudo as necessary.)
phpize
./configure
make
make install

Note regarding make test: the tests failed for me because it wasn’t finding my currently installed PHP5 modules. I ran make install and it worked anyway. There should be a way to include the modules path in the compilation, but at this time I have not found it.

After installing the module, you must configure Apache to include the XHP module in the php.ini file. On my system, the file is located in /etc/php5/apache2. Find the line with extension_dir — mine is the following:
extension_dir = "/usr/lib/php5/20060613/"

In /etc/php5/apache2/conf.d, create a file called xhp.ini.
xhp.ini
#load xhp
extension=xhp.so

Now, at this point everything should work, but some users are reporting issues with XHP on Ubuntu systems, but there is a solution – you may need to include files from the php-lib folder (in the source code folder where you ran make). See the “require_once” function used in the sample code above.