Skip to content

Instantly share code, notes, and snippets.

@asvignesh
Last active June 9, 2023 17:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save asvignesh/f206717e8626bb9e4903e6b7136141c7 to your computer and use it in GitHub Desktop.
Save asvignesh/f206717e8626bb9e4903e6b7136141c7 to your computer and use it in GitHub Desktop.
Install PHP ZTS and enable the pThread

Install packages needed for compiling

sudo apt-get update

sudo apt install autoconf automake bison build-essential curl flex \
  libtool libssl-dev libcurl4-openssl-dev libxml2-dev libreadline7 \
  libreadline-dev libsqlite3-dev libzip-dev libzip4 nginx openssl \
  pkg-config re2c sqlite3 zlib1g-dev
 

Create A dir for cloning the src

mkdir -p ~/bin/php7-latest/

Clone php repo

mkdir ~/apps && cd ~/apps && git clone https://github.com/php/php-src.git

cd php-src

git fetch origin && git checkout -b PHP-7.3.11 origin/PHP-7.3.11

Build the configuration

./buildconf --force

configure

./configure --prefix=$HOME/bin/php-latest \
--with-zlib \
--enable-zip \
--with-openssl \
--with-readline \
--with-curl \
--with-pear \
--enable-ftp \
--enable-mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql \
--with-libzip=/usr/lib/x86_64-linux-gnu \
--enable-sockets \
--enable-sysvmsg \
--enable-sysvsem \
--enable-sysvshm \
--enable-pcntl \
--enable-bcmath \
--enable-mbstring \
--enable-phpdbg \
--enable-shmop \
--enable-simplexml \
--enable-json \
--enable-hash \
--enable-session \
--enable-xml \
--enable-wddx \
--enable-opcache \
--with-pcre-regex \
--with-config-file-path=/$HOME/bin/php-latest/cli \
--with-config-file-scan-dir=$HOME/bin/php-latest/etc \
--enable-cli \
--enable-maintainer-zts \
--with-tsrm-pthreads \
--enable-debug \
--enable-fpm \
--with-fpm-user=www-data \
--with-fpm-group=www-data

make
make install

Copy the php.ini file to the install directory

cp php.ini-development ~/bin/php-latest/lib/

Rename two files.

cd ~/bin/php-latest/etc/  
mv php-fpm.conf.default php-fpm.conf
mv php-fpm.d/www.conf.default php-fpm.d/www.conf

Create symbolic links for your for your binary file.

cd ~/bin
ln -s php-latest/bin/php php
ln -s php-latest/bin/php-cgi php-cgi
ln -s php-latest/bin/php-config php-config
ln -s php-latest/bin/phpize phpize
ln -s php-latest/bin/phar.phar phar
ln -s php-latest/bin/pear pear
ln -s php-latest/bin/phpdbg phpdbg
ln -s php-latest/sbin/php-fpm php-fpm

Link your local php to the php command. You will need to logout then log back in for php to switch to the local version instead of the installed version from the default Ubuntu repositories.

# add this to .bashrc or .profile if don't have it
if [ -d "$HOME/bin" ] ; then
  PATH="$HOME/bin:$PATH"
fi

Test our compilation has ZTS enabled

php -r "echo PHP_ZTS . \"\n\";"

expected output: 1

Compile pthreads

We are going to manually compile and add pthreads to PHP, so make the phpize and php-config helper programs executable

sudo chmod o+x ~/bin/php-latest/bin/phpize
sudo chmod o+x ~/bin/php-latest/bin/php-config

Let's clone pthreads project, I will do it in ~/apps

cd ~/apps
git clone https://github.com/krakjoe/pthreads.git
cd pthreads

Run phpize on pthreads.

~/bin/phpize

Set configuration options for pthreads. --enable-pthreads=shared is the main aspect of the configuration

./configure \
--prefix=$HOME/bin/php-latest \
--with-libdir='/lib/x86_64-linux-gnu' \
--enable-pthreads=shared \
--with-php-config=$HOME/bin/php-config

Build and install the extension

make
make test
make install

expected output example

Installing shared extensions:     /home/ubuntu/bin/php-latest/lib/php/extensions/debug-zts-20180731/

Add the pthreads extension to the ini file.

echo "extension=pthreads.so" | tee -a $HOME/bin/php-latest/lib/php.ini-development
echo "zend_extension=opcache.so" | tee -a $HOME/bin/php-latest/lib/php.ini-development

Testing

php -r "echo var_dump(class_exists('Thread'));"

expected output

bool(true)

if failed, check the troubleshooting section bellow.

then I created this file in ~/apps/pthreads_test.php

<?php

class Task extends Threaded
{
    private $value;

    public function __construct(int $i)
    {
        $this->value = $i;
    }

    public function run()
    {
        $s=0;

        for ($i=0; $i<10000; $i++)
        {
            $s++;
        }

        echo "Task: {$this->value}\n";
    }
}

# Create a pool of 4 threads
$pool = new Pool(4);

for ($i = 0; $i < 15000; ++$i) 
{
    $pool->submit(new Task($i));
}

while ($pool->collect());

$pool->shutdown();

run it with php ~/apps/pthreads_test.php

Troubleshooting

I had some issues with my php.ini, it was not using the ~/bin/php-latest/lib/php.ini-development. I run php --ini

Configuration File (php.ini) Path: //home/ubuntu/bin/php-latest/cli
Loaded Configuration File:         (none)
Scan for additional .ini files in: /home/ubuntu/bin/php-latest/etc
Additional .ini files parsed:      (none)

and got that output, so I copied ~/bin/php-latest/lib/php.ini-development to ~/bin/php-latest/etc/php.ini and that solved the problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment