- Compile PHP Thread Safe And pThreads
- pthreads by krakjoe master branch
- pthreads by krakjoe PHP5 branch
Compiling PHP 5.6.20 with thread safety is surprisingly straight forward. The first link mentioned in the sources actually is a shell script that will execute the process for you on Ubuntu Linux. be warned, there is a bug in that script though. Basically on line 39 of the script, the script clones the master branch of the pthreads project. This branch though is now setup for compatibility with PHP7. In order to compile pthreads with PHP5, you need to checkout the PHP5 branch on the repo before compiling.
We will be going through all of these steps in the tutorial below. This tutorial will walk through basically the above mentioned script as well as give equivalent commands so as to execute the script in Fedora 22 if that is your system instead. Commenting will be added beside any code if there is any difference as to whether to execute it in Ubuntu or Fedora 22. Additionally I am presenting this tutorial as if you were going to run this script by hand in the terminal. So you may find if you are reading along that a few steps appear out of order, this is merely for preference and maybe more intuitive ?
Update The System And Essential Packages
You should always make sure you have the latest packages. run updates for your system with the following commands
sudo apt-get update #Ubuntu sudo dnf update # Fedora 22
Then install some essential tools you will need for downloading and manipulating the build
sudo apt-get install -y build-essential git-core vim curl #Ubuntu sudo dnf install -y git-core vim curl #Fedora 22
Setup Terminal And Directories
For this tutorial I am assuming your executing everything in terminal, and not just running the script in the source links. So we need to set a couple of variables now
sudo mkdir /etc/php5ts PHP_DIRECTORY="/etc/php5ts" PHP_TIMEZONE="UTC"
NOTE: At this point do not close your terminal before finishing this tutorial. The variables that have been set above are only kept as long as the current terminal session remains open. Closing the session will remove the
PHP_TIMEZONE variables. You can test if the variables are set by entering the following commands
echo $PHP_DIRECTORY echo $PHP_TIMEZONE
Build Thread Safe PHP 5.6.20
Download and extract PHP 5.6.20 from here: http://php.net/downloads.php#v5.6.20 and navigate to the extracted root.
Install additional packages with the following commands:
sudo apt-get install -y make autoconf re2c bison #Ubuntu sudo dnf install -y make autoconf re2c bison #Fedora 22 sudo apt-get install -y libicu-dev libmcrypt-dev libssl-dev libcurl4-openssl-dev libbz2-dev libxml2-dev libpng-dev libjpeg-dev libedit-dev #Ubuntu sudo dnf install -y libicu-devel libmcrypt-devel openssl* libcurl libcurl-devel bzip2-devel libxml-devel libpng-devel libjpeg-devel libedit-devel #Fedora 22
Now lets start building PHP! At the PHP folder root enter the following commands
./buildconf --force ./configure --prefix=$PHP_DIRECTORY --with-config-file-path=$PHP_DIRECTORY --with-config-file-scan-dir=$PHP_DIRECTORY/conf.d --disable-all --enable-maintainer-zts --with-curl --with-openssl --with-gd --enable-gd-native-ttf --enable-intl --enable-mbstring --with-mcrypt --with-mysqli=mysqlnd --with-zlib --with-bz2 --enable-exif --with-pdo-mysql=mysqlnd --with-libedit --enable-zip --enable-pdo --enable-pcntl --enable-sockets --enable-mbregex --with-tsrm-pthreads
These commands will do additional checks for any missing packages on your system. If either of these command return errors of missing packages, Install them and then rerun the command.
If both run successfully without errors. Execute this next
This will compile 5.6.20 and could take awhile. After this has completed execute
This will test the compiled php that all functionality expected works. You should only see PASSES and SKIPS while it processes the tests. The test procedure will give you a summary at the end and may prompt you to send results to the php development team. You can say yes or no here. This testing process executes around 10k tests, so aswell, this step will take awhile.
Once complete, run the following command
sudo make install sudo cp php.ini-production /etc/php5ts/php.ini
Great, you now have compiled php5 with threading capabilities. Lets now add the threading!
Download and clone the pthread project from Github. Also we need to checkout the PHP5 branch, since the master branch is being developed for PHP7
git clone https://github.com/krakjoe/pthreads.git git checkout PHP5
Then install php5 dev tools
sudo apt-get install -y php5-dev #Ubuntu sudo dnf install -y php5-devel #Fedora 22
Now lets build the pthread project
phpize ./configure --with-php-config=$PHP_DIRECTORY/bin/php-config
Again, configure will check for any additional dependencies needed that are not on your system. Install them and then rerun the configure step if you are given any errors when executing that command. Then as done when building PHP 5.6.20; compile, test and install pthread with the following commands
make make test make install
This process should not take as long. The test suite only has about 60 tests in comparison. Again you may be prompted to send your results to the php dev team. The choice is up to you.
Complete the installation with the following commands
mkdir $PHP_DIRECTORY/conf.d echo "extension=pthreads.so" > /etc/php5ts/conf.d/pthreads.ini
Congratulations, you have now completed the instalation of the pthreads extension. Lets configure our PHP 5.6.20 we build to use it!
Configure PHP 5.6.20 with pthread
Execute the following commands to update a number of settings in our compiled php to make it aware of the pthread extension
sudo su sed 's#;date.timezone\([[:space:]]*\)=\([[:space:]]*\)*#date.timezone\1=\2\"'"$PHP_TIMEZONE"'\"#g' $PHP_DIRECTORY/php.ini > $PHP_DIRECTORY/php.ini.tmp mv $PHP_DIRECTORY/php.ini.tmp $PHP_DIRECTORY/php.ini sed 's#display_errors = Off#display_errors = On#g' $PHP_DIRECTORY/php.ini > $PHP_DIRECTORY/php.ini.tmp mv $PHP_DIRECTORY/php.ini.tmp $PHP_DIRECTORY/php.ini sed 's#display_startup_errors = Off#display_startup_errors = On#g' $PHP_DIRECTORY/php.ini > $PHP_DIRECTORY/php.ini.tmp mv $PHP_DIRECTORY/php.ini.tmp $PHP_DIRECTORY/php.ini sed 's#error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT#error_reporting = E_ALL#g' $PHP_DIRECTORY/php.ini > $PHP_DIRECTORY/php.ini.tmp mv $PHP_DIRECTORY/php.ini.tmp $PHP_DIRECTORY/php.ini
Execution of the mv steps may cause prompts to confirm whether to overwrite the php.ini file. Say yes to this.
Installation of PHP 5.6.20 has now been completed on your system. To use this thread safe version you will have to call it with
/etc/php5ts/bin/php. Calling just
php in your terminal will execute the dev php we downloaded as part of this compiling process, which is not thread safe. You can make your life easier by creating a symlink to the thread safe php.