Building the PHP MS SQL Server extension from source on Ubuntu 8.10
Yesterday I wrote about how I got an enhanced Sybase CT extension for PHP built on Ubuntu 8.10 from source files and from Red Hat RPM archives. Today I wanted to write about installing the SQL Server extension from source on Ubuntu 8.10.
I have written about this once before but it was for a Red Hat environment. Seeing as we are converting all of our system at work to Ubuntu I had to do the same thing for the Ubuntu platform. And wouldn't you know it, Debian and Red Hat are vastly different from each other.
Aside from the stupidity that is the Ubuntu package manager's rendition of the SQL Server extension for PHP the fact remained for me that I had to be able to integrate database communication from LAMP to a SQL Server on Windows as well to a Sybase server on Unix. The Sybase bit was taken care of in yesterday's write up. But how do we get PHP to talk to SQL Server on Ubuntu 8.10?
Throw out the notion that you can use the php-sybase package from the package manager. It munges stuff up pretty badly and at the same time does things to the Sybase extension that make it unusable for my needs. Not to mention that if their is a Sybase extension already installed then it really pukes because the php-sybase extension that installs the SQL Server functions actually installs the mssql_* functions then maps the sybase_* functions to them, which makes PHP squawk since those functions are already defined.
So the only way to make this happen is to build the SQL Server extension from source.
Building and installing the mssql extension from source
The follow steps will guide you through how I got the mssql extension built and installed on Ubuntu 8.10:
- Build the FreeTDS library
FreeTDS is a free, open source library of clients that allow Linux machines to talk to SQL servers and Sybase servers. Download the latest stable version of the FreeTDS library (as of this writing it was 0.82) and unpack the source to a directory on your machine. Once the source is unpacked, change to the directory where the source is located and configure and make the library:
$ ./configure --prefix=/usr/local/freetds --enable-msdblib
$ make
$ sudo make installDO NOT MAKE CLEAN AT THIS POINT.
Before you do anything else you must make sure that you have copied over all the necessary files from FreeTDS. "Wait, didn't the installation routine do that?" you might ask. No, it doesn't, because of a change made on the part of the FreeTDS developers. There are two files that are needed in order to build the mssql extension. Those files are among the make files and must be copied over to the freetds install directory.
From inside the source directory of freetds, where you built it from, enter:
$ sudo cp include/tds.h /usr/local/freetds/include
$ sudo cp src/tds/.libs/libtds.a /usr/local/freetds/lib - Get the PHP source code
Change to a directory where you wouldn't mind having the entirety of the PHP source stashed. From the command line type:
$ apt-get source php5 - Copy the mssql extension files to a directory you can build from
My preference is to leave source alone and work from copies. So I always copy the files I need to a different location and build from the copies. To that end, I created a directory at ~/SourceCode/php-mssql and copied the following files from the original PHP source directory:
$ cp ext/mssql/config.m4 ~/SourceCode/php-mssql
$ cp ext/mssql/php_mssql.c ~/SourceCode/php-mssql
$ cp ext/mssql/php_mssql.h ~/SourceCode/php-mssql - Make and install the extension
Make sure you have the php5-dev package installed on your system so that you can build PHP extensions from source. From inside the directory where your mssql extension source code is, at the command line, enter:
$ phpize
$ ./configure --with-mssql=/usr/local/freetds
$ make
$ make installFind out where your extensions directory is on your machine and quickly check it to make sure there is a php_mssql.so file living in it. On my machine the extension directory is /usr/lib/php5/20060613+lfs/. Yours may be different.
- Configure PHP to use the new extension we just made
Now we need to tell PHP to use the new extension we just built. To do that we need to create an ini file for the extension and put it inside of the extensions directory where PHP can find it. On my machine PHP looks for ini files to parse in /etc/php5/conf.d/ so naturally that is where I am going to go to to tell PHP to use this extension.$ cd /etc/php5/conf.d/Now we need to create an ini file and put into a directive to load the extension. You can use whatever editor you like. I prefer to use vim:
$ sudo vim mssql.iniInside this file place the following two lines:
; Enable the mssql extension
extension=mssql.so - Configure your environment to load a much needed environment variable whenever the machine starts
Much like the Sybase extension we did yesterday the mssql extension needs an environment variable in order to function properly. Again, this one caused me fits for a long time in Ubuntu. To be sure you can use the SQL Server extension from both the CLI and the web server you will need to add an environment variable to both the /etc/profile startup script AND the web servers environment variable setting script.$ sudo vim /etc/profileAt the end of the file add:
export FREETDSCONF=/etc/freetds/freetds.confNow add this same entry into your web server’s environment variables. I am using apache and assuming you are to. If not, consult your web server’s documentation for how to do this:
$ sudo vim /etc/apache2/envvarsNow add these entries to the end of the file:
export FREETDSCONF=/etc/freetds/freetds.conf - Configure FreeTDS
In order for FreeTDS to communicate properly with the SQL server a DSN of sorts needs to be created. To make a DSN you need to edit the freetds.conf file:
$ sudo vim /etc/freetds/freetds.confGo to the end of the file and add the following lines
;--- Custom MSSQL server name ---
; THIS CAN BE ANY NAMED SERVER REFERENCE YOU WANT TO CREATE
[SERVERNAME]
; This is the host name of the MSSQL server
host=HOSTNAME
; This is the port number to use
port=PORTNUMBER
; This is the TDS version to use for anything over Server 2000
tds version=8.0Now we need to add the freetds library to the load library stack.
$ sudo vi /etc/ld.so.confGo to the end of the file and add the following line:
/usr/local/freetds/lib - Restart your web server
Like everything that involves a change to the PHP environment or configuration on your machine, restart the web server. I am assuming this is being built upon an apache server. If not, you will need to know how to stop and start your web server or, at the very least, know how to reboot your machine:
$ sudo apache2ctl stop
$ sudo apache2ctl startRun a PHP info page or CLI call to see if it is loaded:
$ php -mYou should now see mssql.
Again, I hope this was helpful to you. If it was, please leave a comment and let me know. It something went haywire for you, leave me a comment, too. Systems being what they are, it is never unheard of for two almost identical systems to have vastly different experiences with building software from source.
Handling multiple Sybase result sets in PHP on Ubuntu 8.10
A long time ago I was tasked with creating a PHP application that would talk to our Sybase database that drives our entire enterprise at work. This is not normally a big thing to do as one can simply enable the Sybase extension for PHP and have a database connection generally within minutes. However there was one thing that was an absolute necessity in this application that made it significantly more difficult to get it working: some parts of the application would be reliant upon stored procedures that returned multiple result sets.
First off let me say that any time you have the option to build stored procedures for your database quieries, do it. They are faster, safer, compiled and centralized and they keep your code from being littered with query builders all over the place. That said, this is neither the time nor the place to talk about the merits of stored procedures in application development. Just know that where I work there are no direct queries ever allowed to touch our database servers.
Knowing that I had to work under the requirement of handling multiple Sybase result sets I set out to find out how to do it and what I found was alarming. Few database extensions in PHP handle multiple result sets. In fact, as I was looking around it seemed that only the MySQLi extension and the SQL Server extension handled multiple result sets. But after a little searching around I found something called the "PHP Sybase CT driver enhancements" project on Sourceforge that essentially provided prepared statement handling and multiple result set handling for the PHP Sybase Extension. However, after trying to install it it became evident that the extension, as it was written, was still not usable in the state it was offered up for download in.
Not to worry, I have modified the package for compiling within a PHP 5 environment and have made a few changes to it that should allow it to be used as is. The source code archive file can be downloaded (in tar.gz format) by clicking here.
Please read the notes on this source code before installing it!
Before installation
Before we get started we need to cover a few assumptions:
- You have a licensed copy of Sybase Adaptive Server
I have not tried this against any Sybase database server other than Adaptive server. - You have the Sybase client installed or at the very least have the installer available
For this exercise we are using a Red Hat RPM of the Sybase 12.5 client. Yes, we are installing on Ubuntu, which is essentially Debian and therefore does not use RPM files, but we will burn that bridge when we come to it. - You are comfortable building PHP extensions from source
There is no way around this. In order to get this to work you will need to be able to build from source. - You have the php-devel package installed on the machine you building this extension for
If you don't have it, a quicksudo apt-get install php5-devshould do the trick. This will be necessary to build the Sybase extension from source.
Installation instructions
Ok, these are the steps I took in order to build the enhanced Sybase extension for PHP on my Ubuntu 8.10 desktop machine starting with Sybase ASE client and common packages in Red Hat RPM format.
- Convert the Sybase 12.5 OpenClient and Sybase 12.5 Common RPM archives to DEB packages and install them
To do this I used the alien package converter tool and, while inside the directory where the RPMs were living, issued the following command:
$ alien -k sybase-common-12.5.0.1de-1.i386.rpm
$ alien -k sybase-openclient-12.5.0.1esd-1.i386.rpmThis created two new archives, in the directory I was in, in debian format named
- sybase-common-12.5.0.1DE-1_i386.deb
- sybase-openclient-12.5.0.1ESD-1_i386.deb
I then issued the following commands to install them:
$ sudo dpkg --install sybase-common-12.5.0.1DE-1_i386.deb
$ sudo dpkg --install sybase-openclient-12.5.0.1ESD-1_i386.debThis made a directory named sybase-12.5 in the /opt directory.
- Prepare your environment to build the extension
To make things easier (and a little more compatible with the Red Hat way of doing things) I created a link inside of /top called sybase and pointed it to the sybase-12.5 directory.
$ cd /opt
$ ln -s -T /opt/sybase-12.5 sybaseI then had to export some environment vars that are needed by the extension and the web server in order to handle communication with the Sybase server:
$ export SYBASE=/opt/sybase
$ export SYBASE_OCS=OCS-12_5
$ export PATH=/opt/sybase/OCS-12_5/bin:$PATH
$ export LD_LIBRARY_PATH=/opt/sybase/OCS-12_5/lib:/opt/sybase/OCS-12_5/lib3p:$LD_LIBRARY_PATH
$ export INCLUDE=/opt/sybase/OCS-12_5/include:$INCLUDE
$ export LIB=/opt/sybase/OCS-12_5/lib:$LIBYes, I know that each one of these commands could have been placed in a file and sourced, but for some reason source is not available to my installation of Ubuntu so it was in fact easier and faster for me to do it this way. Do this how you will, but remember the values because you will need these later.
- Build and install the php-sybase-ct extension from source against the Sybase client you just installed
Remember that package I told you about earlier? The one that I said you could download? If not, get it now and unpack it to a directory somewhere where you have permission to unpack stuff on your system. For me, it was ~/Temp.Change to the directory you unpacked the source code to and configure it for make and installation using the php-devel package:
$ cd ~/Temp
$ phpize
$ ./configure --with-sybase-ct=$SYBASE/$SYBASE_OCS
$ make
$ sudo make installFind out where your extensions directory is on your machine and quickly check it to make sure there is a php_sybase_ct.so file living in it. On my machine the extension directory is /usr/lib/php5/20060613+lfs/. Yours may be different.
At this point you can safely do a
sudo make cleanbut you might want to hold off on that until it is all working in case you need to rebuild at all. It does happen from time to time. Just sayin'. - Configure PHP to use the new extension
Now we need to tell PHP to use the new extension we just built. To do that we need to create an ini file for the extension and put it inside of the extensions directory where PHP can find it. On my machine PHP looks for ini files to parse in /etc/php5/conf.d/ so naturally that is where I am going to go to to tell PHP to use this extension.$ cd /etc/php5/conf.d/Now we need to create an ini file and put into a directive to load the extension. You can use whatever editor you like. I prefer to use vim:
$ sudo vim sybase_ct.iniInside this file place the following two lines:
; Enable the sybase extension
extension=sybase_ct.so - Configure your environment to load the correct environment variables whenever the machine starts
This one caused me fits for a long time in Ubuntu. In order to ensure that you can use the Sybase extension from both the CLI and the web server you will need to take all of the environment variables that exported prior to building the extension and place them in both the /etc/profile startup script AND the web servers environment variable setting script. This caused countless hours of frustration and anger for me and I hope I can save you some angst with this little snippet.Add the environment vars to /etc/profile. You can use any editor you like. I like vim:
$ sudo vim /etc/profileAt the end of the file add the environment vars:
export SYBASE=/opt/sybase
export SYBASE_OCS=OCS-12_5
export PATH=/opt/sybase/OCS-12_5/bin:$PATH
export LD_LIBRARY_PATH=/opt/sybase/OCS-12_5/lib:/opt/sybase/OCS-12_5/lib3p:$LD_LIBRARY_PATH
export INCLUDE=/opt/sybase/OCS-12_5/include:$INCLUDE
export LIB=/opt/sybase/OCS-12_5/lib:$LIBNow add these same entries into your web server's environment variables. I am using apache and assuming you are to. If not, consult your web server's documentation for how to do this:
$ sudo vim /etc/apache2/envvarsNow add these entries to the end of the file:
export SYBASE=/opt/sybase
export SYBASE_OCS=OCS-12_5
export PATH=/opt/sybase/OCS-12_5/bin:$PATH
export LD_LIBRARY_PATH=/opt/sybase/OCS-12_5/lib:/opt/sybase/OCS-12_5/lib3p:$LD_LIBRARY_PATH
export INCLUDE=/opt/sybase/OCS-12_5/include:$INCLUDE
export LIB=/opt/sybase/OCS-12_5/lib:$LIB - Restart your web server
Like everything that involves a change to the PHP environment or configuration on your machine, restart the web server. I am assuming this is being built upon an apache server. If not, you will need to know how to stop and start your web server or, at the very least, know how to reboot your machine:
$ sudo apache2ctl stop
$ sudo apache2ctl startRun a PHP info page or CLI call to see if it is loaded:
$ php -mYou should see sybase_ct. If not, something went wrong. If so, you are now golden.
At this point you should be able to run queries against your Sybase server AND handle multiple result sets using the sybase_next_result() function. There is no documentation for this function, but a quick read through of the mssql_next_result() function will tell pretty well how to use it.
Enjoy! And if this was at all helpful please leave a comment to let me know.
Notes on the PHP Sybase CT Enhanced extension
Some things to keep in mind when using this extension:
- This extension is coded in PHP 4 source.
- When I got my hands on it the code was still much in development phase. Not much has changed about that other than I removed debug output from it.
- It does not handle connection failure well when the server does not respond. So unwell in fact that internally it causes segmentation faults and results in blank web pages upon failure. I do not know how to fix that.
- If you know how to program in c or want to make it better, please feel free to do so. I have contacted the original authors of this extension and of the three only one has done any real work on it and none have worked on it in the last five years and really do not intend to. I was given permission to distribute it and modify it. But I really do not know what I am doing in c. Yet.
Setting up Fedora 10 as a VM in Windows
A couple of days ago I posted about my experience in setting up Ubuntu 8.10 as a virtual machine under Windows Virtual PC. I was so insanely surprised at the ease of that installation that I decided to try doing it all over again with Fedora 10.
That was a colossal mistake.
I think part of the mystique behind Ubuntu as the next real viable alternative to Mac and Windows as an operating system is its total ease of use and installation. Even in a VM it was super easy to install and get working. Whether it was Wubi or a straight install, Ubuntu is just easy to install. Kubuntu is too. And for those that need super ultra easy Xubuntu is the way to go.
Fedora however is not at all on par with Ubuntu when it comes to ease of use. Fedora has never been easy to install or use and trying to get it into a VM was no exception.
I started out like I did with Ubuntu by trying a live install ISO. That failed miserably because the installer could not get through the first step. So I moved to the full install DVD ISO. Again, epic fail. After several failed attempts I ended up downloading and installing by way of the text based internet installer.
The text based installer worked, but it took about four hours (the first run did - yes, I said first run) as it downloaded various packages for installation. It also didn't help that I didn't know about a huge boot bug that would render a perfectly good installation as a black screen of crap. So after a few hours of actually getting Fedora 10 installed correctly - and without knowing it - I reinstalled it thinking I had screwed up. That was another four hours or so down the toilet.
After getting the second good install installed I had to figure out why I was getting that stupid black screen. It ended up being that the Fedora text based installer seems to not be able to pick up the monitor its on and chooses to not install a default monitor like Ubuntu does, so it just leaves it and tries to pick it up later. But it never gets there so it tries to display into nothing. So with a little research in hand and some googling done, I learned that if I hit a down arrow key immediately on start up I get access to the GRUB boot loader and I can edit the start up commands. Armed with that I was able to get some stuff handled.
The first thing I had to do was, at the kernel ... line, enter "e" to edit the loader and add vga=0x32D then enter "b" to continue booting. After that I followed a small bit mentioned in this tutorial about Installing Fedora 10 on Virtual PC about installing the system-config-display package and reconfiguring your display configuration.
Once that was done I was able to get logged in to Fedora to the command prompt. But I didn't get a desktop yet. After some work I was able to figure out that since there was really no display set that even changing the default run level from 3 to 5 would not force Fedora to boot to desktop. Entering the init 5 command at the prompt would take me to a GUI, but it would only allow me to restart or shutdown once there.
However I was able to get to a desktop by entering startx at the prompt, which allowed me to setup a default user instead of root. But...
That is where it stops. I haven't had the time to get this tested further. I will be spending more time on this, but I believe I will be spending that time in VMWare instead of Virtual PC because of the cross platform compatibility and portability. But if you are looking to setup a Linux install as a VM in Virtual PC, do yourself a favor and use Ubuntu instead of Fedora. It will be way better for you.
Using Picasa on Ubuntu
A few days ago I was working on some image stuff when I ran across a built-in Linux file system utility that allows you to sync images with Picasa. I used it to upload a few images then went about my other business.
A few days later I tried to find it again and was completely without luck. Not to be left unable to post pictures, I quickly began searching for a Linux Picasa app that would let me push my images to Picasa. At first all I found were pages that said "Google does not officially support Picasa on Linux". Then I found something cool.
I am not sure where I got this link from but I found a Picasa for Linux app provided by Google. How cool is that? Now I can manage my Picasa photos right from my Linux desktop.
Now all I need is a way to sync my iPhone to iTunes on Linux without using Wine and I am golden. Anyone got any links?
Connecting to an HP Photosmart 2610 on Ubuntu 8.04
Click here to go to the instructions and bypass all of my wonderful writing.
Today I fell in love all over again. With Linux. Specifically Ubuntu 8.04.
I have been playing around with Ubuntu 8.04 for the last week and a half or so and I have totally fallen in love with software, operating systems and geekery again. Not only does Ubuntu just flat out work straight out of the box, it has made using my computer fun again. And productive.
Take installing my printer driver on my machine. A few years ago I bought a new HP Photosmart 2610 printer for an outrageously discounted price at a black Friday after Thanksgiving sale at Circuit City. I had gotten it home and installed the software for it on my computer (a Winblows 2000 computer at the time). It took about 30 minutes and two reboots (that's right, you heard me) to get the software installed.
Once it was loaded it worked very well. It allowed me to print, fax, scan... the works. Right from my computer. Awesome.
When I got my new laptop about 2 years ago I installed the software for the printer on it. Once again, about 30 minutes and two reboots later I was able to use my printer. I followed this same process a few months later for my wife's laptop.
Did I mention all of these computers I did this too were Winblows? The last two were XP machines, one XP Pro the other XP home. Thanks Microsoft.
Today I decided to "trudge" through getting my printer connected to my machine. I say "trudge" because I have always heard that installing printers on Linux is a lesson in futility and that, seeing as I am losing hair already, if I wanted to keep what little hair I had I would just not bother.
Was I ever stupid for listening to that mumbo jumbo.
Installing the HP Photosmart 2610 driver on Ubuntu 8.04
A quick Google search for HP Photosmart 2600 on Linux returned a link to the Linux Printing web site for my printer. After reading over the driver information I decided to visit the home page for HPIJS/HPLIP.
Once there I looked around and found that there is an unusual amount of support for many Linux distros form HP's printer division. Since I quickly found my distro on the list I decided to use Add/Remove ... from the Application menu to see if I could install my printer that way.
Did I mention I love Ubuntu? In 8.04 go to Applications -> Add/Remove ... and in the search box enter HPLIP. You should get one result. Install the "HPLIP Toolbox" and once it is done, go to System -> Preferences -> HPLIP Toolbox.
If your system is like mine (vanilla to the core) you will have no printers installed to manage. I was asked by the Toolbox if I wanted to add a printer and I said I did by clicking the "Add device" button.
I was asked how my device was connected to my machine and I chose "Network" since that is how my printer is connected.
About 1 second later I was told my printer had been found and did I want to install that printer. I said yes and within about 1 second I was asked which driver I wanted to install. Since there was only one I chose it.
Within about 1 second my driver was installed.I was asked to set up some information for the printer (like the name, location and such as well as the fax number and fax name). I chose the defaults (because they were what I would have entered anyway) and let the setup work.
About five seconds later I was able to test print. No reboot. No 30 minute installs. Just printing. Fast.
Did I mention I love Ubuntu?
Anyhow, I hope this can help someone else who may have believed that setting up printers on Ubuntu was hard. This was painfully easy and now I am printing away like a printing machine.
Well, not really. My printer is doing that. But I am telling it to. And it is all because of Ubuntu.