Learn about Centmin Mod LEMP Stack today
Register Now

PHP-FPM Insight Guide How to install pthreads PECL Extension for PHP CLI Only ?

Discussion in 'Centmin Mod Insights' started by eva2000, Sep 20, 2018.

Thread Status:
Not open for further replies.
  1. eva2000

    eva2000 Administrator Staff Member

    37,214
    8,128
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +12,514
    Local Time:
    4:19 PM
    Nginx 1.15.x
    MariaDB 5.5/10.x
    This pthreads PECL extension install guide is for PHP CLI only for PHP 7.2+ and is provided as is without any support. Centmin Mod hasn't been tested with pthreads PECL extension at all, so below guide is just for install of pthreads for PHP CLI only for PHP 7.2. As such you need to be on Centmin Mod 123.09beta01 or newer for PHP 7.2 and for the below steps to work.

    Note PHP-FPM with ZTS enabled may reduce performance of PHP.

    Step 1. Recompile PHP 7.2+ latest with Zend Thread Safety



    Recompile PHP 7.2 latest with ZTS (Zend Thread Safety) support PHP: pthreads - Manual. So set in persistent config file /etc/centminmod/custom_config.inc the following variable which extends PHP configuration options to add --enable-maintainer-zts flag to compile PHP 7.2+ with ZTS support.

    Add to /etc/centminmod/custom_config.inc
    Code (Text):
    PHP_EXTRAOPTS=" --with-xsl --enable-maintainer-zts"
    

    Then run centmin.sh menu option 5 and recompile latest PHP 7.2 version from php.net so right now PHP 7.2.10 is latest

    Code (Text):
    --------------------------------------------------------
         Centmin Mod Menu 123.09beta01 centminmod.com
    --------------------------------------------------------
    1).  Centmin Install
    2).  Add Nginx vhost domain
    3).  NSD setup domain name DNS
    4).  Nginx Upgrade / Downgrade
    5).  PHP Upgrade / Downgrade
    6).  XCache Re-install
    7).  APC Cache Re-install
    8).  XCache Install
    9).  APC Cache Install
    10). Memcached Server Re-install
    11). MariaDB MySQL Upgrade & Management
    12). Zend OpCache Install/Re-install
    13). Install/Reinstall Redis PHP Extension
    14). SELinux disable
    15). Install/Reinstall ImagicK PHP Extension
    16). Change SSHD Port Number
    17). Multi-thread compression: pigz,pbzip2,lbzip2...
    18). Suhosin PHP Extension install
    19). Install FFMPEG and FFMPEG PHP Extension
    20). NSD Install/Re-Install
    21). Update - Nginx + PHP-FPM + Siege
    22). Add Wordpress Nginx vhost + Cache Plugin
    23). Update Centmin Mod Code Base
    24). Exit
    --------------------------------------------------------
    Enter option [ 1 - 24 ] 5
    --------------------------------------------------------
    

    Code (Text):
    Do you want to run YUM install checks ?  [y/n]
    
    This will increase your upgrade duration time wise.
    Check the change log centminmod.com/changelog.html
    to see if any Nginx or PHP related new additions
    which require checking YUM prequisites are met.
    If no new additions made, you can skip the
    YUM install check to speed up upgrade time.
    
     [y/n]: n
    

    Code (Text):
    PHP Upgrade/Downgrade - Would you like to continue? [y/n] y
    
    ----------------------------------------------------------------
    Install which version of PHP? (version i.e. 5.6.38, 7.0.30, NGDEBUG)
    PHP 7.x/7.1.x/7.2.x is GA Stable but still may have broken PHP extensions.
    NGDEBUG is PHP 7.3.0 dev builds minus incompatible PHP extensions
    ----------------------------------------------------------------
    Enter PHP Version number you want to upgrade/downgrade to: 7.2.10
    ----------------------------------------------------------------
    existing php.ini will be backed up at /usr/local/lib/php.ini-oldversion_200918-004302
    ----------------------------------------------------------------
    
    -----------------------------------------------------------------------------------------
    Detected PHP 7.2 branch.
    You can compile Zend OPcache (Zend Optimizer Plus+) support
    as an alternative to using APC Cache or Xcache cache.
    But Zend OPcache only provides PHP opcode cache and
    DOESN'T do data caching, so if your web apps such as Wordpress,
    Drupal or vBulletin require data caching to APC or Xcache,
    it won't work with Zend OPcache.
    
    -----------------------------------------------------------------------------------------
    Do you want to use Zend OPcache [y/n] ? y
    
    *************************************************
    * Zend Optimizer Plus OPcache configured
    *************************************************
    

    Code (Text):
    PHP 7+ detected which uses newer mysqlnd
    or PDO MySQL extensions and removed the
    legacy mysql extension. You can optionally
    re-add the removed legacy mysql extension
    to PHP 7+ by answering yes to next question
    Only answer yes if you know for sure you
    have very old web scripts which need mysql
    legacy extension re-added. Otherwise answer
    no which is recommended for best stability
    
    Re-add legacy mysql extension to PHP 7+ [y/n] ? n
    


    Step 2. Install pthreads PHP extension



    Install pthreads PHP extension from master branch source code at
    krakjoe/pthreads

    Code (Text):
    cd /svr-setup
    git clone https://github.com/krakjoe/pthreads
    cd pthreads
    phpize
    ./configure
    make -j$(nproc)
    make install
    


    Step 3. Load pthread extension only for PHP CLI



    Use separate .ini settings file at /usr/local/lib/php-cli.ini for PHP CLI line by making copy of /usr/local/lib/php.ini
    Code (Text):
    cp -a /usr/local/lib/php.ini /usr/local/lib/php-cli.ini
    

    add to /usr/local/lib/php-cli.ini
    Code (Text):
    extension=pthreads.so
    

    Can do this via command to append to php-cli.ini
    Code (Text):
    echo "extension=pthreads.so" >> /usr/local/lib/php-cli.ini
    


    Step 4. Recompile ZTS Safe PHP extensions



    Recompile alot of PHP extensions for PHP ZTS enabled version of PHP-FPM so run centmin.sh menu option 10, option 13 submenu option 2, and option 15 for memcache/memcached, redis and imagick PHP extensions respectively.

    Step 5. Verify



    Restart php-fpm and verify PHP CLI has loaded module
    Code (Text):
    fpmrestart
    

    check PHP version reports as ZTS instead of NTS
    Code (Text):
    php -v
    PHP 7.2.10 (cli) (built: Sep 20 2018 00:45:30) ( ZTS )
    Copyright (c) 1997-2018 The PHP Group
    Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
        with Zend OPcache v7.2.10, Copyright (c) 1999-2018, by Zend Technologies
    

    look for pthreads loaded module for PHP CLI
    Code (Text):
    php -m
    

    verify
    Code (Text):
    php --ri pthreads
    

    example output
    Code (Text):
    php --ri pthreads
    
    pthreads
    
    Version => 3.1.7dev
    

    Code (Text):
    php -m
    [PHP Modules]
    bcmath
    bz2
    calendar
    Core
    ctype
    curl
    date
    dom
    enchant
    exif
    fileinfo
    filter
    ftp
    gd
    geoip
    gettext
    gmp
    hash
    iconv
    igbinary
    imagick
    imap
    intl
    json
    ldap
    libxml
    mbstring
    mcrypt
    memcache
    memcached
    mysqli
    mysqlnd
    openssl
    pcntl
    pcre
    PDO
    pdo_mysql
    pdo_sqlite
    Phar
    posix
    pspell
    pthreads
    readline
    redis
    Reflection
    session
    shmop
    SimpleXML
    snmp
    soap
    sockets
    SPL
    sqlite3
    standard
    sysvmsg
    sysvsem
    sysvshm
    tidy
    tokenizer
    wddx
    xml
    xmlreader
    xmlrpc
    xmlwriter
    xsl
    Zend OPcache
    zip
    zlib
    
    [Zend Modules]
    Zend OPcache
    

    The verify PHP non-CLI version doesn't have pthreads loaded by creating phpinfo file /usr/local/nginx/html/phpw.php and inspecting it within your web browser at http://YOUR_SERVER_IP_ADDR/phpw.php and then deleting the file at /usr/local/nginx/html/phpw.php
    Code (Text):
    echo '<?php phpinfo();' > /usr/local/nginx/html/phpw.php
    

    Or inspect via command like curl and grep filter for references to pthreads
    Code (Text):
    curl -s localhost/phpw.php | grep pthreads
    

    should return empty line if pthreads isn't found which is the correct expectation as you only want pthreads enabled for PHP CLI only for PHP 7.2+

    Further checks to see if PHP CLI is ZTS enabled and pthreads installed, these 2 commands should return a value of 1
    Code (Text):
    php -r "echo PHP_ZTS;"
    php -r "print_r(class_exists('Thread'));"
    

    Code (Text):
    php -r "echo PHP_ZTS;"
    1
    

    Code (Text):
    php -r "print_r(class_exists('Thread'));"
    1
    

     
    Last edited: Sep 20, 2018
  2. eva2000

    eva2000 Administrator Staff Member

    37,214
    8,128
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +12,514
    Local Time:
    4:19 PM
    Nginx 1.15.x
    MariaDB 5.5/10.x

    Revert Back To Non Thread Safe PHP-FPM Defaults



    To revert back to Centmin Mod default Non Thread Safe (NTS) PHP-FPM, just remove the line --enable-maintainer-zts from variable you added in persistent config file /etc/centminmod/custom_config.inc
    Code (Text):
    PHP_EXTRAOPTS=" --with-xsl"
    

    And re-run centmin.sh menu option 5 to recompile PHP 7.2.10 and remove extension=pthreads.so from /usr/local/lib/php-cli.ini which you created via step 3 above or remove /usr/local/lib/php-cli.ini entirely and restart PHP-FPM service
     
..
Thread Status:
Not open for further replies.