Test Your PHP Application on PHP 7
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!
Pingback: PHP Annotated Monthly – August 2015 | JetBrains PhpStorm Blog
Hi Lorna,
Starting with RC1, ServerPilot has made PHP 7.0 available on all servers. So you can now spin up a server at a provider like DigitalOcean, connect it to ServerPilot, and be using PHP 7.0 without needing to do any configuration. As new RCs and the final release comes out, they’ll get updated automatically on your server.
https://serverpilot.io/blog/2015/08/20/php-7.0-available-on-all-servers.html
Thanks,
Justin
Looks pretty interesting, I’m downloading it at the moment. Goodness knows how large it is though as ETA is 90 minutes! :)
Pingback: August 2015 Newsletter - Nomad PHP
PHP 7 is quite a big improvement over the previous versions. It is significantly faster than PHP 5.6. When I upgrade to PHP 7 on my PHP 7 hosting (https://www.cloudways.com/en/php-cloud-hosting.php ) server, I noticed a 200% increase in performance.