Getting PHP and MSSQL talking on Mac OSX

April 5, 2012

Here is what I had to do to get PHP talking to an MSSQL database on MAC OSX (Lion). Note that I am running Acquia dev desktop for PHP, you’ll have to change the paths to PHP if your setup is different.

Download Free TDS stable source from http://www.freetds.org/software.html and extract from the tarball.

Login as root with

sudo /usr/bin/bash

Go into freetds sources folder and configure freetds

CFLAGS="-arch i386" LDFLAGS="-arch i386" ./configure --prefix=/usr/local/freetds --enable-msdblib --with-tdsver=8.0

Compile freetds with

make && make install

Download and extract PHP sources for your version of PHP from http://php.net/downloads.php

Go into the php sources folder ext/mssql directory and configure

CFLAGS='-arch i386 -isysroot /Developer/SDKs/MacOSX10.7.sdk -mmacosx-version-min=10.7' ./configure --with-php-config=/Applications/acquia-drupal/php/bin/php-config --with-mssql=/usr/local/freetds

This will create a file called libtool

Go back to the freetds sources directory and run

make clean
CFLAGS="-arch i386" LDFLAGS="-arch i386" ./configure --prefix=/usr/local/freetds --enable-msdblib --with-tdsver=8.0

delete the freetds install
rm -rf /usr/local/freetds

copy the libtool file from the php sources ext/mssql directory to the freetds directory
cp /path/to/php-source/ext/mssql/libtool /path/to/freetds-source

Compile and install freetds again
make && make install

Go to the php-sources/ext/mssql directory and compile mssql

cp the so file to your php extensions dir
cp modules/mssql.so /Applications/acquia-drupal/php/ext

Edit your php.ini to include the mssql.so extension (e.g. with vim or nano)
vim /Applications/acquia-drupal/php/bin/php.ini

Add the following line in the Dynamic extensions section

Tell the system where the FreeTDS config file is
echo "export FREETDSCONF=/usr/local/freetds/etc/freetds.conf" >> /etc/profile
echo "/usr/local/freetds/lib" >> /etc/ld.so.conf
ln -s /usr/local/freetds/bin/tds /usr/bin

and also run the followig (to save restarting your MAC)

export FREETDSCONF=/usr/local/freetds/etc/freetds.conf

Edit the freetds config file and set the freetds version to 8.0 (others dont seem to work with SQL server properly)
vim /usr/local/freetds/etc/freetds.conf
tds version = 8.0

Restart Web server

If you need help setting up SQLEXPRESS…

The basics…

Setting up SQLEXPRESS for mixed login (windows authentication didn’t work for me from my remote PHP server)

Adding a new user account to sql express:

Let this new account access a db:


One comment

  1. CFLAGS=’-arch i386 -isysroot /Developer/SDKs/MacOSX10.7.sdk -mmacosx-version-min=10.7′ ./configure –with-php-config=/Applications/acquia-drupal/php/bin/php-config –with-mssql=/usr/local/freetds

    Result: -bash: ./configure: No such file or directory

    I did change the config path the proper value.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: