Compile PHP 5.6 with pthreads and ZTS Ubuntu/Debian

Compile PHP with pthreads and ZTS on Ubuntu or Debian

…and all most common extensions including multi-threading, PDO and mongo extension.

Compile PHP with pthreads

1. Setup your installation path first

In the first step, we download the latest PHP sourcecode and download the sourcecode of pthreads and the mongo extension. I know it can be a struggle to compile, but most of it happens automatigally ūüėČ

Download php into a new directory in /usr/local/src and the two modules into the php-x.x.x/ext folder:

We use git to fetch the latest pthreads from the GitHub repository:

2. Configure the build and install missing includes

When you want to compile PHP with pthreads and other modules, like curl, mongo, mcrypt,.. you have to download the libraries and their header include files. Using apt-get, this is a rather easy task:

Or if you prefere openssl over gnutls:

One simple thing to focus on

The header files are in the packages ending with -dev. Most libs do start with libxxxxx- or simply by their common name. You can search for packages using apt-cache :

It isn’t a bad idea installing the php5-curl or php5-mcrypt¬†extensions, too, because they depend on the libs which we will need to compile PHP. But we are not going to use the php5-xxxx packages, because we build them ourselves to be compatible with our new PHP API version. Don’t worry!

Here is a list of packages I had to install:

If you have errors with the mongo part, you can skip them or install the client using apt as described on their website.

3. Build and compile PHP with pthreads and ZTS enabled

Make sure you are in the source directory and clean it up:

In the next step, we will configure our build. This is not going to work mostly on the first try.

When you get errors of missing files or unknown extension, leave me a comment and I’ll try to find it.
Make sure the paths in the ./configure command are correct, like --with-libdir= .
Most of them can be found using apt-cache search .

Prefix is our “install dir”

No previous PHP setup will be changed, we only write to the directory we specify in --prefix=/usr/local/src/php-5.6.12 .

When it did finish with success, we can compile php:

4. Build and compile the mongo extension

There are two scripts which must be executable, head into the mongo extension dir, phpize it, configure, and make && make install:

5. Use the default php.ini

Fairly easy, we just copy the default ini:

Open the file and edit the extension_dir to¬†/usr/local/src/php-5.6.12/lib/php/extensions/debug-zts-20131226/, where you have to check which number is in your folder name, but it has to be starting with “debug-zts-“.

Finally add mongo: extension=””.

6. Verify everything worked

Let’s create a symlink to /usr/bin so we can access our new php-cli from everywhere:

10 thoughts on “Compile PHP 5.6 with pthreads and ZTS Ubuntu/Debian

  1. PHP Warning: PHP Startup: Unable to load dynamic library ‘/usr/local/src/php-5.6.12/lib/php/extensions/debug-zts-20121212/’ – /usr/local/src/php-5.6.12/lib/php/extensions/debug-zts-20121212/ cannot open shared object file: No such file or directory in Unknown on line 0

  2. Hi,
    It’s wonderful article on configuring pthread with php. I have follow all the steps. However I have stuck with notice while compiling new php version 5.3.29. It said following
    Notice: Following unknown configure options were used:
    Check './configure --help' for available options

    Do you have any idea what will be the possible reason for this ?

  3. I’ve tried to call a new thread from running thread, it had run but not parallel. Does it should work?
    php 5.6.12

  4. I have a problem installing it on Ubuntu 14.04. When I try to compile with “make” it says: fatal error: Zend/zend_smart_str.h: No such file or directory #include Is there any solution for this, because I can’t find one on the internet

  5. hi
    in the make i’ve got error

    Makefile:916: recipe for target ‘ext/sockets/conversions.lo’ failed
    make: *** [ext/sockets/conversions.lo] Error 1

    root@raspberrypi:~# uname -a
    Linux raspberrypi 3.18.11+ #781 PREEMPT Tue Apr 21 18:02:18 BST 2015 armv6l GNU/Linux

    • Hi Victor,
      did you use the latest PHP source 5.6.12 or 5.6.9 ?
      I spotted an error in the ./configure command, where I have different versions, make sure to use the same everywhere.
      I am not sure if it works 100% on arm (RaspberryPi) tho.

      • i’ve removed –enable-sockets from configure and php 5.6.9 successfully installed on the raspberry with pthreads support
        thanks for this article

    • Ok, solved the problem myself. Doing git clone downloaded the newest version of pthreads for PHP 7. If anyone is intrested here are the commands I used to install it:

      cd /usr/local/src

      wget -O php.tar.gz

      tar xzvf php.tar.gz

      apt-get install libmemcached-dev libmcrypt-dev libcurl4-openssl-dev libgd2-xpm-dev \
      libmysqlclient-dev zlib1g-dev libssl1.0.0-dbg \
      libssl-dev libssl-dev libsslcommon2-dev libgd2-xpm-dev

      cd /usr/local/src/php-5.6.12

      apt-get install libcurl4-openssl-dev
      apt-get install autoconf

      ./buildconf –force

      ./configure \
      –prefix=/usr/local/src/php-5.6.12 \
      –with-libdir=/lib/x86_64-linux-gnu/ \
      –with-openssl=/usr –with-curl=/usr \
      –with-apxs2=/usr/local/apache2/bin/apxs \
      –disable-cgi \
      –with-config-file-path=:/usr/local/src/php-5.6.12/etc \
      –enable-gd-native-ttf –enable-mysqlnd \
      –enable-opcache –enable-pcntl \
      –enable-debug –enable-maintainer-zts \
      –enable-mbstring \
      –enable-bcmath –enable-exif \
      –enable-ftp –enable-shmop \
      –enable-soap –enable-sockets \
      –enable-sysvmsg –enable-sysvsem \
      –enable-sysvshm –enable-wddx \
      –enable-opcache –enable-zip –enable-dba

      make install

      cd /usr/local/src/php-5.6.12
      mkdir etc
      cp php.ini-production etc/php.ini

      ln -s /usr/local/src/php-5.6.12/sapi/cli/php /usr/bin/php

      php go-pear.phar
      ln -s /usr/local/src/php-5.6.12/bin/pear /usr/bin/pecl
      pecl install
      php -m

      • Thank you a ton for sharing, very appreciated!

        Keep in mind, the master branch of pthreads is for PHP 5.*, the branch “seven” is for PHP 7.*.
        It’s a bit misleading how he setup the branches.

Leave a Reply

Your email address will not be published. Required fields are marked *