How To Compile And Install PHP Extensions From Source

Mattias Geniar, Sunday, September 14, 2008 - last modified: Saturday, January 24, 2015

It's not as difficult as it might sound, so here's the quick-n-dirty way of installing and compiling your very own PHP extensions/libraries, from the PHP source code. With a normal install, not every library is compiled and installed -- so it might leave you with several functions that aren't working.

This guide is building a module for PHP 5.2, but the steps are identical for PHP 5.3, 5.4, 5.5, or 5.6.

In this case, I'll assume you're trying to compile the dBase-extension. It'll probably start with the following error, shown on screen:

Fatal error: Call to undefined function dbase_create() in ... on line ...

It means the extension wasn't loaded -- and in most cases isn't present on the server at all. So we'll have to create it ourselves.

Install the PHP development package

On Ubuntu/debian, you can use apt-get, it's a piece of cake.

$ apt-get install php5-dev

On CentOS / Red Hat, use yum for your PHP packages.

$ yum install php-devel

(alternatively: check which repositories you're using and which PHP package you have installed, the package may also be named php55-devel, or similar).

Download & unzip the PHP5 source code

Go the the PHP-download page, and select the complete PHP5 source code.

$ cd /usr/local/src
$ mkdir php_source
$ cd php_source
$ wget http://be.php.net/distributions/php-5.2.6.tar.gz

Next, unzip the file you just downloaded (the ".gz"-file extensions means it has been gzipped).

$ gunzip php-5.2.6.tar.gz

And untar it.

$ tar xvf php-5.2.6.tar

Prepare the extension (phpize)

Go to the proper directory. You just untarred the files, so we'll browse to that specific subdirectory (note; directory-names may vary, depending on the version you just downloaded). In this case, we go to the "dbase" subfolder, but it could be any of the extensions you want to configure.

$ cd php-5.2.6/ext/dbase/

And use phpizein that directory.

$ cd /usr/local/src/php_source/php-5.2.6/ext/dbase/
$ phpize

Configuring for:
PHP Api Version:         20041225
Zend Module Api No:      20060613
Zend Extension Api No:   220060519

Configure & Make the extension

Configure it

$ ./configure

And make it

$ make

The make command, will render a lot of text (compilation-info), and most likely end with the following snippet.

...
----------------------------------------------------------------------

Build complete.
(It is safe to ignore warnings about tempnam and tmpnam).

Extension is now created in ./.libs/dbase.so

Move it to the extensions directory

Woohoo, we compiled our dbase-extension! Now all we need to do, is activate it.

Move the extension

If you already have an extensions-directory on your server, use that one -- if not, you can create a new directory to place the extensions. To find out, check the extension_dir directive.

$ php -i | grep extension_dir
extension_dir => /usr/lib/php/extensions/no-debug-non-zts-20121212

Copy the extension to that directory.

$ cp libs/dbase.so /usr/lib/php/extensions/no-debug-non-zts-20121212/

Edit your PHP.INI

The extension is ready to be included -- now we need to change the php.ini file to load that particular extension. First, find out which php.ini your system is using (note: the CLI may load a different config than the php-fpm or the Apache module one, a phpinfo() in your application would tell you for certain).

$ php -i | grep 'Configuration File'
Configuration File (php.ini) Path => /etc
Loaded Configuration File => /etc/php.ini

In the example above, we use /etc/php.ini as our base php.ini file.

$ vi /etc/php.ini

Add the module you want to load. I usually add these at the very bottom, where my changes are. This is relative to the extension_dir directive you found above.

extension=dbase.so

Restart your php

In case of Apache

$ /etc/init.d/httpd restart
$ /etc/init.d/apache2 restart

or php-fpm:

$ /etc/init.d/php-fpm restart

And it's as easy as that!



Hi! My name is Mattias Geniar. I'm a Support Manager at Nucleus Hosting in Belgium, a general web geek & public speaker. Currently working on DNS Spy & Oh Dear!. Follow me on Twitter as @mattiasgeniar.

Share this post

Did you like this post? Will you help me share it on social media? Thanks!

Comments

tayfun Monday, December 29, 2008 at 17:40 - Reply

Hi Mattias,

I’ve used this great article as a guide in adding another module to PHP. Thanks for the step by step guide, I think there are not many guides to dealing with this without compiling whole PHP from scratch. I’ve written a Turkish version detailing this process for my blog site. Gotta spread the word.

Thanks again.


Matti Monday, December 29, 2008 at 18:25 - Reply

Hi Tayfun,

Glad you liked it, and found it useful. I only wrote it because I couldn’t find a decent solution myself, so I felt it was needed. :-)

Cheers,
Mattias


Brian Thursday, June 3, 2010 at 05:54 - Reply

Great tutorial!

Still best on the web two years later.


Muhammad Adnan Sunday, August 1, 2010 at 13:04 - Reply

hey folk,
I just came to this blog via Google. I was searching about “compiling extension from source php”

I have to install php extension pslib because there is no DLL for windows environment.


Pranav Tuesday, February 22, 2011 at 13:57 - Reply

How I can do this process on windows?
for http://pecl.php.net/package/pecl_http
thanks


Reem H. Wednesday, June 1, 2011 at 09:54 - Reply

Hi,
I tried this to enable xsl and everything went well BUT the xsl wan NOT enabled!! in phpinfo() nothing changed :(
I need help is there something missing or something that I should have done?
Thanks in advance


    Matti Wednesday, June 1, 2011 at 09:58 - Reply

    At the command line, do:
    # php -i | grep -i xsl

    Do you see anything appear there? If not, check your php.ini file and make sure the path to your extension is correct so PHP can load it. Also check for any errors when executing the “php -i” statement like “module cannot be found” or similar.

    If it works at the CLI, restart your Apache so the new config is loaded.


      Reem H. Saturday, June 4, 2011 at 11:59 - Reply

      I solved the problem.
      The problem was that XSL extension depends on DOM, so I had to do the process for DOM and enable it at first then do the same for XSL otherwise it won’t work.
      Thanks :)


THANK YOU Thursday, September 15, 2011 at 08:21 - Reply

I just wanted to say Thank you for this blog post , Detailed and informative :)

Thank you so much agian , finally I found the right way to do it !


pradeep kumar Monday, March 26, 2012 at 06:48 - Reply

Dear Mattias,
thank you for this wonderful blog. iam a windows user and i need to compile dbase module in php for my project. is it possible for me to do it in windows? or can i do it in linux environment and make an installer and move it to windows, install and use it. Please let me know. It would be of great help to me.

Thanks again,
Pradeep


    Matti Monday, March 26, 2012 at 08:54 - Reply

    Hi,

    You can’t compile the Windows DLL extensions on Linux, it has to happen on Windows. By default it is indeed missing, but I’m afraid I don’t have the experience on compiling it on Windows.


Harish Pareek Monday, January 28, 2013 at 21:23 - Reply

Hi,
I need php_perl.dll file for integration of php 5.3.4 with perl 5.14.3
If someone have this extension plzzz send me a copy to harishpareek11@gmail.com
Thanks in advance :)


peacengell Sunday, July 28, 2013 at 15:17 - Reply

Hi great tut I could not get gd and Zlib to work ..

I don’t know why
extension_dir=”/usr/local/lib/php/extensions/”
extension=gd.so
extension=zlib.so

please need help thanks..
if there any tutorials how to compile php with most
php extension send it to me


Rosa Thursday, May 7, 2015 at 20:02 - Reply

Hi!
I’m trying to configure the dbase extension. When I untared the tar and searched the ext/dbase I found that it was no folder named like that. Should I just create it? I’m quite new to linux.
BTW I used this path in the wget command http://be.php.net/distributions/php-5.5.24.tar.gz
I really apreciate the information.
Thanks :)


Guntars Wednesday, August 12, 2015 at 15:05 - Reply

Thanks, a really great post!
I used it as a starting point for writing a how-to about installation of the experimantal pdo_oci module. Of course all credits added!


mrfadh Monday, June 20, 2016 at 08:07 - Reply

Thanks… its really helpful


ilgar Jafarov Thursday, August 25, 2016 at 19:16 - Reply

thanks, helps me t much


Freddy Saturday, September 3, 2016 at 17:28 - Reply

I am trying to build my own PHP extension on Windows. but during the compilation i got the error below:

error LNK2019: simbolo externo __imp_QMCallx sin resolver
And this same error for the rest of the functions i’m using. I’m trying to build this lib using the VS2012 x86 Native Tools Command Prompt, doing nmake over the PHP source directory, which should build the whole PHP solution (including it’s libs).

This extension is needed to handle QM Database (www.openqm-zumasys.com) with PHP over Windows. This library is working fine on linux (CentOS).

The source code to build this lib was downloaded from QM Client Downloads (http://www.openqm-zumasys.com/downloads/).

I already have installed QM on my pc, so i guess all needed libraries are installed too.

Those functions as QMCallx belong to qmclient library. I know the path and i have access to all these external libs.

I’m just stuck at this point.

If is needed posting the source code, please, let me know it and i’ll do it.

Any comment? Thanks in advance.


Doug Wednesday, February 22, 2017 at 19:46 - Reply

Thank you this read was a very enlightening read on PHP extension the compilation requirements online are frequently muddied by people that say you must compile php first to write or compile custom extensions for use.


Leave a Reply

Your email address will not be published. Required fields are marked *

Inbound links