Adding support for MS SQL Server to PHP in Linux

Adding support for MS SQL Server in PHP is not very difficult. Searching (Google/Bing/whatever) reveals lots of information on how to do this with Windows – naturally – but very little on how to go about it using Linux. Most people use precompiled PHP installations and I will show how to add MS SQL Server support to a precompiled PHP installation here. Those of you compiling PHP yourselves will probably understand what to do and what not based on the information here as well.

1. Install FreeTDS

First download and install FreeTDS from freetds.org. Use the following build commands to enable support for MS SQL Server (as root or using sudo).

./configure --enable-msdblib --prefix=/usr/local/freetds
make && make install

Unfortunately you need to tweak the installation somewhat as PHP still checks for files in FreeTDS that is no longer part of the installation. Just make sure these files exist (empty) by issuing the below commmands. (If you use another –prefix path above you will need to change the path accordingly)

touch /usr/local/freetds/include/tds.h
touch /usr/local/freetds/lib/libtds.a

2. Get the PHP source and compile the mssql extension

Yes – you need the complete PHP source even though you already have a precompiled PHP installed. You will not touch your PHP installation and we are not going to compile all of PHP. We need the source to be able to compile the mssql extension.

It is advised to always use the source of the same PHP version you have installed!

Unpack the source and compile the mssql extension. Remember again to change the path accordingly if you installed freeTDS in another location.

cd php*/ext/mssql
phpize
./configure --with-mssql=/usr/local/freetds
make

The extension should now be compiled and ready to install. You will find the binary in the immediate sub directory modules.

3. Install the extension

Find out where PHP expects to find extension libraries. The simplest way to check this is through the command line.

php -i | grep extension_dir

Some distributions use different php.ini files for command line PHP and the PHP web server module. So it might be good to double check using the function phpinfo() in a php script loaded through the web server (using your browser is easiest).

<?php phpinfo();

Then search for extension_dir in the configuration information displayed. For example, on my laptop running Ubuntu, the path is /usr/lib/php5/20060613+lfs.

Continuing from above without having moved away from the directory where you compiled your mssql extension.

cp modules/mssql.so /usr/lib/php5/20060613+lfs/

The extension is in the right place and all you have to do now is to make sure PHP actually loads it. To do this add the extension somewhere in the php.ini file. For example in the section Dynamic Extensions to keep it somewhere logical.

extension=mssql.so

3. Restart the web server

If using the Apache 2 web server you would normally issue

/etc/init.d/apache2 restart

4. Post installation

Well that’s about it. You should have a workable mssql extension added to your PHP installation. You should be able to continue using your platforms chosen way of upgrading PHP without affecting the MS SQL Server support.

However, you might need to dig into the freetds.conf file. If you have followed my steps without altering the installation path you will find the freetds.conf file in /usr/local/freetds/etc/freetds.conf.

Sometimes it is difficult getting the connection work without adding it to the the freetds.conf. Especially since you may have to use different values for the tds version directive depending on the MS SQL Server version. Examples:

[logisticsServer]
host = ntmachine.localdomain
port = 1433
tds version = 7.0

[intranetServer]
host = 192.168.1.145
port = 1433
tds version = 4.2

Again. Check that you are using the correct freetds.conf file and that you are using the correct tds version! More information on this at freetds.org. This and the above mentioned “missing files” that PHP is looking for are the two most common pitfalls.

Good luck!

Tagged with: , , ,
Posted in PHP
81 comments on “Adding support for MS SQL Server to PHP in Linux
  1. Your style is very unique compared to other people I’ve read stuff from.
    I appreciate you for posting hen youu have the opportunity,
    Guess I will just bookmark this web site.

  2. May I simply just say what a comfort to uncover somebody
    who actually knows what they are discussing on the internet.
    You certainly realize how to bring an issue to light and make it important.
    More and more people must check this out and understand this side
    of the story. I was surprised you are not more popular because you definitely
    have the gift.

  3. Alfred says:

    Take a look at my web-site site (Alfred)

  4. Link exchange is nothing else however it is only placing the other person’s web site link on your page at proper place and other person will also
    do same for you.

  5. relaxing says:

    Hello There. I discovered your weblog using msn. This is an extremely neatly
    written article. I will make sure to bookmark it and return to read more of your helpful info.
    Thanks for the post. I’ll definitely return.

    Look into my page – relaxing

  6. Very good blog post. I absolutely appreciate this site. Keep
    it up!

  7. Guillermo says:

    This is the right web site for anybody who really wants to understand
    this topic. You realize so much its almost hard to argue with
    you (not that I really would want to…HaHa).
    You definitely put a brand new spin on a subject which has been written about for years.
    Wonderful stuff, just great!

  8. Hi there, all is going fine here and ofcourse every one is sharing information,
    that’s actually fine, keep up writing.

  9. I’ve been exploring for a little for any high quality
    articles or weblog posts on this sort of area . Exploring in Yahoo I ultimately stumbled upon this site.
    Studying this info So i’m glad to exhibit that I have an incredibly just right uncanny feeling I discovered just what I needed.
    I so much no doubt will make certain to do not overlook this
    website and give it a glance on a continuing
    basis.

  10. Leandra says:

    Great blog right here! Also your site quite
    a bit up very fast! What host are you the usage of?
    Can I am getting your affiliate link in your host? I want
    my website loaded up as fast as yours lol

  11. parody songs says:

    Hurrah, that’s what I was searching for, wha a data!

    existing here at this website, thanks admin of this website.

  12. Very soon this web site will be famous amid all
    blogging and site-building visitors, due to it’s pleasant posts

  13. I’d like to thank you for the efforts you have put in penning
    this website. I am hoping to see the same high-grade content from you in the future as well.
    In truth, your creative writing abilities has inspired me to
    get my own site now ;)

  14. hi!,I really like your writing very much! share we keep up a correspondence
    more approximately your article on AOL? I need an expert on this space to
    resolve my problem. Maybe that’s you! Looking ahead to see you.

  15. Tasha says:

    Thanks very nice blog!

    Alsso visit my web blog :: hair loss treatment (Tasha)

  16. search says:

    Assume that the real estate agent will push for the
    higher end of your budget, and that most people will buy a home for
    around $195,000 when their budget limit is $200,000.
    This means that you will probably have to look at subprime.
    s wall, this will greatly help in creating
    an impression.

  17. Clara says:

    Pretty! This has been an incredibly wonderful article. Many
    thanks for providing these details.

  18. Van says:

    My brother recommended I might like this web site.
    He was totally right. This post truly made my day. You cann’t imagine just howw much time I had spent for this information! Thanks!

  19. Wonderful blog! I found it while browsing on Yahoo News.
    Do you have any suggestions on how to get listed in Yahoo News?
    I’ve been trying for a while but I never seem to get there!
    Cheers

  20. sbobet link says:

    It is not my first time to visit this site, i am
    browsing this web site dailly and get fastidious data from here everyday.

  21. Hi, I want to subscribe for this website to obtain most up-to-date updates,
    so where can i do it please help out.

  22. Have you ever thought about creating an e-book or guest authoring on other websites?
    I have a blog based upon on the same ideas you discuss and would love to have you
    share some stories/information. I know my readers would enjoy your work.
    If you’re even remotely interested, feel free to shoot
    me an e-mail.

  23. Great beat ! I would like to apprentice while you
    amend your website, how could i subscribe for a blog web site?
    The account aided me a acceptable deal. I had been tiny
    bit acquainted of this your broadcast provided bright clear
    idea

  24. bitcoin says:

    Fantastic goods from you, man. I’ve take into account your stuff prior to and
    you’re just extremely excellent. I actually like what you have bought right here, really
    like what you are stating and the way through which you assert it.
    You are making it enjoyable and you still take care of to stay it wise.
    I can not wait to learn much more from you. This
    is really a great web site.

  25. Norman says:

    What’s up, this weekend is pleasant designed for me, because this occasion i am reading this fantastic informative piece of writing here at my residence.

  26. Greetings I am so delighted I found your webpage, I really
    found you by accident, while I was looking on Yahoo for something else,
    Anyhow I am here now and would just like to say kudos for
    a remarkable post and a all round interesting blog (I also love
    the theme/design), I don’t have time to read through it all
    at the moment but I have book-marked it and also added your
    RSS feeds, so when I have time I will be back to read a great deal more,
    Please do keep up the excellent work.

  27. Miriam says:

    Magnificent items from you, man. I have be aware your stuff previous to
    and youu are just extrrmely fantastic. I actually like what you have got right here,
    certainly like what youu are stating and the best way
    by which you assert it. Youu make it enjoyable and you still care for to stay it
    sensible. I can’t wait tto read far more from you.
    This is actually a wonderful site.

  28. Gabriel says:

    I’m now not sure the place you’re getting your info, however great topic.
    I needs to spejd some time finding out much more or figuring out more.

    Thanks for fantastic info I was looking for thyis information for my mission.

  29. Alfonzo says:

    Hello there, you have actually done an exceptional task.
    I will definitely digg it and directly suggest to my pals.
    I make certain they’ll benefit from this internet site.

    Feel free to surf to my web-site: play fantasy football for money,
    Alfonzo,

  30. This is extremely appealing; you’re an excellent professional blog owner.

    I’ve joined your feed and look forward to searching for additional of your superb
    articles. I have actually shared your web site in mmy social networks.

  31. I have to thank yyou for the efforts you have actualy placed in writing this website.
    I definitely wish to have a look att the very same state-of-the-art
    post by you in the fuiture too. Your creative writing capacities has actually encouraged
    me to acquire myy very own web site now ;-) .

10 Pings/Trackbacks for "Adding support for MS SQL Server to PHP in Linux"
  1. [...] Linux zu kompilieren und einzubinden. Danne Lundqvist hat dazu in seinem Blog einen entsprechenden Artikel [...]

  2. abcphp.com says:

    Adding support for MS SQL Server to PHP in Linux…

    Adding support for MS SQL Server in PHP is not very difficult. Searching (Google/Bing/whatever) reveals lots of information on how to do this with Windows – naturally – but very little on how to go about it using Linux. …

  3. [...] 1/6/2010 Update Adding support for MS SQL Server to PHP in Linux [...]

  4. [...] Lundqvist has a new post showing how to get MS SQL Server support to work with your PHP installation on linux with the help [...]

  5. [...] Adding support for MS SQL Server to PHP in Linux [...]

  6. [...] here: Adding support for MS SQL Server to PHP in Linux January 5th, 2010 | Tags: but-very, linux, little-on-how, nformation-on-how, not-very, php, [...]

  7. [...] are a lot of tutorials out there on how to build the extension yourself on Mac or Linux machine (this site for example), but none seemed to work for [...]

  8. [...] Adding support for MS SQL Server to PHP in Linux This entry was posted in Linux, Programming, Uncategorized, Web Designing by mehrdust. Bookmark the permalink. [...]

  9. [...] Винрарная статья по установке [...]

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>