Linux下使PHP支持MSSQL(SQL Server),需要先安装FreeTDS。
PHP官方网站:http://www.php.net 当前版本5.26
FreeTDS官方网站:http://www.freetds.org 当前版本0.82
1. 编译FreeTDS
下载FreeTDS并解压,进入该目录。
./configure –prefix=/usr/local/freetds
make
make install
2. 编译PHP
使PHP支持MSSQL,可以使用两种方式,一种是直接编译进PHP,一种是编译成PHP可载入的模块。如果还没有编译安装PHP,建议使用前一种方式,因为这样PHP的执行速度快。如果已经安装了PHP,又嫌重新编译PHP麻烦,用后一种方式吧。
(1) 编译进PHP
如何编译PHP,请参考LAMP环境配置(http://www.code-life.com/?p=185)。要增加对MSSQL的支持,只要在编译参数上加上–with-mssql=/usr/local/freetds即可。
(2) 编译成模块
cd /path/to/php/source 进入PHP源码目录
cd ext/mssql 进入MSSQL模块源码目录
/usr/local/php/bin/phpize 生成编译配置文件
./configure –with-php-config=/usr/local/php/bin/php-config –with-mssql=/usr/local/freetds
make
make install
编译完成生成mssql.so,修改php.ini,将该模块载入:
extension=”/path/to/extension/mssql.so”
!! IMPORTANT 找不到FreeTDS编译失败的解决
在编译配置的时候可能会遇到找不到FreeTDS的错误:
configure: error: Directory /usr/local/freetds is not a FreeTDS installation directory
按照官方的解释(http://www.freetds.org/news.html),PHP检测一些文件以确定FreeTDS是否安装,由于新版的FreeTDS不再安装这些文件,导致PHP编译失败。只需建立两个空文件即可:
touch /usr/local/freetds/include/tds.h
touch /usr/local/freetds/lib/libtds.a