Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Test Your PHP Application on PHP 7

DZone's Guide to

Test Your PHP Application on PHP 7

Use a PHP 7 Vagrant box to quickly test your PHP out your applications on PHP 7. Simple mounting, nginx config, and host file setup are all that's required.

· Web Dev Zone
Free Resource

Make the transition to Node.js if you are a Java, PHP, Rails or .NET developer with these resources to help jumpstart your Node.js knowledge plus pick up some development tips.  Brought to you in partnership with IBM.

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 how-to.

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!

Learn why developers are gravitating towards Node and its ability to retain and leverage the skills of JavaScript developers and the ability to deliver projects faster than other languages can.  Brought to you in partnership with IBM.

Topics:
php ,php 7

Published at DZone with permission of Lorna Mitchell, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}