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).
- 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. - Download the PHP source code for whatever version you're currently running
- 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/. - Type
phpize
(This won't work if you didn't verify step 1) - 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. - Type
make - When finished, the compiler should tell you where it created the
.sofile (most likely in themodules/subdirectory of your current location). Copy the.sofile 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 theextension_dirparameter. You'll need root access to do this. - 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.ddirectory in it's own ini file for a cleaner installation approach. - Restart apache, if you're using it
- Check
phpinfo()to verify that your new module is installed