Compiling the MSSQL.so extension for PHP 5.3.5 on MediaTemple DV 4

We just enabled the MSSQL extension via FreeTDS on a Media Temple DV 4 VPS. Here’s a line by line of how we did it, borrowing heavily from http://www.mavrick.id.au/2012/php-5-3-6-mssql-freetds/
, but adding the particulars for the MediaTemple DV and for the particular version of PHP (5.3.5) that we were using:

Make a directory to hold these sources:

cd /
mkdir /source
cd /source

Grab the source of FreeTDS:

wget http://mirrors.ibiblio.org/freetds/stable/freetds-stable.tgz

tar zxvf freetds-stable.tgz
cd freetds-0.91

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

For some reason these files need to be copied over:

cp include/tds.h /usr/local/freetds/include
cp src/tds/.libs/libtds.a /usr/local/freetds/lib

cd /source

Adjust for your desired version of PHP (this example is 5.3.5):

wget http://museum.php.net/php5/php-5.3.5.tar.gz
tar xvfz php-5.3.5.tar.gz
cd php-5.3.5

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

Now, when we first ran the make command we had to deal with a type redefinition in

nano /source/php-5.3.5/ext/mssql/php_mssql.h

Copy the extension to the proper directory… You can find out what directory it is with this command:

php -i | grep extension_dir

In our case it was /usr/lib64/php/modules.

cp modules/mssql.so /usr/lib64/php/modules

Edit php.ini file (this is the global one):

nano /etc/php.ini

Add:

extension=mssql.so

Then restart your web server:

/etc/init.d/httpd restart

and done!


Other PHP MSSQL Resources:

http://php.net/manual/en/mssql.setup.php

https://github.com/jamestkirk/devbox/wiki/install-mssql-php-extension

http://www.robert-gonzalez.com/2009/02/18/building-the-php-ms-sql-server-extension-from-source-on-ubuntu-810/

https://docs.moodle.org/22/en/Installing_MSSQL_for_PHP

http://howtogetitworking.com/2008/02/26/how-to-install-mssql-extension-for-php-on-unix/

http://www.linuxquestions.org/questions/linux-server-73/how-to-enable-mssql-on-linux-589956/

 

UPDATED:

(For reference:)

I compiled the pdo_dblib.so extension… but it looks like PHP is compiled with –disable-pdo. So I went back to mssql_connect as a connection method…

Logging in as the user to the bash shell, I could telnet to the database server’s ip at port 1433.

From the command line as well, I could run tsql (the diagnostic tool that comes with FreeTDS) and connect to the server, where I was able to verify that the hostname, instance, and dbname lookups are correct. I could even run T-SQL statements and query the database successfully from there.

I configured the FreeTDS conf file to have [appname] as a reference to the server, and then accessed via the following code in:

putenv("FREETDSCONF=/etc/freetds.conf");
$hostname = 'appname';
 $username = 'username';
 $password = 'password';
$databasename = 'database';
//connection to the database
 $connection = mssql_connect($hostname, $username, $password)or die("Unable to connect to $hostname");
 echo "
Connected to MSSQL
";
mssql_select_db($databasename,$connection);
$result = mssql_query("SELECT TOP 1 field from table");
$row = mssql_fetch_array($result);
echo("
Result from sample query: ".$row[0]."
");

Which outputs the expected:

Connected to MSSQL
Result from sample query: x

 

​This was the missing puzzle piece:

 putenv("FREETDSCONF=/etc/freetds.conf");

 

Apparently FreeTDS looks in /usr/local/etc by default, not /etc.