Skip to main content

Silex running on HHVM

This blog post might be outdated!
This blog post was published more than one year ago and might be outdated!
· 2 min read
Stephan Hochdörfer
Head of IT Business Operations

First of all I assume you already got HHVM running with nginx. If this is not the case feel free to follow these steps to get everything up and running. To install Silex we will use Composer, so let's install all the needed requirements and Composer itself. As root user run the following commands:

apt-get install curl git unzip
curl -sS https://getcomposer.org/installer | sudo hhvm --php -dHttp.SlowQueryThreshold=30000
mv composer.phar /usr/local/bin/composer

While downloading/installing Composer I got an error stating "Download failed: Failed to open https://getcomposer.org/composer.phar (Connection time-out)". Luckily the error message seems to be wrong, Composer was downloaded properly and will run fine. So simply ignore the error.
To be able to run Composer via HHVM this line needs to be added to ~/.bashrc and we need to run source ~/.bashrc to load the changes:

alias composer="hhvm -v ResourceLimit.SocketDefaultTimeout=30 -v Http.SlowQueryThreshold=30000 -v Eval.Jit=false /usr/local/bin/composer"

Now let's create a basic composer.json file with the Silex dependency. Change to /usr/share/nginx/www (the default document root of nginx) and create a composer.json file:

{
"require": {
"silex/silex": "v1.2.0"
}
}

Let Composer download all the needed dependencies by invoking the following command:

composer install

Next up is to fine-tune the nginx configuration. The silex documentation states that the following configuration is enough:

server {
#site root is redirected to the app boot script
location = / {
try_files @site @site;
}

#all other locations try other files first and go to
# our front controller if none of them exists
location / {
try_files $uri $uri/ @site;
}

#return 404 for all php files as we do have a front controller
location ~ \.php$ {
return 404;
}

location @site {
fastcgi_keep_conn on;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root/index.php;
include fastcgi_params;
}
}

Last not least we need to set-up a minimalistic Silex application:

<?php

require_once __DIR__.'/vendor/autoload.php';

$app = new Silex\Application();

$app->get('/', function () {
return "Hello World!";
});

$app->run();

Now point your browser to http://localhost:8080/ and you should see your Silex app responding.

I recently learned that you can check if your project's dependencies are able to run on HHVM by uploading your composer.lock file here. So this might be a good starting point for you ;)