Monday, September 29, 2008

PHP and PostgreSQL on Leopard.

Folks,

This is my first blog post, and I want to write on my experience while I was setting up PHP5 and PostgreSQL on Leopard. This post is technical and hope it helps others.

I started out with installing PostgreSQL, by downloading it form here. Installing the database and setting up of the user was a piece of cake.

The next step was to make sure that php understands functions like pg_connect(), however it turns out that the necessary binaries for the same are not present with the built in version of php that comes with Leopard. So the only option is recompile php.

Which I faitfully did by issuing the following commands

sudo ./configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --disable-dependency-tracking --with-apxs2=/usr/sbin/apxs --with-ldap=/usr --with-kerberos=/usr --enable-cli --with-zlib-dir=/usr --enable-trans-sid --with-xml --enable-exif --enable-ftp --enable-mbstring --enable-mbregex --enable-dbx --enable-sockets --with-iodbc=/usr --with-curl=/usr --with-config-file-path=/etc --sysconfdir=/private/etc --with-openssl --with-xmlrpc --with-xsl=/usr --without-pear --with-pgsql=/Library/PostgreSQL/8.3/

sudo make

sudo make install


Please note that sometime you may want to specify the path to your PostgreSQL as I did with --with-pgsql=/Library/PostgreSQL/8.3/

Php was recompilied successfully but it turns out that the newly created libphp5.so is not understood by apache2.
so issuing apachetl --help gives the following error

harshad$ apachectl --help

httpd: Syntax error on line 114 of /private/etc/apache2/httpd.conf: Cannot load /usr/libexec/apache2/libphp5.so into server: dlopen(/usr/libexec/apache2/libphp5.so, 10): no suitable image found.
Did find:\n\t/usr/libexec/apache2/libphp5.so: mach-o, but wrong architecture

Turns out that you need to reinstall apache.
I did the same by issuing the following commands

./configure --enable-layout=Darwin --enable-mods-shared=all
make
make install


Open the apache config file located at /usr/local/apache2/conf/httpd.conf (for me) and added the following 2 lines

LoadModule php5_module /usr/libexec/apache2/libphp5.so
AddType application/x-httpd-php .php


and I was successfully able to connect to my postgresql database using php on leopard.