Skip to content

Instantly share code, notes, and snippets.

@RatserX
Last active February 6, 2023 11:33
Show Gist options
  • Save RatserX/38d86b56fdda91067193857e0199dbdf to your computer and use it in GitHub Desktop.
Save RatserX/38d86b56fdda91067193857e0199dbdf to your computer and use it in GitHub Desktop.
Setup the Microsoft ODBC Driver for SQL Server on Debian/Ubuntu with different PHP versions

Setup the Microsoft ODBC Driver for SQL Server on Debian/Ubuntu with different PHP versions

Prerequisites

Update the installed packages.

apt update

Install the Ondřej PHP repository.

apt install software-properties-commonsudo
add-apt-repository ppa:ondrej/php
apt update

Check that the repositories are correctly installed.

grep ^ /etc/apt/sources.list /etc/apt/sources.list.d/*

Step 1 - Install the PHP versions

In this case, PHP 7.0 and 7.1 will be installed.

# PHP 7.0
apt-get install php7.0 php7.0-dev php7.0-cli
# PHP 7.1
apt-get install php7.1 php7.1-dev php7.1-cli

Step 2 - Install PEAR

apt-get install php-pear

Step 3 - Update PECL

pecl channel-update pecl.php.net

Step 4 - Setup the PHP version to be used by PEAR

Set which version to use when compiling the extensions.

# PHP 7.0
pear config-set php_bin "/usr/lib/cgi-bin/php7.0"
# PHP 7.1
pear config-set php_bin "/usr/lib/cgi-bin/php7.1"

Force an upgrade to save changes.

pear upgrade --force

Step 5 - Install the Microsoft ODBC Driver for SQL Server

# Debian


sudo su 
curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -

#Download appropriate package for the OS version
#Choose only ONE of the following, corresponding to your OS version

#Debian 8
curl https://packages.microsoft.com/config/debian/8/prod.list > /etc/apt/sources.list.d/mssql-release.list

#Debian 9
curl https://packages.microsoft.com/config/debian/9/prod.list > /etc/apt/sources.list.d/mssql-release.list

exit
sudo apt-get update
sudo ACCEPT_EULA=Y apt-get install msodbcsql17
# optional: for bcp and sqlcmd
sudo ACCEPT_EULA=Y apt-get install mssql-tools
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bash_profile
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
source ~/.bashrc
# optional: for unixODBC development headers
sudo apt-get install unixodbc-dev


# Ubuntu


sudo su 
curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -

#Download appropriate package for the OS version
#Choose only ONE of the following, corresponding to your OS version

#Ubuntu 14.04
curl https://packages.microsoft.com/config/ubuntu/14.04/prod.list > /etc/apt/sources.list.d/mssql-release.list

#Ubuntu 16.04
curl https://packages.microsoft.com/config/ubuntu/16.04/prod.list > /etc/apt/sources.list.d/mssql-release.list

#Ubuntu 18.04
curl https://packages.microsoft.com/config/ubuntu/18.04/prod.list > /etc/apt/sources.list.d/mssql-release.list

#Ubuntu 18.10
curl https://packages.microsoft.com/config/ubuntu/18.10/prod.list > /etc/apt/sources.list.d/mssql-release.list

exit
sudo apt-get update
sudo ACCEPT_EULA=Y apt-get install msodbcsql17
# optional: for bcp and sqlcmd
sudo ACCEPT_EULA=Y apt-get install mssql-tools
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bash_profile
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
source ~/.bashrc
# optional: for unixODBC development headers
sudo apt-get install unixodbc-dev

Step 6 - Configure the Update Alternatives for PHP

Use the following command to choose the PHP version to be used as an alternative.

update-alternatives --config php-config

Step 7 - Install the SQL Server PHP extension from PECL

pecl install pdo_sqlsrv
pecl install sqlsrv

Step 8 - Check the extension

Use the following command to find where the extension was installed.

pear config-show | grep ext_dir

If necessary, move the extension to the correct PHP extension folder and add the extension to the corresponding php.ini file.

extension=sqlsrv.so
extension=pdo_sqlsrv.so

Step 9 - Configure the SQL Server PHP extension

# PHP 7.0
printf "; priority=20\nextension=sqlsrv.so\n" > /etc/php/7.0/mods-available/sqlsrv.ini
printf "; priority=30\nextension=pdo_sqlsrv.so\n" > /etc/php/7.0/mods-available/pdo_sqlsrv.ini
phpenmod -v 7.0 sqlsrv pdo_sqlsrv
# PHP 7.1
printf "; priority=20\nextension=sqlsrv.so\n" > /etc/php/7.1/mods-available/sqlsrv.ini
printf "; priority=30\nextension=pdo_sqlsrv.so\n" > /etc/php/7.1/mods-available/pdo_sqlsrv.ini
phpenmod -v 7.1 sqlsrv pdo_sqlsrv

Step 10 - Restart Apache

/etc/init.d/apache2 reload

Troubleshooting


References

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment