HOWTO: PHP with MSSQL on Ubuntu 6.06

I recently set up an Ubuntu server that needed to access a MSSQL database. The php5-sybase package sort of worked (the queries appeared to work, but data was missing). So, I set out to build some .deb packages with MSSQL support built in.

First make sure you have the Debian package development tools:

apt-get install build-essential debhelper

(More tools might be needed depending on what you already have installed, but you will tend to get warnings about what is missing)

Then, download the PHP sources:

apt-get source php5

(The current version in the Ubuntu repositories at this time is php5-5.1.2, so adjust for version numbers in the future.)

And you will need all the dependencies for building PHP:

apt-get build-dep php5

Change into the debian build directory for PHP:

cd php5-5.1.2/debian

The first step is to edit the “modulelist� file. Insert a line above the one that says

mysql MySQL

with the contents:

mssql MSSQL

Next, edit the file “rules” and look for the section starting with “configure-apache2-stamp”. You will see a line similar to:

--with-mysql=shared,/usr

Open a line above this with the contents:

--with-mssql=shared,/usr

Make sure not to forget the backslash on the end.Now, we must edit the “control” file in the same directory. Add this to the end of the file:

Package: php5-mssql
Architecture: any
Depends: ${shlibs:Depends}, ${misc:Depends}, ${php:Depends}, php5-common (= ${Source-Version})
Description: MSSQL module for php5
 This package provides a module for MSSQL using FreeTDS.
 .
 PHP5 is an HTML-embedded scripting language. Much of its syntax is borrowed
 from C, Java and Perl with a couple of unique PHP-specific features thrown
 in. The goal of the language is to allow web developers to write
 dynamically generated pages quickly.

Move up a directory.

cd ..

Now, run:

dpkg-buildpackage

And go get something to eat. This will do the whole configure/compile for PHP and create debs in the parent directory. When this process is done, move up to the parent directory:

cd ..

And, you will see all of the PHP debs, including the new php5-mssql_[version].deb

Run a dpkg -i on the ones you need, and you should be all set to connect to MSSQL databases.

technorati tags:, ,

13 thoughts on “HOWTO: PHP with MSSQL on Ubuntu 6.06

  1. Please ignore my last post… I know now why It didn’t work: I think I entered dpkg -i php5_*.deb and that did not install the mssql deb… i explicitly installed the mssql deb and it worked… thanks

  2. Ranouf, I have the same problem….

    root@linux:~/php5-5.1.6# dpkg-buildpackage
    dpkg-buildpackage: source package is php5
    dpkg-buildpackage: source version is 5.1.6-1ubuntu2.1
    dpkg-buildpackage: source changed by Martin Pitt
    dpkg-buildpackage: host architecture i386
    dpkg-buildpackage: source version without epoch 5.1.6-1ubuntu2.1
    dpkg-checkbuilddeps: error: per-package paragraph 23 in control info file is missing Package line
    dpkg-buildpackage: Build dependencies/conflicts unsatisfied; aborting.
    dpkg-buildpackage: (Use -d flag to override.)

  3. sorry… Ignore my last post… It was due to extra spaces after I pasted into the control file… I do have another problem through. After I try to do a dpkg-build package I get the following. This will be the second tutorial that this has happened to me on. I’m not sure why either.

    root@linux:~/php5-5.1.6# dpkg-buildpackage
    dpkg-buildpackage: source package is php5
    dpkg-buildpackage: source version is 5.1.6-1ubuntu2.1
    dpkg-buildpackage: source changed by Martin Pitt
    dpkg-buildpackage: host architecture i386
    dpkg-buildpackage: source version without epoch 5.1.6-1ubuntu2.1
    debian/rules clean
    : No such file or directory
    ‘. Stop. No rule to make target `

    I think i’m going to try and use an older verison of PHP.

  4. great stuff, just finished my 4th build of this on a different machine over the past 6 weeks!

    Matt: If you move up to the parent folder the php5-mssql extension should have been built anyway, just install it with dpkg -i, worked perfectly for me!

  5. Thank you Matt and Robin for catching the extra blank line in the control file. When I first wrote this, it worked as in this article, but when updating my PHP installation on 6.10 more recently, I came across the same issue. I just forgot to update it in this article – thank you for reminding me, and I have fixed the code above.

  6. Hi Everyone. I went through this tutorial, and all went as expected. However after installation, I ran the following:

    — Installed the newly compiled packages
    dpkg -i php5-mysql_5.1.6-1ubuntu2.4_i386.deb
    dpkg -i php5-mysqli_5.1.6-1ubuntu2.4_i386.deb
    — Restarted Apache services
    /etc/init.d/apache2 restart

    This is all on Ubuntu 6.10 Server setup with LAMP during installation. Next I tried to run the follow script to test this, of course I plugged in my server’s IP Address and login info, plus a valid database and table.:

    “, “sa”, “”);
    mssql_select_db (“”, $con);
    $sql= “SELECT * FROM “;
    $rs= mssql_query ($sql, $con);
    echo “The field number one is: “;
    echo mssql_result ($rs, 0, 0);
    mssql_close ($con);
    ?>

    And now here’s what I get:

    Fatal error: Call to undefined function mssql_connect() in /home/webuser/public_html/test.php on line 4

    Any suggestions? Is there anyway to to verify the new packages are installed correctly?

    Thanks for any suggestions.
    Sam Alexander
    samalex@gmail.com

  7. @Sam Alex:

    I noticed that you installed “dpkg -i php5-mysql_5.1.6-1ubuntu2.4_i386.deb”.
    You need to install the .deb with “mssql” in the name, not “mysql”, if you need to get mssql functionality.
    It gets built into its own package, not bundled in with mysql.
    Hope this solves your problem!

    -Robert

  8. Just wanted to confirm that this method works on the new Debian lenny release. However it seems to have a problem with imap, and the deb build failed with errors on php_imap.c. I don't need IMAP so I just disabled it in the rules by setting the two lines with imap to –without. If php with imap & mssql is crucial to you, take care before upgrading to lenny.

  9. So i've followed the guide and installed php5-mssql_5.2.6-2ubuntu4.1_i386.deb.

    What do I need to do now to test it?

  10. How long does it take for the process to complete? Mine has been going for a very long time, seems like it is looping as well… Any comments?

Leave a Reply