pthreads using php+zts rpms on centos

UPDATE: To make it work with PHP 7.1, you will need to download the latest master branch from pthread’s github¬†instead of PECL.

I recently had to take an old project from several years ago out of our SVN archives, it is a PHP CLI that uses the pthreads extension to do a bunch of work in parallel. It also abuses set_time_limit() to get around the fact that some other extensions and library functions in PHP fail to provide a way to set timeouts, allowing the parent thread to manage them.

No problem, I’ll just install pthreads again on my current rpm distribution of PHP 5.6 install on centos 6.5.

# pecl install pthreads
WARNING: channel "" has updated its protocols, use "pecl channel-update" to update
downloading pthreads-2.0.10.tgz ...
Starting to download pthreads-2.0.10.tgz (85,247 bytes)
....................done: 85,247 bytes
36 source files, building
running: phpize
Configuring for:
PHP Api Version: 20131106
Zend Module Api No: 20131226
Zend Extension Api No: 220131226
building in /var/tmp/pear-build-rootc3k10v/pthreads-2.0.10
running: /var/tmp/pthreads/configure
checking whether to enable Threading API... yes, shared
checking whether to enable pedantic locking... no
checking checking for ZTS... configure: error: pthreads requires ZTS, please re-compile PHP with ZTS enabled

No problem, I’ll just install zts. A quick yum install php56w-zts reveals there isn’t such a package in my repos.

My organization has been using Andy‘s fantastic webtatic php releases for several years. Sure enough checking his repo and I can’t find a recent zts repo, more googling reveals that with 5.5.1 zts is now rolled into the main rpm. Great, so I should have it right? Let’s just check what’s installed.

# php<tab>
php php-cgi php-config phpize
# ls /etc/php<tab>
php.d/ php.ini php.ini.rpmnew php.ini.rpmsave php-zts.d/
# ls /usr/lib64/php<tab>
php/ php-zts/

So clearly I have some kind of zts branch installed, but webtatic’s php 5.6 page only mentions using zts with apache’s Not with the php or php-cgi executables. At this point I did some more googling, but every post out there indicated I would have to build php myself. I absolutely did not want to do this, it would be hell for releasing this code to our many web servers, for one. I started to look into converting the code to pcntl which would work out of the box with my php cli.

But first a quick tweet @andytson and he’d get back to me a few hours later with what I was missing.

Aha! I can still use pthreads! I’m not really comfortable digging into the contents of rpm’s, so I’m honestly not sure I would have ever found this. It’s not documented anywhere nor does it follow the conventions set above. Why this one is zts-php and not php-zts is beyond me. Note that it’s in the php56w-devel package, but I already had this installed on my system.

# zts-php<tab>
zts-php zts-php-config zts-phpize

Andy even went a cut above and tweeted again to provide me the info I would need to build the pthreads extension, warning me there is no zts-pecl provided. This I could work with, thanks Andy!

# cd /tmp
# pecl download pthreads
# tar zxvf pthreads-2.0.10.tgz
# cd pthreads-2.0.10
# zts-phpize
# ./configure --with-php-config=/usr/bin/zts-php-config
# make

And I was the new owner of a You may need to yum install automake and autoconf to get the ‘configure’ and ‘make’ commands on your system, as well as some other -devel libraries. To put it into the general structure, I had to do a bit of copying.

# cp modules/ /usr/lib64/php-zts/modules/.
nano /etc/php-zts.d/pthreads.ini

And I was rolling. To double check everything was running properly I ran a grep on phpinfo.

# zts-php -i | grep -i thread
Thread Safety => enabled

Notice the Thread Safety line shows enabled, without ZTS it will be disabled. The pthreads line tells me my extension was properly loaded. I changed my cron job to execute zts-php instead of php and my project was working with pthreads again!

Please note that you’ll have to recompile pthreads each time you download a new version from webtatic, as Andy does not yet provide a php56w-pthreads rpm. Here’s to hoping!

I’m a big fan of the webtatic rpms as they have saved me hours of work, and have been more stable then remi and newer than centos or epel. If you find his packages useful I encourage you to click his donate button on the webtatic page to support his efforts.