以下示例以CentOs7.6 + php7.3 为例,可根据实际情况切换其他版本,注意版本之间的对应关系即可
第一步、安装Microsoft ODBC driver for SQL Server (Linux)
通过 https://learn.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-ver16 查询到支持php7.3版本的 Microsoft Drivers 版本为5.6和5.8,这里选择 5.6, 对应的 DOBC driver 版本 选择ODBC 17
sudo su
#Download appropriate package for the OS version
#Choose only ONE of the following, corresponding to your OS version
#RHEL 7 and Oracle Linux 7
curl https://packages.microsoft.com/config/rhel/7/prod.repo > /etc/yum.repos.d/mssql-release.repo
#RHEL 8 and Oracle Linux 8
curl https://packages.microsoft.com/config/rhel/8/prod.repo > /etc/yum.repos.d/mssql-release.repo
#RHEL 9
curl https://packages.microsoft.com/config/rhel/9.0/prod.repo > /etc/yum.repos.d/mssql-release.repo
exit
sudo yum remove unixODBC-utf16 unixODBC-utf16-devel #to avoid conflicts
sudo ACCEPT_EULA=Y yum install -y msodbcsql17
# optional: for bcp and sqlcmd
sudo ACCEPT_EULA=Y yum install -y mssql-tools
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
source ~/.bashrc
# optional: for unixODBC development headers
sudo yum install -y unixODBC-devel
第二步,下载 Microsoft Drivers for PHP for SQL Server
下载地址(5.6):
https://github.com/microsoft/msphpsql/releases/download/v5.6.1/CentOS7-7.3.tar
下载后解压
第三步,安装 sqlsrv 扩展
查看当前安装的PHP版本的 Thread Safety
php -i | grep "Thread Safety"
# Thread Safety => enabled
enabled 代表线程安全,使用 php_sqlsrv_73_ts.so 和 php_pdo_sqlsrv_73_ts.so
disabled 代表非线程安全,使用 php_sqlsrv_73_nts.so 和 php_pdo_sqlsrv_73_nts.so
将对应的扩展文件复制到php扩展目录里面
查看扩展目录
php -i | grep "extension_dir"
将文件上传到扩展目录
编辑 php.ini 文件,增加以下配置
extension=php_sqlsrv_73_nts
extension=php_pdo_sqlsrv_73_nts
重启PHP
sudo /etc/init.d/php-fpm restart