Home > php | software | unix > How To: Install mssql extension for php on unix

How To: Install mssql extension for php on unix

woh! okie.. this installation is gng to be long a tough.. took me a few days of google crawling and testing before i finally get it to work.

okie, why will u need to install mssql extension on php? possibly becos u need to connect to a ms sql server of ur vendor or something like that. for integration i hope. cos unix works best w php and mysql, ie, installation, it is almost already thr. correct me if i m wrong.

so wad you need?
if your server is jus fresh out of the box or say u had installed whm/cpanel there will be quite a far bit of stuff u will need.

1. Txt2man
2. unixODBC
3. freeTDS
4. PHP mssql.so

if this point if you are wondering if i m using any rpm for installation?
sorry,i m not, cos my server centos installed w whm/cpanel seems to be poorly equipped or configured for rpm installation, too many dependencies required. so jus download all the binary and upload them onto ur server.

Setup 1: Install unixODBC package

Install unixODBC first. Download the software in your home directory or anywhere you like and uncompress and untar the software.

You can do this by using following command

# tar -xvzf unixODBC-2.2.12.tar.gz

Once it is done, go to the unixODBCxxxx dir and run the configure command.

# ./configure -prefix=/usr/local -enable-gui=no

Note: If you dont use enable-gui option and dont have Qt package, you will get the error. So make sure you use it if no Qt is installed.

After configure is successfully completed, run the make and then make install commands.

# make

Install it

# make install

Step 1.1: Installing Txt2man
this step is optional if your server already have txt2man installed.
txt2man is required by freeTDS to man it documentation, i do find this portion pretty useless though.
else jus download the package, and run the make

# make

Install it

# make install

Step 2: Install freeTDS package

Download the freeTDS package and untar and uncompress it.

# tar -xvzf freetds-stable.tgz

Run the configure.

# ./configure -with-tdsver=8.0 -with-unixODBC=/usr/local

Run the make

# make

Install it

# make install

Step 3: Configuration freeTDS
Start configuration with freeTDS. Look for the freetds.conf file and add the entry for the MS SQL server.

[MSHOSTNAME]
host = MSHOSTNAME
port = 1433
tds version = 8.0

Sql server standard port is the 1433. The tds version for MS SQL server 2000 is 8. You can try 8.0 with new version as well.
Important: the host need to be the same as the declaration. ie
if you have [mssql.hostname.com], ur host will be = mssql.hostname.com

Step 4: Configuration – unixODBC

The unixODBC need two main configuration files called odbcinst.ini and odbc.ini. These files should be in the /etc/ dir.

I. The first file odbcinst.ini contain the definition of ODBC driver.

[TDS]
Description = FreeTDS driver
Driver = /usr/local/lib/libtdsodbc.so
Setup = /usr/local/lib/libtdsodbc.so
Trace = Yes
TraceFile = /tmp/freetds.log
FileUsage = 1

Note: Before making above entries, make sure libtdsodbc.so exists in the /usr/local/lib dir.
In the above configuration, we have define TDS as driver.

II. The second file is odbc.ini. This file has information about your MS SQL database.

[MSHOSTNAME]
Driver = TDS
Description = MS SQL Test
Trace = Yes
TraceFile = /tmp/mstest.log
Servername = 192.168.1.100
Database = testdb
Port = 1433

You can notice above that the freeTDS driver is the name defined in odbcinst.ini while servername is the one defined in freetds.conf.

Step 5: Test MS SQL connectivity from Linux box
Check if you can connect to MS SQL database using unixODBC tool called isql.

# isql -v mstest satest satest
+—————————————+
| Connected! |
| |
| sql-statement |
| help [tablename] |
| quit |
| |
+—————————————+
SQL> select * from “sysObjects”;

If you see the results, you are connected.

Step 6: Getting your mssql.so file
Update: I came back to follow this to create an .so file and realise, mmm, this instruction wasn’t too clear. So here it goes. From PHP.net Download page, download the source code, then if you unzip, you will find a folder “ext” and “mssql” in the “ext” folder. tat’s the folder i m referring to, ‘cd’ to this folder and then run phpize as below mentioned.

Add: for the benefits of those who just need the .so file, you can download the mssql.so from here. however, i don’t assure, compatibility with you php version. is built with php 5.2.10 binaries.

php.net is not distrubuting the binary version of php package for *nix server, so you will have to create the .so extension file yourself. download the source file for php and upload the folder /ext/mssql/ into your server
then

# cd /uploaded_path/mssql/
# phpize
# ./configure
# make
# make install

this commands will automatically create a copy of mssql.so in the installed extension folder.
you can check your extension folder from

<?php
phpinfo();
?>

Step 7: Enabling the extension

# echo “extension=mssql.so” > /usr/local/lib/php.ini

or you can use a texteditor and add that value manually into php.ini

Step 8: Restarting Apache Server!
dependin on version installed and location

# /etc/init.d/httpd restart

and you are done!
do drop me an email should you face any problem at any step!
i know the pain in gettin this extension installed on unix. and considering rpm fails to work in the first place.
i will say, this is the best way to get it installed, cos u have full control over it. and didn’t fail me, of cos, after hell lots of googling around.

Source:
dpacool.com
unixodbc.org
Scott’s Blog
PHP PECL

, , , , , , , , , , ,

Comments:48

Leave a Reply
  1. didats 08/05/19

    you are saving my life…

    Thank you very much for your documentation…!

  2. ialexs 08/06/16

    thanks… at least i’m heading for the right direction…

    trying to work with yum/rpm first before getting the tar ball

  3. ialexs 08/06/16

    done. lucky me, my yum/rpm is all okay

    (search the freetds for your CentOS)
    # yum search freetds

    you’ll find freetds*.rpm freetds-devel*.rpm freetds-doc*.rpm

    (install with yum)
    # yum install freetds-xxx

    (or download the rpm with yumdownloader)
    # yumdownloader freetds-xxx

    the rest is the same with the above article

    :)

  4. admin 08/06/16

    ialexs! that great to hear. =)

  5. ialexs 08/06/16

    also notes, if it’s on a 64b box (in this case still with Centos),
    the freetds installing the lib in /usr/lib64

    be sure when do a “configure” in the php source directory to define the “–with-lib” option

  6. AlexM 08/08/15

    Your blog is interesting!

    Keep up the good work!

  7. Evan 08/08/26

    I cannot get passed step 4. For some reason, I cannot find the odbc ini files, both odbc.ini and ocbcinst.ini are missing from the /etc folder. I do have the libtdsodbc.so file though. How do I find the odbc ini files?

  8. Steven 08/08/27

    Hi Evan, you got the first step right without errors? cos the installation will create those 2 files.

  9. Evan 08/08/27

    Hi Steven,

    yeah the first 3 steps completed fine with no errors. I downloaded and installed everything from the /root folder as root user. However, I forgot where unixODBC was installed after I ran the make install. Is there a way to manually copy the ini files from somewhere?

  10. Steven 08/08/27

    Evan, you need to find the directory where you installed, cause ODBC is configured to read from those path. u can try this, “vi unix.ini” and then insert the above content, and same for odbcinst.ini
    not too sure if this will work tho. let me knw! =)

  11. Evan 08/08/27

    understood. I created the ini files and cloned the content above. Now on step 5, I am ready to test isql. My firewall admin has not opened port 1433 yet but I have all the settings correct for the ini file. So naturally when doing the isql test connect it will say data source name not found, but it also says no default driver specified.

    Will both these errors go away when the port is open or do I still need to do something about the default driver?

  12. Evan 08/08/27

    whoops, guess what, I found the odbc.ini and odbcinst.ini files in the /usr/local/etc folder. That’s where it was installed by the unixODBC make install. I edited these and removed the ones I made in the /etc folder in the root.

  13. Steven 08/08/27

    ah ha!!! u get to connect w isql? i think u may need to open the port first.

  14. Evan 08/08/27

    lol. Of course I may be getting too preemptive here, but I don’t suppose there is an easier way to get step 6 done, I’m having trouble finding the binary mssql.so download. I found an RPM on rpmfind.net but I’m not sure if that’s the right one. Can’t you tell I’m a newbie? :)

  15. Steven 08/08/27

    no worries, that actually the easiest after ur first try, go to http://www.php.net/downloads.php to download the complete source for your version, the same major version shld works too actually.. then mssql.so can be found inside.

  16. Evan 08/08/27

    ack! ok I got the source, tarred and browsed to /root/php-5.2.6/ext/mssql

    Did the phpize and then ./configure.

    After starting ./configure it halts and says it cannot find freeTDS in known installation directories. Is there a setting I’m missing to maybe change the path to where FreeTDS is? (/usr/local/lib)

  17. Evan 08/08/27

    I added this to the configure line:

    ./configure –with-mssql=/usr/local

    Now it errors out with /usr/local not being a freetds installation directory.

  18. Steven 08/08/27

    did u run freetds configure w the following line?

    # ./configure -with-tdsver=8.0 -with-unixODBC=/usr/local

    you seems to be facing quite a bit of wrong installation directory, on unixodbc too.

  19. Steven 08/08/27

    mmm, okie i wun say wrong, but non-default or standard folders.

  20. Evan 08/08/28

    Yeah I did exactly that on the ./configure line. Perhaps fate doesn’t want me to succeed with this. If I can just get the mssql.so file copied to the directory and add extension=mssql.so to the php.ini file, is that all I really need?

  21. Steven 08/08/28

    evan, yes! get the original php source to produce the mssql.so instead of getting the file itself, better to be done this way.

  22. Evan 08/08/28

    That’s what I’m trying to do but I can’t get it to work. That’s where I run into the error that #$%# freetds install directory is not valid.

  23. Evan 08/08/30

    ok I made some progress, the 0.82 version of freetds doesn’t seem to work here so I went back to 0.63, and I was able to make the mssql.so file and it copied to the php extensions directory. Upon adding it to the php.ini file with extension=mssql.so it seemed to have no effect. The phpinfo() does not show MSSQL support and the test page still shows mssql_connect as an undefined function.

  24. Steven 08/08/30

    ah! u restarted apache?

  25. Evan 08/08/30

    yes sir! did both versions:

    service httpd restart
    /etc/inid.d/httpd restart

    apache restarted but php did not add the mssql support…

  26. Evan 08/08/30

    crap. ok I got past that error and now php recognizes mssql support. but I can’t connect to my mssql server. I have to access it by it’s ip address and I already have port 1433 open.

  27. Steven 08/09/03

    Hey Evan, sorry i was away, so how is it now?
    successfully installed?

  28. Evan 08/09/07

    Hi Steven, no problem, I was away as well and just returned. I was able to get everything working perfectly! The only thing that was left standing in the way was a minor glitch that prevented connection to port 1433 but I got it resolved. Thanks again for all your help!

    By the way, I hope people will know that the latest build of FreeTDS (0.82) does not work with these instructions as it does not contain the tds.h file anymore. I had to download the 0.63 version to get it working.

  29. Steven 08/09/07

    Alright man!
    Yep, thanks for the update!

  30. Brian 08/09/18

    Steven, I ran across your info and it’s been a godsend helping me to do what’s necessary to connect to MSSQL from my SuSE Linux 11.0 system. Your instructions work fine so long as I use the package manager in SuSE to install things. But then, just like Evan, the ./configure command for making mssql.so halts and says “Cannot find FreeTDS in known installation directories”.

    Linux rpm’s put php, freetds and others in non-standard locations. For example, UnixODBC config files are in /etc/unixODBC, and lib files are in /usr/lib64/ and /usr/lib64/unixODBC/. Freetds executables are in /usr/bin/, config files are in /etc/ and lib files are in /usr/lib64.

    Maybe I can edit the php configure script to look for freetds elsewhere.

    I peeked into the script and it appears to be looking for a file called tds.h either in ‘/usr/local/include/’ or
    /usr/include/’. I found out that freetds-0.82 no longer writes a file called ‘tds.h’.

    One solution I found is to create two files where FreeTDS was installed e.g.:
    > $ touch /usr/include/tds.h
    > $ touch /usr/lib/libtds.a

    I think for me that would mean ‘/usr/lib64/’.

    Another solution I found is to edit the configure script (see http://bugs.php.net/bug.php?id=44991)

    if test “$PHP_MSSQL” = “yes”; then
    for i in /usr/local /usr; do
    – if test -f $i/include/sybdb.h; then
    + if test -f $i/include/tds.h; then
    FREETDS_INSTALLATION_DIR=$i
    FREETDS_INCLUDE_DIR=$i/include
    break
    – elif test -f $i/include/freetds/sybdb.h; then
    + elif test -f $i/include/freetds/tds.h; then
    FREETDS_INSTALLATION_DIR=$i
    FREETDS_INCLUDE_DIR=$i/include/freetds
    break
    @@ -27,10 +27,10 @@

    elif test “$PHP_MSSQL” != “no”; then

    – if test -f $PHP_MSSQL/include/sybdb.h; then
    + if test -f $PHP_MSSQL/include/tds.h; then
    FREETDS_INSTALLATION_DIR=$PHP_MSSQL
    FREETDS_INCLUDE_DIR=$PHP_MSSQL/include
    – elif test -f $PHP_MSSQL/include/freetds/sybdb.h; then
    + elif test -f $PHP_MSSQL/include/freetds/tds.h; then
    FREETDS_INSTALLATION_DIR=$PHP_MSSQL
    FREETDS_INCLUDE_DIR=$PHP_MSSQL/include/freetds
    else
    @@ -38,8 +38,8 @@
    fi
    fi

    – if test ! -r “$FREETDS_INSTALLATION_DIR/$PHP_LIBDIR/libsybdb.a” &&
    test ! -r “$FREETDS_INSTALLATION_DIR/$PHP_LIBDIR/libsybdb.so”; then
    – AC_MSG_ERROR(Could not find
    $FREETDS_INSTALLATION_DIR/$PHP_LIBDIR/libsybdb.[a|so])
    + if test ! -r “$FREETDS_INSTALLATION_DIR/$PHP_LIBDIR/libtds.a” &&
    test ! -r “$FREETDS_INSTALLATION_DIR/$PHP_LIBDIR/libtds.so”; then
    + AC_MSG_ERROR(Could not find
    $FREETDS_INSTALLATION_DIR/$PHP_LIBDIR/libtds.[a|so])
    fi

    PHP_ADD_INCLUDE($FREETDS_INCLUDE_DIR)

    This link was last dated Jun 13, 2008.

    I don’t think this completes the solution for me unless I also change the directory in which the script is looking. And change the file it’s looking for: my installation of freetds doesn’t have ‘ibsybdb.so’. It has ‘ibsybdb.so.5’.

    This is a long comment. It may prove useful to others. I’m going to mess around with these ideas and see if they help me.

  31. Steven 08/09/19

    Hey Brain, definitely useful, i think i am gng to rewrite this documentation after i try doing a new installation on another server w the latest available versions.
    of cos, when i have got the time.

    yea, installing mssql in linux is one pain in the ass, it took me 2 weeks before i finally concluded on the above. most of the rpm because of versions issues dun really seems to work well with each other, unless u got the specific version that comes together.

  32. Brian 08/09/19

    Well, I made a few little changes to the configure script so it not barf when it came to locating freetds. I altered the location it was looking in and the file it was looking for and so the ‘./configure’ command was rewarded with “config.status: creating config.h”.

    Hoping for the best, I proceeded to the ‘make’ command. It bombed with a laundry list of errors. The first ones were

    error: sqlfront.h: No such file or directory
    error: sqldb.h: No such file or directory
    error: expected specifier-qualifier-list before ‘LOGINREC’

    … and 320 more. I’ll start scouring the net for what to do now. Damn!

  33. Brian 08/09/19

    Steven, I discovered I had to install the ‘freetds-devel’ and ‘python-devel’ (2.5.x) rpm’s to work out those dependencies needed by the configure script. The ‘make’ and ‘make install’ commands then worked fine and the ‘mssql.so’ extension was automatically created in ‘/usr/lib64/php5/extensions’ (where SuSE wants ’em).

    After that I copied and pasted the [MSSQL] section from another ‘php.ini’ (I had on my Mac, where mssql connections already worked) and I created ‘mssql.ini’ in ‘/etc/php5/conf.d/’ (where SuSE wants them) so that the ‘mssql.so’ extension would load. Finally I restarted Apache and my goal was reached.

    Thanks a million for putting your solution on the web! It took a lot of searching before I found it. You might be the only person who’s published a solution.

  34. Steven 08/09/19

    Brian, you are most welcome! btw, what OS and version r u on?

  35. Brian 08/09/19

    Principal software and versions: OpenSuSE 11.0 x86_64, KDE 4, PHP 5.2.6, Apache 2.2.8, PostgreSQL 8.3.3, running on an 8-core Intel MacPro in a dual-boot configuration

  36. Steven 08/09/19
  37. Justin A 09/02/12

    Hi. I tried to install FreeTDS on a system running a 64bit version of opensuse. I couldn’t get it to work.
    I run
    # ./configure -with-tdsver=8.0 -with-unixODBC=/usr/local

    and
    then make.
    After that I run make check to see if it worked.
    it ends up saying that I failed 10 of 13 tests in the make check.

    I was wondering if you ever ran into a problem like this ?

  38. Steven 09/02/14

    hi Justin, you may want to check the versions? of your tds and unixodbc built.
    i know of installation test failure for newer version or either.

  39. R 09/02/17

    help!
    after on step 6 i got the following error:
    Cannot find config.m4.
    Make sure that you run ‘/usr/bin/phpize’ in the top level source directory of the module
    i’m very sure that i am in directoy ‘/usr/bin when executing “phpize” sorry i’m new in linux and i don’t know what that means…

  40. Endackady 09/05/23

    eh. really like it :)

  41. José Carlos 09/07/19

    hi!!!

    I have tried this and lots of tutorials but I couldn’t obtain mssql.so

    Coul anybody please send me it to jcchavezs[a]uni.pe

    ITS TOOOOOOO URGENT PLEASE

    thanks a lot!!!!!

  42. fotos de chicas en ligero 09/07/19

    I should notify you about it.

  43. Steven 09/07/19

    Hi José Carlos,

    I have upload a copy of mssql.so here, you can download from, but will still recommend you follow the steps above to get your .so file. do hope it helps.

  44. uberweiss 10/03/04

    i have followed this guide but get the following error when i run the command isql -v mstest satest satest which is under step 5.

    [IM002][unixODBC][Driver Manager]Data source name not found, and no default driver specified
    [ISQL]ERROR: Could not SQLConnect

    can anybody help what this mean.?!?

  45. Steven 10/03/04

    uberweiss, you may want to check your “/usr/local/lib/” folder if the “libtdsodbc.so” is there? and make sure in step 3 your names are same and correct.

  46. invisibled 10/03/04

    Hey There,

    I followed the tutorial and connected to the database in terminal no problems. But I can’t seem to get the mssql extension installed. It Doesn’t show up in phpinfo() and says call to undefinded function when i run mssql_connect()

    any suggestions? I’m using OS X snow leopard.

  47. Steven 10/03/04

    invisibled, you may want to check out http://lists.ibiblio.org/pipermail/freetds/2009q2/024684.html for snow leopard.

  48. uberweiss 10/03/05

    Dear Steven.

    Thank you very much for your feedback.

    the “libtdsodbc.so” wasnt in “/usr/local/lib/” but in “/usr/lib/” so i have copied it to “/usr/local/lib/”.#

    i’ve checked the information entered in the /etc/freetds.conf and it looks correct.

    [a_name]

    host = 192.168.10.5
    port = 1433
    tds version = 8.0

    but i still get the same error.

Leave a Reply

Your email address will not be published. Required fields are marked *

Trackbacks:2

Listed below are links to weblogs that reference
How To: Install mssql extension for php on unix from How to Get it Working
pingback from How to get it done » Blog Archive » Mac: Connecting to msSQL Server on Mac May 28, 2008

[…] Entropy.ch the following instruction is for you, else, you may try my earlier post on installing msSQL extension for php in *nix/Centos Server, largely used by […]

pingback from OFF TOPIC! » SQL Server support on PHP (php-mssql) September 30, 2008

[…] to Brian for the […]

TOP