Aug 122013
 

in an attempt to try out pthreads, i have gone on an adventure – compiling php by hand! i wanted to share my experience with the internet, as i had some trouble finding a good guide.

i opted to use the latest stable release of php 5.5 – but you can use most any version you want. it’s worth it to note that i am using centos 6.4

first thing’s first: let’s clean up any packages that would interfere, and install epel. we will need epel for a few extensions.

#remove old php packages
yum erase php*
#install epel - note, this link may be outdated! 
# see here for the latest: http://goo.gl/SAN1Qc
rpm -Uvh http://mirrors.servercentral.net/fedora/epel/6/i386/epel-release-6-8.noarch.rpm

now, we need to get the php source – i like to unpack it into /usr/src in case i need it in the future.

#download the php source - i used php 5.5.1
cd /usr/src
wget -Ophp-5.5.1.tar.bz2 "http://us3.php.net/get/php-5.5.1.tar.bz2/from/this/mirror"
#unpack the source
tar jxf php-5.5.1.tar.bz2
#move in to the source directory
cd php-5.5.1

now, this is the tricky part – usually, compiling is pretty simple. just ./configure, then make && make install. however, with php, there are many configure options you need to know about – and many of them are either undocumented, or hard to tack down. for me, i was mostly interested options required for pthreads, and some basic functionality i needed for my phpircd project. these are:

–prefix=/usr: use /usr as the prefix for installation – this will place the php executable in /usr/bin
–enable-maintainer-zts: enable zend thread safe – required for pthreads
–with-pear: build in pear (so we can install pthreads, and any other pear/pecl extension we need)
–with-config-file-path=/etc: this will place php.ini in /etc
–with-readline: this will enable readline, making the php interactive console much better
–with-mcrypt: builds in mcrypt hashing functions
–with-zlib: builds in zlib compression libraries
–enable-mbstring: builds in multi-byte string support
–with-curl: builds in curl functions
–with-bz2: builds in bzip2 support
–enable-zip: builds in zip support
–enable-sockets: builds in the socket extension
–enable-sysvsem: enables system v messages support
–enable-sysvshm: enables system v shared memory support
–with-mhash: builds in mhash hashing
–with-pcre-regex: builds in pcre regex support
–with-gettext: builds in gettext support
–enable-bcmath: enables the bcmath extension
–enable-libxml: enables in the xml extension
–enable-json: enables in the json extension
–with-openssl: builds in ssl support
–enable-pcntl: enables the pcntl extension

you will need some base build tools, and devel packages to use the above configure options. let’s install those:

yum install gcc make install zlib-devel readline-devel libxml12-devel \
openssl-devel libmcrypt-devel curl-devel bzip2-devel xml2-config 

now, configure and build php:

#configure
./configure --prefix=/usr --enable-maintainer-zts --with-pear \
--with-config-file-path=/etc --with-readline --with-mcrypt \
--with-zlib --enable-mbstring --with-curl --with-bz2 \
--enable-zip --enable-sockets --enable-sysvsem --enable-sysvshm \
--with-mhash --with-pcre-regex --with-gettext --enable-bcmath \
--enable-libxml --enable-json --with-openssl --enable-pcntl

#build!
make

#install!
make install

now, lets install pthreads using pecl:

pecl install channel://pecl.php.net/pthreads-0.0.44

after that, you’re good to go! use pthreads and your fancy new compiled php 5.5 :)

# php -a
Interactive shell

php > class MyThread extends Thread {
php {   function run(){
php {   }
php { }
php > $thread = new MyThread;
php > $thread->start();
php >