Connect remotely to MSSQL with PHP on CentOS 5
Beware this content is over 7 years old and considered stale. It may no longer be accurate and/or reflect the understanding of the author but remains here for reference only. Please keep this in mind as you use this content.
For some time now I’ve been looking for a solution for projects that are using PHP but are connecting to a remote Microsoft SQL Server database.
As many already know, the webmasters ideal (or at least mine) is a LAMP stack which, for those that are not sure, is Linux, Apache, MySQL and PHP. Why is that an ideal, you ask, well have you ever tried getting PHP interacting with a MS SQL database? Enough said.
In short doing the above is definitely not straightforward, even on a Windows platform, where you’ll most likely be transferring and copying .DLL’s from one system folder to another and editing .INI files left, right and centre.
The thing that put me off going directly to a Linux platform is that initial research suggested that the process to getting a working environment was much more complicated and it involved getting my hands a little dirty recompiling PHP from source code in order to enable the extensions/modules I required and then editing configuration files in several locations, and all this from a terminal/command line.
However, today I had a little breakthrough. I stumbled across this helpful post on Installing PHP 5.2.6 on CentOS 5 and I thought I try the changes out on my own VPS, the very VPS that this site is running on, and as you can see it’s still running.
Once I had everything working as it should be, I thought I better put together a quick post to remind myself how I managed this feat when I come to do it again.
Much of this tutorial relies on the oh so useful Yellowdog Updater, Modified (YUM) running on your distro of Linux. If you have a 1and1 server or VPS, it’s almost certainly there. YUM is an open-source command-line package-management utility for RPM-compatible Linux operating systems and you can read more about it over at Wikipedia if you wish to know more.
To give you a quick idea of what to do, you’ll need to log into your server/VPS as root. Now if you own a 1and1 server, you’ll have to login to your admin panel to get the root password and your server IP address. Your root password should be visible in your 1and1 Admin Panel, provided you haven’t already changed it. This root password should be the same password as the one your using to login to your Plesk Control Panel.
Remember to backup everything just in case something goes wrong as I won’t be taking any responsibility for a mishap. You should also keep in mind that this little step-by-step assumes that you have a fresh install and you haven’t configured your server or VPS considerably.
NB: You will need to login into your server using SSH and if your on a Mac, just fire up a fresh Terminal Window, otherwise if your on Windows, you’ll have to download PuTTY to do most of this.
1) Login via SSH: Replace the x’s with your servers IP address.
2) Once you hit Return, you’ll be asked for the password.
3) With 1and1’s standard VPS configuration, you’ll be running PHP 5.1.6 which is already pretty out of date and you’ll be wanting something more current to make use of all of the new functions and fixes available in the PHP 5.2.x releases.
To begin the upgrade install the CentOS’s Testing repository’s GPG key:
4) Then go ahead and download the repository file into your yum repo directory.
5) Start the upgraded to PHP 5.2.6 by entering the following command.
You should be prompted to confirm your decision to update and then, all being well, your get confirmation that it was successful.
6) Then, restart Apache.
7) Confirm the update using phpinfo() on your server. If you cannot confirm that your version of PHP has been upgraded to PHP 5.2.6 you should probably stop here and figure out why before going any further.
8) Now, provided all has gone well so far, all you need do now is enter a few more commands to enable MS SQL for PHP.
This command will get the compatible MSSQL extension (along with any dependencies FreeTDS and unixODBC that may be needed) and for the version of PHP you’ve just upgraded to.
9) Then, restart Apache one last time.
10) Test your changes by running some PHP/MSSQL scripts to ensure all is working well.
That should be it. Any questions or feedback, leave a comment behind and I’ll try and help you out.