10 februari 2015

Running Symfony2 on PHP7

In a previous article (written in Dutch), I talked about our Vagrant setup and how we use shell provisioning to keep things simple.

For this follow-up article, I wrote a script that compiles PHP7 from source. This script was tested on Debian 8. It compiles PHP7 with all options Debian (and Dotdeb) usually do. You probably don't use many of these options, but now you can easily strip down your configure string and re-run the shell script. It activates opcache, has the MySQL extensions and compiles PHP CLI and PHP-FPM.


You can get the repository from This is not an example of our shell provisioning, but it should help you grasp the idea behind it.

You can extend the box to Nginx, MySQL, composer and Symfony2. Or you can do it manually and choose your favorite webserver, database server and PHP framework.

Installing Nginx, MySQL, composer and Symfony2

# as user root
apt-get install -y nginx mysql-server
curl -sS | php -- --install-dir=/usr/bin
# as user vagrant
composer.phar create-project symfony/framework-standard-edition /vagrant/symfony

Configure your Nginx in a way that it points to the symfony installation by default in /etc/nginx/sites-enabled/default and restart nginx:

server {
root /vagrant/symfony/web;
location / {
try_files $uri @rewriteapp;
location @rewriteapp {
rewrite ^(.*)$ /app.php/$1 last;
location ~ ^/(config|app|app_dev)\.php(/|$) {
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
error_log /var/log/nginx/symfony.error.log;
access_log /var/log/nginx/symfony.access.log;

Now, browse to your Vagrant box's IP (eg: if you used my git repository) and you will see the following in the Symfony debug toolbar:



I ran boom against the installation to get some differences in speed between different PHP versions. It's just a simple metric on a vanilla installation to give you an idea, always run your own benchmarks to make decisions about your own projects. I compiled 5.3, 5.4, 5.5, 5.6 in the same way as PHP 7. Versions 5.3 and 5.4 use APC, the other ones use opcache.

This chart shows the number of requests per second that were performed (more is better). You can see that PHP 7 can do a lot more in comparison with the other versions. Response times drop from 0.2269 to 0.0865 seconds in the production environment.


I added an event listener on kernel.response to capture the memory usage:

$dispatcher = $this->get('event_dispatcher');
$dispatcher->addListener('kernel.response', function (\Symfony\Component\EventDispatcher\Event $event) {
memory_get_usage() . ';' . memory_get_peak_usage() . "\n",

This chart shows the peak memory usage and memory usage in dev and prod environments. It is interesting to see that the peak memory usage is actually higher with PHP 7, but overall the memory usage drops from 4,54 MB to 2,10 MB.



I also installed HHVM (nightly) and ran the same tests.

The response times showed an average of 0.1163 seconds (between PHP 5.6 and 7, but closer to PHP 5.6). The memory usage reported around 2 MB, which is comparable with PHP 7. Memory usage and peak usage reported the same value.