PURVEYOR OF FINE WORDS

May 24, 2006

May 24 2006

How to create PHP extensions (aka .so objects)

Although PHP has a great library of functions, many of them are not included in the standard build, or haven’t been included into the popular package installers like yum or apt-get. The man page doesn’t leave you with much instruction, other than something like “compile PHP with the flag –with-pspell[=dir]”. At this point you have 2 options:

  • Recompile PHP
    Find your existing PHP configure command and append this new flag, and recompile PHP. This takes a while and is generally quite bothersome, if not unacceptable like when you’re in a production environment.
  • Create a dynamic extension
    Compile a separate file (usually ending in .so) that you copy into a PHP directory, and edit php.ini. If you are running multiple machines on the same OS, you can just copy the file to all those machines as well. Much easier, and you can turn it on and off at will.

Here’s how to create the extension for modules that appear in the PHP manual on a linux-based system (for third-party extensions, it’s most likely the same).

  1. Check that you have the PHP development package, which often comes in a separate package. Yum lists it as php-devel. You’ll need its components in a few steps.
  2. Download the PHP source code for whatever version you’re currently running
  3. In the source code, there is an ext/ directory that should contain a subdirectory for the module that you’re looking for. Change to that subdirectory, i.e. ext/pspell/.
  4. Type phpize
    (This won’t work if you didn’t verify step 1)
  5. Type ./configure –with-pspell=/usr
    Replace the red portion with the text that is specified in the PHP man page for the module you want. For example, MySQL improved would be something like --with-mysqli=/usr/local/mysql/bin/mysql_config. Be aware that the path is sometimes a base directory, and sometimes needs to point to a specific file. Read the PHP docs carefully.
  6. Type make
  7. When finished, the compiler should tell you where it created the .so file (most likely in the modules/ subdirectory of your current location). Copy the .so file to your PHP extensions directory, i.e. /usr/lib/php/modules. If you don’t know this, it’s listed in your php.ini file under the extension_dir parameter. You’ll need root access to do this.
  8. Finally, tell PHP about your new extension by adding one line to php.ini:
    extension=pspell.so
    Alternatively, if you already have a bunch of extensions installed, you can place it in your /etc/php.d directory in it’s own ini file for a cleaner installation approach.
  9. Restart apache, if you’re using it
  10. Check phpinfo() to verify that your new module is installed



3 Comments »

  1. What’s a “man page”?

    Comment by Jeff — June 17, 2006 @ 9:03 am

  2. I guess he mean “main” :p

    Comment by Alixky — June 21, 2006 @ 6:05 pm

  3. Um… no. By a “man page” he’s referring to the *nix command ‘man’ which accesses the MANual pages for a given app/command/etc–in other words, the built-in help file.

    The fact that you don’t know this suggests that everything in this tutorial is beyond your *nix knowledge and/or ability. Caution is advised.

    Comment by Nomina — July 17, 2006 @ 2:13 pm

RSS feed for comments on this post. TrackBack URI

Leave a comment

 

Linking

Links provided by kottke.org.

Offering

Syndicating