Test Your PHP Application on PHP 7

PHP 7 is coming, which is nice, but what does it mean for the majority of PHP developers? PHP as a community is notoriously slow in adoption, some of us are still waiting for 2012’s new shiny to be available as standard on our hosting platforms. However with the performance benefits and a few really nice new features, PHP 7 is well worth everyone’s attention, and it’s actually quite easy to get started so here’s my quick howto.

Get A PHP 7 Platform

These examples use Rasmus’ php7dev box because it’s self-contained so it won’t affect any other part of your setup, and because it’s super-simple to get started with on any platform. It’s a vagrant box running Debian 8 and with a really easy way of switching PHP versions, including 7. Multiple PHP versions is super handy because then you can very easily check if your PHP 7 bug really is a PHP 7 bug or if it was there on PHP 5.6 as well.

To get a new project set up you need to make the files available in the PHP 7 VM, and set up an nginx server block (equivalent of a virtual host) to point to it. If your project needs storage behind it, you can either connect to those on your host machine, or the VM has MySQL and PostgreSQL already installed.

Mount Your Project As A Shared Folder

Do this step before booting the virtual machine (or just vagrant halt). Add a line to Vagrantfile to tell the VM to share another folder:

    config.vm.synced_folder "/home/lorna/projects/joindin", "/vagrant/joindin"

I added mine immediately before the end at the bottom of the file – the first directory path is where it is on my laptop, and the second is where it will be in the VM. Now when you vagrant up and SSH in (vagrant ssh from the same directory as the Vagrantfile), you should see your directory is there with the files present.

Configure Nginx

While I mostly use Apache with mod_php, this box defaults to using nginx and it’s easy to set up even if you haven’t used it before. All we need to do is create a new file in /etc/nginx/conf.d, and add something like (there’s a lot of this, I obviously copy/pasted the starting point from somewhere but I’m pretty sure it has everything that most applications need so try it):

server {
    listen       80;
    server_name  api.joindin.php7;
    root   /vagrant/joindin/joindin-api/src/public;
    index  index.php;
    access_log  /var/log/nginx/default-access.log  main;
    error_log   /var/log/nginx/default-error.log;

    error_page   500 502 503 504  /50x.html;

    location = /50x.html {
        root   /var/www/default;
    }

    location / {
        try_files $uri $uri/ @rewrite;
    }
    location @rewrite {
        rewrite ^(.*)$ /index.php;
    }

    location ~ \.php {
        include                  fastcgi_params;
        fastcgi_keep_conn on;
        fastcgi_index            index.php;
        fastcgi_split_path_info  ^(.+\.php)(/.+)$;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_intercept_errors on;
        fastcgi_pass unix:/var/run/php-fpm.sock;
    }
}

Edit your server_name and root settings as appropriate, then restart nginx with:


sudo service nginx restart

At this point, the VM is ready but our host machine (in this case my laptop) doesn’t know where to send the traffic.

Set Up Your Host File

The VM will by default have an IP address of 192.168.7.7 and hopefully you know where your hosts file is. I’m on Ubuntu so mine is /etc/hosts and I just need to add one line to it:


192.168.7.7 api.joindin.php7

You should now be able to hit your chosen hostname from your browser and see your PHP application working (or not). Error logs are in /var/log/nginx, and as I say, it’s worth switching back to PHP5.6 (by running newphp 56 – then you want newphp 7 to switch back later) to check if the problem is actually PHP 7 or if you set something else up wrong.

If you have test suites, please run them, and put your application through its paces. Any bugs you find, try to narrow down the replication case and report them to the appropriate place – this might be your team, but could just as easily be a library, a framework, or PHP itself. The only way to get PHP and its ecosystem as good as it can be before it goes stable is for all of us to do with it before release all the things we’ll want to do after!

Getting Going With PHP 7

Hopefully this gave you a quick-start on a very easy platform to try out code on. I wrote it for testing an existing system but you could use it for playing with new code, or really whatever you are interested in. It was originally built to help with testing both the language itself and also the extensions – if you want to get involved with those intiatives, there’s plenty still to do and you can find us at http://gophp7.org/gophp7-ext/, we would love to have some more hands helping us get PHP ready and now you have the tools you need as well!

5 thoughts on “Test Your PHP Application on PHP 7

  1. Pingback: PHP Annotated Monthly – August 2015 | JetBrains PhpStorm Blog

  2. Pingback: August 2015 Newsletter - Nomad PHP

Leave a Reply

Please use [code] and [/code] around any source code you wish to share.

This site uses Akismet to reduce spam. Learn how your comment data is processed.