Using MSSQL with PHP on CentOS 4.4

Posted on April 18th, 2007 by phil and tagged .

OK, so you wanna connect to your MsSQL Database from your CentOS box. Most of these steps should be easily replicated on your CentOS version of choice, but in my case I'm using CentOS 4.4 and the latest version of PHP

$ cat /etc/redhat-release
CentOS release 4.4 (Final)
$ rpm -q php
php-4.3.9-3.22.4

If you can get a pre-built binary version of the PHP MsSQL RPM for your architecture of choice, you should use that. Building PHP from the SRPM (source RPM) as I'm about to do means installing a bunch of cruft on your system that you don't normally need. But if you're ok with that (if you're careful, you can keep track of it and remove it afterwards), let's proceed.

First of all, you need to install rpm-build:

$ yum install rpm-build

Next thing: download the PHP SRPM for your version. In my case, I downloaded it like so and installed it:

$ cd /usr/local/src
$ wget http://mirror.centos.org/centos-4/4/updates/SRPMS/php-4.3.9-3.22.4.src.rpm
$ sudo rpm -i php-4.3.9-3.22.4.src.rpm

(Commands here needing to be run as root are prefixed with sudo, as all of my work is done from a normal account. Feel free to run these all of these as root, but with the usual caveats about security).

Installing a .src.rpm usually simply installs files into /usr/src/redhat. In this case, there should be a SPEC file installed into /usr/src/redhat/SPECS/php.spec

MsSQL depends on FreeTDS. Now this is available via the RPMForge repository, but the locations that it puts the files in do NOT work as far as I can see. PHP requires the FreeTDS files be in specific file paths and the rpmforge package breaks these rules, so I downloaded the RPMs from
ftp://ftp.ibiblio.org/pub/Linux/ALPHA/freetds/stable/redhat/RPMS

So again, a big NB: DO NOT USE RPMFORGE.

Now, the next thing you need to do is define MSSQL in your ~/.rpmmacros file. The alternative is to edit the php.spec directly but that's just yucky.

So place the following into your ~/.rpmmacros file:

%_with_mssql 1

Now, if you do:

$ cd /usr/src/redhat/SPECS
$ sudo rpmbuild -bb php.spec

This should generate a warning about loads of -devel packages that are not installed on your system. Now you need to install all of them (this is the cruft I was talking about above). You can create your own specfile / edit the existing one, but this is the quickest and easiest way. You can of course, remove most of these -devel modules after the compilation has finished.

You may have a good 20 packages there, so yum install them.

If you wanna be lazy, the following bit of shell listed them out for me:

$ sudo rpmbuild -bb php.spec 2>&1 | awk '$1 != "error:" {print $1}' | tr "\n" " "

Once they're installed, and if you haven't done so already, install the freetds and freetds-devel packages. Again for me, I used:

$ sudo rpm -i freetds-0.62.1-1.i586.rpm freetds-devel-0.62.1-1.i586.rpm

Once those things are all installed, the PHP package should build:

$ cd /usr/src/redhat/SPECS/
$ sudo rpmbuild -bb php.spec

That should all build ok and RPMs are outputted to /usr/src/redhat/RPMS/i386/

$ cd /usr/src/redhat/RPMS/i386/
$ ls php-mssql*
php-mssql-4.3.9-3.22.4.i386.rpm

Now to install, simply use:

$ rpm -i php-mssql-4.3.9-3.22.4.i386.rpm

And restart httpd to enable the RPM.

I've left a copy of this particular RPM available for anyone to download who needs it. Be aware, it's not signed with any GPG key and you may want to build it from source yourself for peace of mind!

The link is here: http://www.brassy.net/files/rpms/centos4/php-mssql-4.3.9-3.22.4.i386.rpm

Update (25/07/07): php-4.3.9-3.22.5 is available and I've left the binary RPM for it here: http://www.brassy.net/files/rpms/centos4/php-mssql-4.3.9-3.22.5.i386.rpm