Installing PHP 7.2 with pthreads on CentOS 6
This week I finally took the plunge and upgraded most of the servers I work with from PHP 7.1 to PHP 7.2. (Some of them still can't be brought forward due to dependencies that use mcrypt.) I ran into a few snags with the upgrade to 7.2 and thought I'd document them here. This may be useful if you:
- Run CentOS Linux 6.x (6.9 in my case),
- Want to install PHP 7.2,
- Need pthreads support in PHP, and
- Prefer to compile PHP from source
In order to build PHP 7.2 with pthreads on CentOS 6, you must upgrade to autoconf 2.64, which can't presently be done through the yum package manager. You'll also have to first build PHP 7.2 without pthreads, then use this "bootstrap" copy to prepare the pthreads extension before compiling PHP a second time.
Start by installing autoconf 2.64. The latest version available through yum is 2.63, which isn't sufficient to build PHP's configure script.
It's critical that you supply the
--prefix=/usr option when configuring autoconf. The default installation prefix for the generic autoconf distribution is
/usr/local, but CentOS 6 keeps it in
/usr. If you forget to set the correct prefix, you're going to wind up with two different versions of autoconf installed, and who wants that?
curl -Lo autoconf-2.64.tar.gz ftp://ftp.gnu.org/pub/gnu/autoconf/autoconf-2.64.tar.gz tar xfz autoconf-2.64.tar.gz cd autoconf-2.64 ./configure --prefix=/usr make && make install
Make sure the correct version has been properly installed.
autoconf --version should show:
autoconf (GNU Autoconf) 2.64 Copyright (C) 2009 Free Software Foundation, Inc. [..snip..]
Bootstrap PHP 7.2
Compiling PHP 7.2 with pthreads presents a bit of a Catch-22: in order to build the pthreads extension, you need to have the 7.2 version of PHP (more accurately, a version with ZEND_MODULE_API_NO >= 20170718) and its libraries already installed. To work around this scenario, it's necessary to compile and install a minimal version of the PHP 7.2 CLI first, then use this "bootstrap" version to prepare pthreads.
Start by obtaining the latest PHP 7.2 source distribution and doing a minimal build. The only option you need to pass to configure this time around is
--enable-maintainer-zts, to turn on thread safety. Don't worry about other modules (mysqli, PDO, etc.) as you're not going to be keeping this version of PHP installed for long. If you also run PHP as an Apache module, disregard that for now and just build the CLI version.
curl -Lo php-7.2.1.tar.gz http://us3.php.net/get/php-7.2.1.tar.gz/from/this/mirror tar xfz php-7.2.1.tar.gz cd php-7.2.1 ./configure --enable-maintainer-zts make make install
Verify that a thread-safe version of PHP 7.2 has been installed. The output of
php -v should indicate a 7.2 version number and the
( ZTS ) signature.
PHP 7.2.1 (cli) (built: Jan 8 2018 17:14:36) ( ZTS ) [..snip..]
Obtain and prepare the pthreads extension. Note there's a dot
. as the final argument to
git clone. If you don't use git, you can manually download a .zip file of the pthreads source.
cd ext #this should put you in the php-7.2.1/ext directory mkdir pthreads && cd pthreads git clone https://github.com/krakjoe/pthreads.git . phpize && ./configure && make install
If you receive error output that includes the following,
php_pthreads.c:38:3: error: #error "pthreads requires PHP 7.2, ZTS in versions 7.0 and 7.1 is broken"
...this means that your bootstrap PHP 7.2 didn't install properly, and you have an older PHP command line binary on your system. You should repeat the bootstrapping step above.
Build your final PHP 7.2
Now it's time to build PHP 7.2 for real. Return to the main PHP source tree, clean up the artifacts from the bootstrap build, and generate a new configure script. The
./buildconf --force command creates a new configure script that tells PHP how to bundle the pthreads extension.
cd ../.. #this should put you in the php-7.2.1 directory make clean rm -f configure && ./buildconf --force
If you receive the following error, something didn't work out with your autoconf 2.64 installation and you should scroll up and do that part again.
Forcing buildconf Removing configure caches buildconf: checking installation... buildconf: autoconf version 2.63 found. You need autoconf version 2.64 or newer installed to build PHP from Git. make: *** [buildmk.stamp] Error 1
At this point you should configure PHP as you normally would, enabling whatever options and extensions you require, including the Apache build option if that applies. Since pthreads was the main point of this exercise, don't forget to turn it on by passing the
--enable-maintainer-zts options as well. Here's an example from one of my servers:
./configure --with-apxs2=/usr/local/apache2/bin/apxs --with-openssl --with-zlib \ --with-curl --with-gd --with-snmp --with-pdo-mysql --with-ldap --enable-pcntl \ --enable-pthreads=shared --enable-maintainer-zts --with-config-file-path=/usr/local/etc make make install
Near the end of the output from
make install, look for a line like this:
Installing shared extensions: /usr/local/lib/php/extensions/no-debug-zts-20170718/
You'll need this path in the next step.
Get php-cli.ini in order
Finally, it's time to activate pthreads so you can use it. This is done by editing PHP's configuration file.
Because the pthreads extension only works with the command line version of PHP, you only want to enable it when the CLI SAPI is active. Conveniently, PHP allows you to create a separate
php-cli.ini file for the command line binary. This prevents other SAPIs, such as the Apache module, from trying to load pthreads and causing problems.
Find the php.ini file that PHP is using on your system. Running
php --ini will ouput a line indicating the currently loaded configuration file. If this file is named simply
php.ini, make a copy of it called
php-cli.ini in the same directory as the original. Then run
php --ini again and verify that the "Loaded Configuration File" points to
php-cli.ini in your editor and add the following line to activate the pthreads extension. The path to pthreads.so will be the directory you noted from the previous step. You can place this line anywhere in the file, but the section labeled "Dynamic Extensions" is a reasonable spot for it.
php -m to verify that pthreads appears in the list of loaded modules, and you're all set.