Update - 30th Jan 2018

...only PHP 7.2+ is now supported (requiring the current master branch of pthreads). This is due to safety issues with ZTS mode on PHP 7.0 and 7.1.

Outdated Info

PHP can support multithreading for compute intensive workloads.
The tutorial below is divided into two parts. The first part will recompile PHP with ZTS enabled in order to allow us to then add the pthreads extension for the PHP CLI (not for Apache or FPM).
I will not dive into when not to use multithreading in PHP, but its fair to say that there are good reasons why it supported for the webserver, and hence the pre-built package that you install from the Ubuntu repositories.

Go to the releases page and grab the .tar.gz link for the latest release for the version of PHP you want.
By the time you read this tutorial, these versions are likely to be out of date but serve as a working example.

wget https://github.com/php/php-src/archive/php-7.0.27.tar.gz

Extract the source code and rename it to php-src so the rest of the steps in this tutorial work no matter what version you are running.

The important parts for pthreads are --enable-maintainer-zts and --enable-debug.

Build PHP.

make && sudo make install

Update the symlink for php to point to our custom build.

sudo rm /usr/bin/php
sudo ln -s /etc/php7/bin/php /usr/bin/php

Testing PHP Installation

You have now compiled PHP 7.0 from source with the necessary configuration necessary for installing pthreads later.

You can check your version by running:

php -v

You can also check that you have ZTS support which is required for pthreads:

php -a
echo PHP_ZTS; // this should output a 1 and not a 0

Install Pthreads

You have completed the first step of the tutorial which is to build your own version of PHP that will support installing pthreads.
Now it is time to actually add the extension.

Use PECL to install the pthreads extesion

sudo pecl install pthreads

PHP.INI

At the end of the previous step, the console will have told you that you need to add extension=pthreads.so to your php.ini file.
However, we don't have one yet for our custom built PHP so lets create it.

We are a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for us to earn fees by linking to Amazon.com and affiliated sites. More info.