How to re-install PHP7 with ZTS enabled on Ubuntu 16.04?

Posted February 5, 2017 13.6k views
PHPUbuntu 16.04

I’ve installed the LAMP distribution on Ubuntu 16.04, and now I want to use threads in PHP7, but it wasn’t initially installed with ZTS enabled so now I need to reinstall it. Just wondering if there’s a guide to making this happen.

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
3 answers

Man this seems like a huge amount of work for something so simple. Would you be able to recommend any other language that I could use for scripting with similar functionality to PHP but with threads already included?


This can be done and I’m running a test on my mini-guide before posting them so I can troubleshoot before handing them over.

Upfront, this will require rebuilding PHP from source and there are a few warnings that come with it.


Building from source is resource intensive and may fail on 512MB Droplets. It also takes a long time (20-30 minutes up to 1+ hours depending on resources), so be patient and don’t close out your connection.

I highly recommend building your package on a fresh droplet and transferring the package over to your existing. Why? The first command, build-dep is going to install numerous packages that most standard web servers do not need, thus creating more for you to update and manage.

Create a snapshot of your existing Droplet before you install the created package. You will need to uninstall the current before you will be able to install the new. Additionally, creating a backup before you attempt to install the new package means you have a security blanket that’ll allow you to restore to previous state, prior to the package being installed.

Lastly, please note that by building your own packages, you are now responsible for updating them. This means that where you previously could simply run apt-get -y upgrade and the latest patches and fixes would be installed if available from the maintainer, you will have to go through the process I detail each time you need to rebuild to include what was updated, etc (i.e. security patches, bug fixes, etc).

I’ll follow up shortly.

  • @ryankr1

    Current build is failing on my end. Working to resolve the error, though so far it’s throwing very vague errors, which are causing the build to fail.

    That being said, the option to compile from PHP’s source is always an option as well. It’s a little more detailed and hands-on, but can be done. It just really depends on whether or not you’re comfortable compiling PHP each time a new release/patch/fix is released.

    Beyond that, IIRC and from what I’ve read, the threaded option only works from the CLI, so for threaded PHP scripts to work properly, you’d need to create a script and then run that script from the CLI. This could, of course, be done using system() (PHP’s version) from within a web-based PHP script, but I wanted to point that out in case you were not aware.

    Ultimately we’d be compiling this against the CLI version of PHP that is offered by package maintainers.

    • Was this ever resolved? I’ve tried 3 custom compilations of php and I cannot for the life of me get thread safety enabled.

      apt-get update
      apt-get install -y 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

      git clone –depth=1
      cd php-src/ext
      git clone -b master pthreads
      cd ..
      ./buildconf –force
      CONFIGURESTRING=“–prefix=/etc/php7 –with-bz2 –with-zlib –enable-zip –disable-cgi –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-maintainer-zts –with-tsrm-pthreads –enable-debug –enable-fpm –with-fpm-user=www-data –with-fpm-group=www-data”
      ./configure $CONFIGURE
      make && make install

      Is what I’m using.

      • @mstedman

        Compiling PHP requires quite a bit of trial and error when it comes to installing the packages required by each module/extension.

        Attempting a source build on a stock image (whether Ubuntu or any other distro) will fail as you won’t have the packages required for compiling.

        For example, if we run apt-get build-dep php7.0 on Ubuntu 16.04, the packages that are needed would be:

        apache2-dev aspell aspell-en autoconf automake autotools-dev binutils bison build-essential chrpath comerr-dev cpp cpp-5 debhelper dh-strip-nondeterminism dh-systemd dictionaries-common dpkg-dev emacsen-common firebird-dev firebird2.5-common firebird2.5-common-doc firebird2.5-server-common flex fontconfig-config fonts-dejavu-core freetds-common freetds-dev gcc gcc-5 gettext g++ g++-5 icu-devtools intltool-debian krb5-multidev libaio1 libapparmor-dev libapr1 libapr1-dev libaprutil1 libaprutil1-dev libarchive-zip-perl libasan2 libaspell-dev libaspell15 libatomic1 libbison-dev libbsd-dev libbz2-dev libc-client2007e libc-client2007e-dev libc-dev-bin libc6-dev libcc1-0 libcilkrts5 libcroco3 libct4 libcurl3 libcurl4-openssl-dev libdb-dev libdb5.3-dev libdpkg-perl libedit-dev libenchant-dev libenchant1c2a libevent-core-2.0-5 libevent-dev libevent-extra-2.0-5 libevent-openssl-2.0-5 libevent-pthreads-2.0-5 libexpat1-dev libfbclient2 libfbembed2.5 libfile-stripnondeterminism-perl libfl-dev libfontconfig1 libfontconfig1-dev libfreetype6-dev libgcc-5-dev libgcrypt11-dev libgcrypt20-dev libgd-dev libgd3 libglib2.0-bin libglib2.0-dev libgmp-dev libgmp3-dev libgmpxx4ldbl libgomp1 libgpg-error-dev libgssrpc4 libhunspell-1.3-0 libib-util libice-dev libice6 libicu-dev libisl15 libitm1 libjbig-dev libjbig0 libjpeg-dev libjpeg-turbo8 libjpeg-turbo8-dev libjpeg8 libjpeg8-dev libkadm5clnt-mit9 libkadm5srv-mit9 libkdb5-8 libkrb5-dev libldap2-dev liblsan0 libltdl-dev libltdl7 liblzma-dev libmagic-dev libmcrypt-dev libmcrypt4 libmhash-dev libmhash2 libmpc3 libmpx0 libmysqlclient-dev libmysqlclient20 libodbc1 libonig-dev libonig2 libpam0g-dev libpci-dev libpcre3-dev libpcre16-3 libpcre32-3 libpcrecpp0v5 libpng12-dev libpq-dev libpq5 libpspell-dev libpthread-stubs0-dev libpython-stdlib libpython2.7-minimal libpython2.7-stdlib libqdbm-dev libqdbm14 libquadmath0 librecode-dev librecode0 libsasl2-dev libsctp-dev libsctp1 libsensors4 libsensors4-dev libsm-dev libsm6 libsnmp-base libsnmp-dev libsnmp30 libsqlite3-dev libssl-dev libstdc++-5-dev libsybdb5 libsystemd-dev libtidy-0.99-0 libtidy-dev libtiff5 libtiff5-dev libtiffxx5 libtimedate-perl libtinfo-dev libtool libtsan0 libubsan0 libudev-dev libunistring0 libvpx-dev libvpx3 libwebp-dev libwebp5 libwebpdemux1 libwebpmux1 libwrap0-dev libx11-dev libxau-dev libxcb1-dev libxdmcp-dev libxml2-dev libxmlrpc-epi-dev libxmlrpc-epi0 libxmltok1 libxmltok1-dev libxpm-dev libxpm4 libxslt1-dev libxslt1.1 libxt-dev libxt6 libzip-dev libzip4 linux-libc-dev locales-all m4 make mlock mysql-client-5.7 mysql-client-core-5.7 mysql-common mysql-server mysql-server-5.7 mysql-server-core-5.7 netcat-traditional odbcinst odbcinst1debian2 pkg-config po-debconf python python-minimal python2.7 python2.7-minimal re2c systemtap-sdt-dev unixodbc unixodbc-dev uuid-dev x11-common x11proto-core-dev x11proto-input-dev x11proto-kb-dev xorg-sgml-doctools xtrans-dev zlib1g-dev

        Now, not all of those are physically needed. Some are installed as it’s assumed you’re installing certain modules (i.e the firebird packages aren’t needed if you’re using MySQL or MariaDB, for example), though many of those packages are indeed required to do a source build.

        The same goes for NGINX or any other software in which a source build in being performed.

        The above, however, is a starting point. It’s a lot of work for sure – it can be done, though how you go about compiling PHP is really dependent on what you need specifically. You’ll also have to repeat the process every single time you want to update PHP to another version (bug fixes, new releases, etc).


If you want to run a test build using your configuration, you can install the required packages using:

apt-get -y install autoconf automake bc bison build-essential ccache cmake curl dh-systemd flex gcc geoip-bin google-perftools g++ haveged icu-devtools letsencrypt libacl1-dev libbz2-dev libcap-ng-dev libcap-ng-utils libcurl4-openssl-dev libdmalloc-dev libenchant-dev libevent-dev libexpat1-dev libfontconfig1-dev libfreetype6-dev libgd-dev libgeoip-dev libghc-iconv-dev libgmp-dev libgoogle-perftools-dev libice-dev libice6 libicu-dev libjbig-dev libjpeg-dev libjpeg-turbo8-dev libjpeg8-dev libluajit-5.1-2 libluajit-5.1-common libluajit-5.1-dev liblzma-dev libmhash-dev libmhash2 libmm-dev libncurses5-dev libnspr4-dev libpam0g-dev libpcre3 libpcre3-dev libperl-dev libpng-dev libpng12-dev libpspell-dev libpthread-stubs0-dev libreadline-dev libselinux1-dev libsm-dev libsm6 libssl-dev libtidy-dev libtiff5-dev libtiffxx5 libtool libunbound-dev libvpx-dev libvpx3 libwebp-dev libx11-dev libxau-dev libxcb1-dev libxdmcp-dev libxml2-dev libxpm-dev libxslt1-dev libxt-dev libxt6 make nano perl pkg-config python-dev software-properties-common systemtap-sdt-dev unzip webp wget xtrans-dev zip zlib1g-dev zlibc

I just ran a test using the above and built PHP 7.2.x successfully using the same configure options as you noted in your reply.

Keep in mind, unlike repository packages, some things don’t work as you might expect. You’ll need to set these things up on your own.

For example:

php -v

..won’t work as it would with a repository package. You’ll need to export the path or use the full path when calling PHP from the CLI.

i.e. /etc/php7/bin/php -v

Also, there’s no service/systemd script for PHP-FPM, so you’d need to create that as well, otherwise you won’t be able to use:

service php7 start/stop/restart
systemctl start/stop/restart php7