How-to: Setup Ubuntu and PHP to use MSSQL Microsoft SQL Server

On July 21, 2011, in How-to, by Cubert aka (Cube Dweller)

It is easier to get PHP on Ubuntu to connect to MSSQL than it is to get PHP on Windows.


I would not normally believe such a thing until I tried to setup a pre-existing install of PHP on Windows missing any MSSQL extensions. Looking through Google to find information was less than quick and I found myself wanting to go back to my old faithful Ubuntu server, but how?

Here are my 10 quick steps to get PHP seeing MSSQL:

There are some basic software we will need to grab using apt-get, then we will install them and modify the php.ini file to update the new extension. I will go over the process below on how to install and in what order but before we do that I will give you a brief description of what we are doing.

We will need to get the source for PHP, we will also need the PHP Development packages. We will be installing FreeTDS and then editing our php.ini file. Lastly restarting Apache to complete the process.

For the rest of this we will assume you have ( sudo su -) so you do not need to sudo each cmd, but if not then add sudo where it applies. We are also assuming you already have a working php system and only need to add MSSQL functionality.

  1. apt-get source php5 – This will install the PHP source in the the current directory that you are in.
  2. cd php5-5.3.2/ext/mssql – This will place you in the PHP source directory for MSSQL. You may need to edit the directory path as 5.3.2 was the version as of this document and may not be the same version you are installing.
  3. apt-get install freetds-dev – This installs the extra libraries we will need for MSSQL
  4. apt-get install php5-dev – This installs the application (phpize) which we will use next to help build the MSSQL extension.
  5. phpize  – Run this while inside the (/ext/mssql ) directory
  6. ./configure –with-mssql – This creates the MakeFile we will need to build the extension from source.
  7. make – This cmd builds the extension.
  8. cd modules – move in to the module directory.
  9. cp /usr/lib/php5/20090626   – Here you will need to pay close attention to the last directory here. This will very based on your version so change the numbers to fit what you have in your (/usr/lib/php5) directory.
  10. vi /etc/php5/apache2/php.ini – We want to go down to where you will find (Dynamic Extensions) and add on a new line the following -> extension = then save and close the vi session ( Esc : wq)

You are all done, just restart Apache and test your mssql_connect

Restart Apache:

/etc/init.d/apache2 restart



Tagged with:

11 Responses to “How-to: Setup Ubuntu and PHP to use MSSQL Microsoft SQL Server”

  1. dario says:

    Great Post!

  2. Adriano says:

    Hey man. you really saved my day.
    For those out there who just started with linux, this is the best tutorial on how to install mssql. Follow all steps and get that damn mssql working really quickly.
    Just be carefull with the first step, make sure you are in your home dir and try to make an empty folder like php_sources so you don’t get your files mixed up with this.
    Everything works like the tutorial says.

    Thank you!

  3. nickc says:

    Yep, good post. Concise, accurate and works!


  4. Craig says:

    Thanks so much. Exactly what I was looking for…

  5. mario says:

    Excellent, worked like a charm!

    the only problem I’ve had was that copy-pasting 6th command to PuTTY resulted in changing it from:
    ./configure –with-mssql
    ./configure .with-mssql

    but it was easy to spot.


  6. gurukatre says:

    Many Thanks

    in my case
    i used
    ./configure –with-mssql
    instead of
    ./configure –with-mssql

    and it worked

  7. gurukatre says:

    it seems to problem with wordpress it replace ( – – ) with (–)

  8. steelsnake says:

    Much easier than that:
    sudo apt-get install freetds-common freetds-bin unixodbc php5-sybase
    …restart Apache. Voila, MSSQL support.

  9. I would figure that Ubuntu would someday make this install easier for the average user, However that is not so for all distros of Linux.

  10. Juan Vasquez says:

    Keep getting the following error after the ./config

    Could not find /usr/lib/libsybdb.a|so

    Which version of ubuntu are you using?

  11. You’ve saved me from pulling my hair our for the second time 🙂 (or at least saved me from re-factoring a lot of code to use PDO.) I just wish I’d made a note of this the first time round a few years ago… It took me some time to find it again this time!

Leave a Reply