PHP multi threads – Setup PHP 7.2cli with Multiple Threads (pthreads) on Ubuntu

In this article I’m going to write steps required to setup PHP multi threads on your server.

Make sure you read this before setting it up so you don’t end up running into same issues like me?

Why PHP multi threads – CLI Version:

Running app on PHP 5.6 for web app but need PHP7.2 (pthreads) with multiple threads to run tasks at background so we can run multiple independent business logics without interfering with main web app.

Install required libraries

sudo apt-get install libc-client-dev libkrb5-dev libgmp-dev apache2-dev libzip-dev bison autoconf build-essential pkg-config git-core libltdl-dev libbz2-dev libxml2-dev libxslt1-dev libssl-dev libicu-dev libpspell-dev libenchant-dev libmcrypt-dev libpng-dev libjpeg8-dev libfreetype6-dev libmysqlclient-dev libreadline-dev libcurl4-openssl-dev
sudo ln -s /usr/include/x86_64-linux-gnu/gmp.h /usr/include/gmp.h

Download & Build PHP

wget https://github.com/php/php-src/archive/php-7.2.19.tar.gz
tar --extract --gzip --file php-7.2.19.tar.gz
cd php-src-php-7.2.19
./buildconf --force
CONFIGURE_STRING="--prefix=/etc/php7 --with-bz2 --with-zlib --enable-zip --disable-cgi \
--with-apxs2=/usr/bin/apxs2 --with-gmp --with-imap \ 
--enable-soap --enable-intl --with-openssl --with-readline --with-curl --enable-ftp \
--enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --enable-sockets \
--enable-pcntl --with-pspell --with-enchant --with-gettext --with-gd --enable-exif \
--with-jpeg-dir --with-png-dir --with-freetype-dir --with-xsl --enable-bcmath \
--enable-mbstring --enable-calendar --enable-simplexml --enable-json --enable-hash \
--enable-session --enable-xml --enable-wddx --enable-opcache --with-pcre-regex \
--with-config-file-path=/etc/php7/cli --with-config-file-scan-dir=/etc/php7/etc \
--enable-cli --enable-debug --enable-fpm --with-fpm-user=www-data --with-fpm-group=www-data \
--with-mcrypt --enable-sysvmsg --enable-sysvsem --enable-sysvshm --enable-shmop \
--enable-pthreads --with-tsrm-pthreads --enable-maintainer-zts"
./configure $CONFIGURE_STRING
make && sudo make install

Check it’s installed (this should print 1)

# check it's installed (this should print 1)
/etc/php7/bin/php -r "echo PHP_ZTS;"

Copy ini file

# copy ini file
mkdir -p /etc/php7/cli/
cp php.ini-production /etc/php7/cli/php.ini

Set permissions

chmod o+x /etc/php7/bin/phpize
chmod o+x /etc/php7/bin/php-config

Compile pthreads now

git clone https://github.com/krakjoe/pthreads.git
cd pthreads
/etc/php7/bin/phpize
./configure --prefix='/etc/php7' --with-libdir='/lib/x86_64-linux-gnu' --enable-pthreads=shared --with-php-config='/etc/php7/bin/php-config'
make && sudo make install

Enable in php.ini

echo "extension=pthreads.so" | sudo tee -a /etc/php7/cli/php.ini
echo "zend_extension=opcache.so" | sudo tee -a /etc/php7/cli/php.ini

Check it’s installed (this should print true)

/etc/php7/bin/php -r "print_r(class_exists('Thread'));"

Install Mcrypt

/etc/php7/bin/pecl install mcrypt
echo "extension=mcrypt.so" | sudo tee -a /etc/php7/cli/php.ini

Check it’s installed (this should print true)

/etc/php7/bin/php -r "var_dump(extension_loaded('mcrypt'));"

Update alternatives or create links

# prefer to create links manually to avoid messing the other php installation
sudo ln -s /etc/php7/bin/php /usr/bin/php7.2
sudo ln -s /etc/php7/bin/pecl /usr/bin/pecl7.2

Read more on https://peham.dev/blog

Leave a Reply