Skip to content

Install pdo_sqlsrv for PHP 7.0 on Debian in 3 ways

Jenny Tam edited this page May 10, 2019 · 7 revisions

The following instruction is outdated. Please visit the documentation, which is kept up-to-date.

# You must be root

# Install UnixODBC
apt-get install unixodbc libgss3 odbcinst

# Compile odbc_config as it is not part of unixodbc package
cd /usr/local/src/
apt-get install devscripts debhelper dh-exec dh-autoreconf libreadline-dev libltdl-dev
dget -ux http://http.debian.net/debian/pool/main/u/unixodbc/unixodbc_2.3.4-1.dsc
cd unixodbc-2.3.4/
dpkg-buildpackage -uc -us -B
cp ./exe/odbc_config /usr/local/bin/
# Save this binary for later

# Microsoft ODBC Driver 13 for Linux
cd /usr/local/src/
#wget https://download.microsoft.com/download/2/E/5/2E58F097-805C-4AB8-9FC6-71288AB4409D/msodbcsql-13.0.0.0.tar.gz
wget -nv -O msodbcsql-13.0.0.0.tar.gz "https://meetsstorenew.blob.core.windows.net/contianerhd/Ubuntu%2013.0%20Tar/msodbcsql-13.0.0.0.tar.gz?st=2016-10-18T17%3A29%3A00Z&se=2022-10-19T17%3A29%3A00Z&sp=rl&sv=2015-04-05&sr=b&sig=cDwPfrouVeIQf0vi%2BnKt%2BzX8Z8caIYvRCmicDL5oknY%3D"
tar xf msodbcsql-13.0.0.0.tar.gz
cd msodbcsql-13.0.0.0/
ldd lib64/libmsodbcsql-13.0.so.0.0; echo "RET=$?"
# Replace "$(uname -p)" with "x86_64" in install.sh as Debian may report "unknown"
# Change the req_dm_ver="2.3.1"; line to req_dm_ver="2.3.4"; due to new unixodbc version. @see https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=818407
editor ./install.sh
./install.sh install
# Resulting:
# /opt/microsoft/msodbcsql/
# /etc/odbcinst.ini

# Add PHP 7 repository
cd
# for Debian jessie:
echo "deb http://packages.dotdeb.org jessie all" > /etc/apt/sources.list.d/dotdeb.list
wget -nv https://www.dotdeb.org/dotdeb.gpg
apt-key add dotdeb.gpg
apt-get update

# (1) Install PDO driver from pecl
apt-get install unixodbc-dev php7.0-dev php-pear
pecl install pdo_sqlsrv
echo -e "; priority=20\nextension=pdo_sqlsrv.so" > /etc/php/7.0/mods-available/pdo_sqlsrv.ini
phpenmod pdo_sqlsrv

# (2) Or build PDO driver with phpize
apt-get install unixodbc-dev php7.0-dev
cd /usr/local/src/
wget -nv https://github.com/Microsoft/msphpsql/archive/PHP-7.0-Linux.tar.gz
tar -xf PHP-7.0-Linux.tar.gz
cd msphpsql-PHP-7.0-Linux/source/
cp -r shared/ pdo_sqlsrv/
cd pdo_sqlsrv/
phpize
./configure CXXFLAGS=-std=c++11
make
#make install
make "INSTALL=$(pwd)/build/shtool install -c --mode=0644" install
echo -e "; priority=20\nextension=pdo_sqlsrv.so" > /etc/php/7.0/mods-available/pdo_sqlsrv.ini
phpenmod pdo_sqlsrv

# (3) Or download precompiled pdo_sqlsrv extension binaries
cd /usr/local/src/
wget https://github.com/Microsoft/msphpsql/releases/download/4.0.8-Linux/Ubuntu15.tar
tar -xf Ubuntu15.tar
cp ./Ubuntu15/php_pdo_sqlsrv_7_nts.so /usr/lib/php/20151012/
echo -e "; priority=20\nextension=php_pdo_sqlsrv_7_nts.so" > /etc/php/7.0/mods-available/pdo_sqlsrv.ini
phpenmod pdo_sqlsrv

# Test
cd
cat > sqlsrv.php <<"EOF"
<?php
$db = new PDO("sqlsrv:server=tcp:1.2.3.4,1433", "user", "pass");
$stmt = $db->prepare("SELECT @@version;");
$stmt->execute();
while ($row = $stmt->fetch()) {
    print_r($row);
}
EOF
php sqlsrv.php