By compiling PHP with mssql extension support one can have capability to connect to MS SQL server from a PHP Linux environment.

For enabling mssql support in PHP we need FreeTDS installed and configured which is a set of Unix/Linux libraries that implement the TDS protocol.

This post explains the steps for doing this, lets get started.

First we will need to download and install FreeTDS, you can find more information and download link at http://www.freetds.org URL. Note the exact installation steps below :

cd /usr/local/src
wget http://ibiblio.org/pub/Linux/ALPHA/freetds/stable/freetds-stable.tgz
tar -xzvf freetds-stable.tgz
cd freetds-*/
./configure --prefix=/usr/local/freetds --with-tdsver=8.0 --enable-msdblib --enable-dbmfix --with-gnu-ld
make
make install

Once the installation is complete we will add the related FreeTDS libraries and binaries installation path to the dynamic linker binding config files at /etc/ld.so.conf , we will add below two paths :

/usr/local/freetds/lib
/usr/local/freetds/bin

Edit the config file using vi or editor of your choice to add above paths and then use ldconfig command to build dynamic links for the shared libraries :

vi /etc/ld.so.conf
ldconfig -v

You can check connectivity to external MS SQL server using below command :

tsql -S [servername] -p [port]  -U [username] -P [password]

Standard MS SQL port is 1443 which you can use above. Also if you want to have VPS/Server level connectivity configuration for MS SQL you can configure the/usr/local/freetds/etc/freetds.conf file for that.

Once done , we will proceed with rebuilding PHP using the /scripts/easyapache script on the cPanel servesr/vps. For a none-cpanel installation same config parameter can be used during configure step for compiling PHP.

We need to add the configure parameter –with-mssql=/usr/local/freetds in file /var/cpanel/easy/apache/rawopts/all_php5 . Once done, save the file and proceed with easyapache script to rebuild PHP.

/scripts/easyapache

If you are on a 64bit platform , you may recieve this error :

configure: error: Could not find /usr/local/freetds/lib64/libsybdb.a|so

You will need to create a symlink from the original location of the libaray, the path can vary depending on the platform. In my case I noticed the file was located at normal lib directory under freetds i.e. it was located at /usr/local/freetds/lib , so I simply created a symlink named lib64 to the oringal directory lib as follows :

cd /usr/local/freetds
ln -s lib lib64
ldconfig -v

Also added the path /usr/local/freetds/lib64 to the ld.so.conf file and build the dynamic linking as done above.

Once easyapache script completes successfully, you will find mssql support enabled in PHP. You can verify that using below command :

php -i | grep mssql

Now we will create/install a copy of mssql.so libaray which we can then enable in the php.ini file. On cPanel system this can be achieved by following below steps :

cd /home/cpeasyapache/src/php-5.2.17/ext/mssql/
phpize
./configure
make
make install

This will install a the mssql.so library at the extension_dir path mentioned in php.ini file located at /usr/local/lib/. To enable the extension add below link in php.ini file :

extension=mssql.so

Save the file and restart Apache.

Thats it !

Tags: , , ,

6 Comments on How To : Install mssql extension for PHP in cPanel CentOS / Linux

  1. badtz says:

    Just to note, if you try and copy/paste these commands the – in the font you are using causes errors, that said thanks, this page saved me some time and trouble getting this installed and configured.

  2. Riz Khan says:

    @badtz , Thanks for notifying, it appear the formatting was bit messed up , I have corrected it now , copy/pasting the command should work fine now.

  3. Harry says:

    It appears that the mssql extension no longer builds (for PHP 5.3 at least) with the latest freetds-stable package, which was updated on 8/18/2011. You’ll encounter errors like:

    php-mssql/php_mssql.h:68:24: error: redefinition of typedef ‘LPBYTE’

    To work around this, just use an earlier version of freetds:

    ftp://ftp.ibiblio.org/pub/Linu......82.tar.gz

  4. che says:

    If you get Cannot find FreeTDS in known installation directories
    try this
    touch /usr/local/freetds/lib/libtds.a
    touch /usr/local/freetds/include/tds.hp

    then on the last step use
    ./configure –with-mssql=/usr/local/freetds

  5. Thanks this helpme a lot ! Congratulations from Brazil.

  6. Clebson says:

    Hi,

    I get the following error when executing the command ./configure after command phpize.

    Do you know the cause of this error and how to solve it?

    Error: Cannot find FreeTDS in known installation directories

    Thanks.

Leave a Reply