You may want to connect to your Microsoft SQL database (MSSQL) by the applications in your Linux server. This article describes the steps to install the php modules php-mssql on CentOS 6 with PHP 5.3

 

Step 1 – Install required packages (freetds and php-mssql)

The module php-mssql is not in the default repository.  So it is required to add a repository in order to download it.

# yum install freetds
# cd /etc/yum.repos.d/
# wget http://rpms.famillecollet.com/enterprise/remi.repo
# yum install --enablerepo=remi php-mssql

 

Step 2 – Configure freetds

Add the following configurations at the end of /etc/freetds.conf

[mssql01]
host = your_mssql_server_ip
port = 1433
client-charset = UTF-8
timeout = 180
connection timeout = 180

 

Step 3 – Configure odbcinst

Add the following configurations at the end of /etc/odbcinst.ini

[freetds]
Description = v0.91 with protocol v8.0
Driver = /usr/lib/libtdsodbc.so.0
Setup = /usr/lib/libtdsS.so.2
UsageCount = 2

Note: the Driver and Setup path may be different in your system

# mv /etc/odbc.ini /usr/local/etc
# mv /etc/odbcinst.ini /usr/local/etc
# cd /etc
# ln -s /usr/local/etc/odbc.ini
# ln -s /usr/local/etc/odbcinst.ini
# chmod 0664 /usr/local/etc/odbc*.ini

 

Step 4 – Test connection to MSSQL server

# tsql –H your_mssql_database –p port_number –U db_user
1> use your_db;
2> go
1> select * from table_in_db;
2> go

The db information should be displayed. So it is better to select a table with less data.

 

Step 5 – Configure PHP

Uncomment the following configuration in /etc/php.ini

mssql.max_procs = -1

Restart apache

# service httpd restart
Step 5 – Create a php file for testing
# vim mssqldb_test.php
$db_host = 'mssql_db_host';
$db_user = 'mssql_db_user';
$db_pass = 'mssql_db_password';
$db_name = 'mssql_db_name';

$connection = mssql_connect($par_Config_host_MSSQL,$par_Config_user_MSSQL,$par_Config_password_MSSQL) or die("Connection Error");
if($connection)
    echo "Connection Fine!<br>";
$db = mssql_select_db($par_Config_db_MSSQL,$cn) or die("Database Error");
    if($db)
echo "Select Database Fine!<br>";

 

If you can see the output below in browser, Congratulations!

Connection Fine!
Select Database Fine!

 

Reference:

http://www.6tech.org/2013/01/centos-6-3-apache-2-2-15-mssql-2008-r2/