Configuring MSSQL Server to talk to PHP in Ubuntu

B. Creating MSSQL extension

1) We get the PHP source code
$ apt-get source php5

2) Extract the compressed folder
$ tar -zxvf php5_5.3.6.orig.tar.gz

3) Change directory to the extracted folder and then to the MSSQL directory inside ext folder
$ cd php5-5.3.6/ext/mssql/

4) Create and install the MSSQL extension. Make sure you have php5-dev package installed on your system so that you can build PHP             extensions from source. Check if phpize command works
$ phpize

     If yes then it we know php5-dev package is installed on your system. If not then we need to install, use the below command

$ sudo apt-get php5-dev

    After it gets install, use the command from inside the directory where your mssql extension source code is present

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

$ make install

Check if php_mssql.so file residing inside the PHP extension directory. In my case it is in /usr/lib/php5/20090626+lfs/

C. Install PDO for MSSQL

sudo apt-get install php5-sybase

D. Enviromental variable configuration

We need to configure our environment to load the environment variable

1)    $ sudo vim /etc/profile

At the end of the file we add
export FREETDSCONF=/etc/freetds/freetds.conf

2)    $ sudo vim /etc/apache2/envvars

At the end of the file we add
export FREETDSCONF=/etc/freetds/freetds.conf

3)    Configuring FreeTDS

$ sudo vim /etc/freetds/freetds.conf

At the end of the file we add

[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.0

4)     $ sudo vi /etc/ld.so.conf

At the end of the file we add
/usr/local/freetds/lib

Then restart your apache server

$ sudo /etc/init.d/apache2 restart

Check the PHP info page if “mssql” and “pdo_dblib” is listed. We can also check the same by using the below command
$ php -m

We are done with the configuration. Lets configure our application

i. CakePHP Configuration

Inside Config/database.php we check the following

public $default = array( ‘datasource’ => ‘Database/Sqlserver’, ‘driver’ => ‘mssql’, ‘persistent’ => false, ‘host’ => ‘xxx.xxx.xxx.xxx’, ‘login’ => ‘xxxxx’, ‘password’ => ‘******’, ‘database’ => ‘xxxx’, ‘port’ => ‘1433’, ‘prefix’ => ”,

);

We should make sure that the datasource file is present inside “app/Model/Datasource/Database/Sqlserver.php” with Sqlserver class or Dblib class.

ii. CodeIgniter Configuration

Inside Config/database.php we change the following
db[‘default’][‘dbdriver’] = ‘mssql’;

Hope it comes handy to someone

150 150 Burnignorance | Where Minds Meet And Sparks Fly!