Debian 9 Stretch, PHP 7 and Microsoft SQL Server access

Nov 12

Microsoft supports SQL Server access from Linux and PHP 7, but unfortunately not with Debian 9 Stretch, but only for Debian 8 (and Ubuntu 15.10, Ubuntu 16.10, Ubuntu 6.04, RedHat 7 and MacOS).


I have tried to use the PDO Sybase drivers (package php7.0-sybase), but was not able to connect.

Then I found the page and have done the following:

pecl install sqlsrv-5.1.1preview
pecl install pdo_sqlsrv-5.1.1preview

The build failed until I installed the package unixodbc-dev
apt-get install unixodbc-dev

To enable the both modules in Apache2, I have done also the following:
cd /etc/php/7.0/mods-available
cp pdo_dblib.ini pdo_sqlsrv.ini
cp pdo_dblib.ini sqlsrv.ini

changed the contents of the files to list the appropriate -so file ( and,
cd /etc/php/7.0/apache2/conf.d
ln -s /etc/php/7.0/mods-available/sqlsrv.ini 20-sqlsrv.ini
ln -s /etc/php/7.0/mods-available/pdo_sqlsrv.ini 20-pdo_sqlsrv.ini

and restarted Apache2.

Now, my test function returned that the ODBC driver was missing.
I have downloaded then the Debian 8 version and installed the driver and the tools:
apt-get install unixodbc
dpkg -i msodbcsql_13.1.9.1-1_amd64.deb
dpkg -i mssql-tools_14.0.7.0-1_amd64.deb

After this, I was able to connect to the SQL server and run a select statement.

$conn = new PDO("sqlsrv:Server=$myserverName;Database=$myDB", $myUser, $myPass);

Comments are closed.