Short answer

sudo apt-get install php5-dev
cd /path/to/extension

The extension directory must have a minimum of

1. config.m4
2. php_sample.h
3. sample.c

phpize
./configure
make
sudo make install

Now add the dynamic extension to your php.ini files in /etc/php5.

;;;;;;;;;;;;;;;;;;;;;;
; Dynamic Extensions ;
;;;;;;;;;;;;;;;;;;;;;;
;
; If you wish to have an extension loaded automatically, use the following
; syntax:
;
;   extension=modulename.extension
;
; For example, on Windows:
;
;   extension=msql.dll
;
; … or under UNIX:
;
;   extension=msql.so
;
; Note that it should be the name of the module only; no directory information
; needs to go here.  Specify the location of the extension with the
; extension_dir directive above.
; Example lines:
extension=sample.so

Long answer

http://mattiasgeniar.be/2008/09/14/how-to-compile-and-install-php-extensions-from-source/