We were hoping to have this announcement out in time for Christmas gift but it was not to be. Instead it’s an early New Years gift. Nonetheless, we’re happy to announce a whole slew of new options for PHP projects to make them faster and more robust on Platform.sh: PHP 7.1 support, async support, and PThreads support.
Big ones first: As of today we offer support for PHP 7.1. It’s just like PHP 7.0, only better in every way. To use it, simply swap out the
type line in your application configuration file:
And now when deployed your application will use PHP 7.1. That’s it, you’re done. We recommend doing so if you can. PHP 7.1 includes a number of improvements, including a small performance boost and a number of new syntactic features. See the PHP announcement and Migration Guide for all the shiny goodness.
This is also a good time to remind everyone that if you’re running a version of PHP older than 5.6 you are running unsupported software, often with known security vulnerabilities, and after this week even 5.6 will get only critical security fixes. See the PHP Supported Versions page for details. We strongly urge you to upgrade to PHP 7.0 or 7.1 as quickly as possible, for the continued support, vastly improved speed, and plethora of new features. It’s a one line, possibly one character change to update your site on Platform.sh. Why not make that your New Years resolution?
Async PHP support
PHP usually runs as a CGI process, and at Platform.sh we use PHP-FPM behind Nginx: a fairly standard and robust configuration. However, CGI-style PHP has some limitations such as no support for long-running processes, WebSockets, or other new fanciness.
A handful of projects have sought to address that by implementing their own non-blocking IO libraries, allowing PHP to run as a single-process asynchronous process in much the same way as Node.js. The most notable of these are ReactPHP and AmPHP. Both run as their own command line application and listen to incoming requests through Nginx, allowing them to handle WebSockets, run as queue workers, or other such tasks.
Thanks to our new support for start commands on PHP containers, it’s now very straightforward to to spin up a ReactPHP- or AmPHP-based container on Platform.sh. We’ve provided a basic Hello World ReactPHP example as well as a WebSocket chat server in both ReactPHP and AmPHP to show how it works, but of course your WebSocket or queue processing decisions are limited only by your imagination.
What’s more, if you’re running on the new-and-shiny PHP 7.1 container then
ext/event is available as well.
ext/event moves the core event loop of supporting async libraries down into optimized C code for even more speed. It’s also trivial to enable, so we recommend using it whenever possible. Just add the following to your
.platform.app.yaml file and ReactPHP will start using it automatically:
Pthreads: Multithreaded PHP
The Pthreads extension is a PHP extension that offers support for real, honest-to-goodness posix threads in PHP. It’s not always easy to setup, though. For one, it only works with a special Zend Thread Safe (ZTS) version of PHP, which is not the default on most distributions. It also then requires an extra extension to work. To top it all off, pthreads is only compatible with the CLI version of PHP, not the CGI version. Like async support, though, that makes it a very good choice for queue workers, WebSockets, and other stand-alone persistent daemons.
For that reason, we’re happy to announce that our new PHP 7.1 containers are running PHP 7.1 ZTS, and include the Pthreads extension. In our testing we found no performance penalty to using the ZTS version of PHP (in fact it was very slightly faster in general, which is quite curious!), and enabling Pthreads via
.platform.app.yaml is dead simple:
Poof. You can now use the
web.commands.start directive to start a script that uses Pthreads and do… whatever you want it to do. Queue workers, WebSockets, something else weird-but-cool? Let us know if you find something especially cool to do with it.