UNIX: Compiling PHP Extensions

This procedure describes how to compile a PHP extension. Zend Server includes over 77 extensions however there still may be a PHP extension that you want to compile by yourself.

Requirements:

Install the following packages:

Users of distributions with package managers (mainly Debian, Ubuntu, RHEL, CentOS and Fedora Core and many others) should install the following packages from their distribution's repository: gcc, make, autoconf, automake and libtool. Some of these tools depend on each other, for instance the libtool package depends on the gcc package, but no damage can be done from specifying all of them.

Note:

Users who utilize distributions that do not have package managers (Linux from scratch anyone?), can compile these tools themselves or obtain pre-compiled binaries for them quite easily.

Additionally, you can compile a PHP extension from the main PHP source (as opposed to PECL). This requires installing a package from the Zend Server repository called php-5.2-source-zend-server or php-5.3-source-zend-server, depending on your Zend Server's major PHP version. This package includes full PHP sources as patched, for security or optimization concerns, by the Zend development team. This ensures that you are using the exact same source code we used when building Zend Server.

Scenario 1: compile a PECL extension called Newt

Newt is a PHP extension for RedHat's Newt (New Terminal) library, a terminal-based window and widget library for writing applications with user friendly interfaces.

Being what it is, this extension requires the existence of the Newt library development files. If you are using Debian or Ubuntu you should install a package called libnewt-dev. On RedHat based distributions the package name is newt-devel. Make sure these are installed before continuing.

NOTE: Other extensions will have other dependencies. For example, the Mcrypt extension will require the Mcrypt development package.

NOTE: Since PECL will attempt to write the extension onto /usr/local/zend/lib/php_extensions, you will have to become a super user to perform this procedure. This is only needed for the actual make install.

 

 

Instructions on how to complete a procedure

To compile your own extension:

  1. Assuming you have the Newt development package installed, run:
    # /usr/local/zend/bin/pecl install newt

The truncated output of this command, along with explanations:

PECL retrieves the package from the repository...*/ downloading newt-1.2.1.tgz

Starting to download newt-1.2.1.tgz (24,853 bytes)

.........done: 24,853 bytes

5 source files, building

/*The phpize script is executed...*/

running: phpize

Configuring for:

PHP Api Version:         20041225

Zend Module Api No:      20060613

Zend Extension Api No:   220060519

building in /var/tmp/pear-build-root/newt-1.2.1

Configure comes into play

running: /tmp/pear/download/newt-1.2.1/configure

checking for grep that handles long lines and -e... /bin/grep checking for egrep... /bin/grep -E checking for a sed that does not truncate output... /bin/sed checking for gcc... gcc checking for C compiler default output file name... a.out checking whether the C compiler works... yes checking whether we are cross compiling... no checking for suffix of executables...

checking for suffix of object files... o

Next comes libtool.

creating libtool

appending configuration tag "CXX" to libtool

configure: creating ./config.status

config.status: creating config.h

The actual compilation process: calls make which internally triggers GCC and LD.

running: make

/bin/sh /var/tmp/pear-build-root/newt-1.2.1/libtool --mode=compile gcc -I. -I/tmp/pear/download/newt-1.2.1 -DPHP_ATOM_INC -I/var/tmp/pear-build-root/newt-1.2.1/include

-I/var/tmp/pear-build-root/newt-1.2.1/main

-I/tmp/pear/download/newt-1.2.1 -I/usr/local/zend/include/php -I/usr/local/zend/include/php/main -I/usr/local/zend/include/php/TSRM

-I/usr/local/zend/include/php/Zend -I/usr/local/zend/include/php/ext -I/usr/local/zend/include/php/ext/date/lib  

-I/usr/local/zend/include/php -DHAVE_CONFIG_H  -g -O2   -c

/tmp/pear/download/newt-1.2.1/newt.c -o newt.lo mkdir .libs  gcc -I. -I/tmp/pear/download/newt-1.2.1 -DPHP_ATOM_INC -I/var/tmp/pear-build-root/newt-1.2.1/include

-I/var/tmp/pear-build-root/newt-1.2.1/main

-I/tmp/pear/download/newt-1.2.1 -I/usr/local/zend/include/php -I/usr/local/zend/include/php/main -I/usr/local/zend/include/php/TSRM

-I/usr/local/zend/include/php/Zend -I/usr/local/zend/include/php/ext -I/usr/local/zend/include/php/ext/date/lib -I/usr/local/zend/include/php -DHAVE_CONFIG_H -g -O2 -c /tmp/pear/download/newt-1.2.1/newt.c  -fPIC -DPIC -o .libs/newt.o /bin/sh /var/tmp/pear-build-root/newt-1.2.1/libtool --mode=compile gcc -I. -I/tmp/pear/download/newt-1.2.1 -DPHP_ATOM_INC -I/var/tmp/pear-build-root/newt-1.2.1/include

-I/var/tmp/pear-build-root/newt-1.2.1/main

-I/tmp/pear/download/newt-1.2.1 -I/usr/local/zend/include/php -I/usr/local/zend/include/php/main -I/usr/local/zend/include/php/TSRM

-I/usr/local/zend/include/php/Zend -I/usr/local/zend/include/php/ext -I/usr/local/zend/include/php/ext/date/lib  

-I/usr/local/zend/include/php -DHAVE_CONFIG_H  -g -O2   -c

/tmp/pear/download/newt-1.2.1/newt_vcall.c -o newt_vcall.lo  gcc -I. -I/tmp/pear/download/newt-1.2.1 -DPHP_ATOM_INC -I/var/tmp/pear-build-root/newt-1.2.1/include

-I/var/tmp/pear-build-root/newt-1.2.1/main

-I/tmp/pear/download/newt-1.2.1 -I/usr/local/zend/include/php -I/usr/local/zend/include/php/main -I/usr/local/zend/include/php/TSRM

-I/usr/local/zend/include/php/Zend -I/usr/local/zend/include/php/ext -I/usr/local/zend/include/php/ext/date/lib -I/usr/local/zend/include/php -DHAVE_CONFIG_H -g -O2 -c /tmp/pear/download/newt-1.2.1/newt_vcall.c

-fPIC -DPIC -o .libs/newt_vcall.o

/bin/sh /var/tmp/pear-build-root/newt-1.2.1/libtool --mode=link gcc -DPHP_ATOM_INC -I/var/tmp/pear-build-root/newt-1.2.1/include

-I/var/tmp/pear-build-root/newt-1.2.1/main

-I/tmp/pear/download/newt-1.2.1 -I/usr/local/zend/include/php -I/usr/local/zend/include/php/main -I/usr/local/zend/include/php/TSRM

-I/usr/local/zend/include/php/Zend -I/usr/local/zend/include/php/ext -I/usr/local/zend/include/php/ext/date/lib  

-I/usr/local/zend/include/php -DHAVE_CONFIG_H  -g -O2   -o newt.la

-export-dynamic -avoid-version -prefer-pic -module -rpath /var/tmp/pear-build-root/newt-1.2.1/modules  newt.lo newt_vcall.lo -lnewt gcc -shared  .libs/newt.o .libs/newt_vcall.o  -lnewt  -Wl,-soname -Wl,newt.so -o .libs/newt.so creating newt.la (cd .libs && rm -f newt.la && ln -s ../newt.la newt.la) /bin/sh /var/tmp/pear-build-root/newt-1.2.1/libtool --mode=install cp ./newt.la /var/tmp/pear-build-root/newt-1.2.1/modules

cp ./.libs/newt.so /var/tmp/pear-build-root/newt-1.2.1/modules/newt.so

cp ./.libs/newt.lai /var/tmp/pear-build-root/newt-1.2.1/modules/newt.la

PATH="$PATH:/sbin" ldconfig -n /var/tmp/pear-build-root/newt-1.2.1/modules

----------------------------------------------------------------------

Libraries have been installed in:

   /var/tmp/pear-build-root/newt-1.2.1/modules

Build complete.

  1. Run 'make test'.
  2. Use PECL to put the newly built Newt extension into place.
    run: make INSTALL_ROOT="/var/tmp/pear-build-root/install-newt-1.2.1"
  3. instal the shared extensions by running:
    var/tmp/pear-build-root/install-newt-1.2.1//usr/local/zend/lib/php_extensions/

running: find "/var/tmp/pear-build-root/install-newt-1.2.1" | xargs ls -dils

574096   4 drwxr-xr-x 3 root root   4096 Mar 30 20:45

/var/tmp/pear-build-root/install-newt-1.2.1

574119   4 drwxr-xr-x 3 root root   4096 Mar 30 20:45

/var/tmp/pear-build-root/install-newt-1.2.1/usr

574120   4 drwxr-xr-x 3 root root   4096 Mar 30 20:45

/var/tmp/pear-build-root/install-newt-1.2.1/usr/local

574121   4 drwxr-xr-x 3 root root   4096 Mar 30 20:45

/var/tmp/pear-build-root/install-newt-1.2.1/usr/local/zend

574122   4 drwxr-xr-x 3 root root   4096 Mar 30 20:45

/var/tmp/pear-build-root/install-newt-1.2.1/usr/local/zend/lib

574123   4 drwxr-xr-x 2 root root   4096 Mar 30 20:45

/var/tmp/pear-build-root/install-newt-1.2.1/usr/local/zend/lib/php_extensions

574118 244 -rwxr-xr-x 1 root root 241717 Mar 30 20:45 /var/tmp/pear-build-root/install-newt-1.2.1/usr/local/zend/lib/php_extensions/newt.so

Build process completed successfully

Installing '/usr/local/zend/lib/php_extensions/newt.so'

install ok: channel://pear.php.net/newt-1.2.1

  1. The Extension has been successfully compiled using PECL.
  2. To load the extension, in the php.ini or in a separate file under the scan dir insert extension=<my_extension_name>.so and replace <my_extension_name> with your extension's binary name such as "extension=newt.so".
  3. If you're using the DEB and RPM versions of Zend Server, the best practice is to place a file called newt.ini under /usr/local/zend/etc/conf.d.
  4. Restart your webserver.

Ensure the extension is properly loaded by checking the output of PHP Info. This can be viewed in the Zend Server PHP Info page.

The extension will now appear in your Administration Interface under Server Setup | Extensions from which you can also load and unload the extension (for more information see: Working with Extensions).

Scenario 2: Compile a PHP extension included in the main PHP source called PSpell

Pspell (Portable Spell Checker Interface Library) provides a generic interface to the system spelling checking libraries. To compile PSpell first install the php-source-zend-[ce|pe] package for this procedure. Also, since this extension relies on the portable spell-checking interface (pspell) library, you will need to install its devel package. Debian and Ubuntu users should install the libpspell-dev package, on RedHat based distributions, the package name is aspell-devel.

 

 

Instructions on how to complete a procedure

To compile your own extension:

  1. CD the extension's source directory(in our example, the PHP version is 5.2.9 as it is the current stable version Zend Server is shipped with):
    $ cd /usr/local/zend/share/php-source/php-5.2.9/ext/pspell
  2. Run phpize:
    $ /usr/local/zend/bin/phpize

The output should be similar to this:

/Configuring for:

PHP Api Version:         20041225

Zend Module Api No:      20060613

Zend Extension Api No:   220060519/

  1. Run the configure script, generated by phpize:
    $ ./configure --with-php-config=/usr/local/zend/bin/php-config
  2. Run make:
    $ make
  3. Become a super user [root] and run:
    # make install

The output should be:

/Installing shared extensions:     /usr/local/zend/lib/php_extensions/

  1. Insert the "extension=pspell.so" directive either in php.ini or in a separate file under the scan dir.
  2. Restart your webserver.

Ensure the extension is properly loaded by checking the output of PHP Info. This can be viewed in the Zend Server PHP Info page.

The extension will now appear in your Administration Interface under Server Setup | Extensions from which you can also load and unload the extension (for more information see: Working with Extensions).

 

Troubleshooting:

The configure script outputs messages as it goes along and many times you will be able to understand the problem just by looking at it, however, sometimes, the error doesn't necessarily reflect the real issue so it is always a good idea to review the config.log. This is a very generic statement but no other statement can be made as there are many different extensions and issues one may come across so attempting to list them all will be somewhat futile.

 

 

Related Links

Related Links:

PHP Extension List
Adding Extensions