Blog tvlooy

Running Symfony2 on PHP7

php, Symfony2 | February 10, 2015

This article was written for Intracto and first appeared on the Intracto blog on February 10, 2015.

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 7.7. 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.

It starts with PHP from git, but I reset the repository to the commit that I tested so it should work for anyone.

You can get the repository from https://github.com/tvlooy/php7-symfony-blog. 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

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

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

Symfony debug toolbar

Benchmark

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.

Symfony hello world benchmark request

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

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.

Symfony hello world benchmark memory

HHVM

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.