Question
It is required to connect to Microsoft SQL Server (MS SQL) databases via PHP script on a Plesk for Linux server.
What extension should be installed and how it can be done?
Answer
PHP extensions sqlsrv
and pdo_sqlsrv
should be installed and enabled for PHP handlers:
Warning: These extensions were not properly tested with Plesk and are not officially supported. Perform all operations at your own risk.
Warning: These extensions do not yet support operating systems AlmaLinux/RHEL 9, Debian 12, Ubuntu 24.04.
Note: These extensions are only for PHP 7.x and 8.x.
Instructions cover only operating systems supported both by Plesk and PHP extensions sqlsrv
and pdo_sqlsrv
:
-CentOS/RHEL/CloudLinux 7.x
-AlmaLinux/RHEL/CloudLinux/Rocky Linux 8.x
-Debian 10, 11
-Ubuntu 18.04, 20.04, 22.04
-
Connect to the server using SSH.
-
Install necessary packages:
-
on CentOS/AlmaLinux/RHEL/CloudLinux/Rocky Linux:
# yum install unixODBC-devel make gcc-c++ gcc autoconf automake libtool-ltdl.x86_64 libtool-ltdl-devel.x86_64 plesk-phpXX-devel
Note: "XX" in
plesk-phpXX-devel
should be replaced with the PHP version. For example, "74" for PHP 7.4, or "82" for PHP 8.2. -
on Ubuntu and Debian:
# apt install apt-transport-https make gcc g++ unixodbc unixodbc-dev plesk-phpXX-dev
Note: "XX" in
plesk-phpXX-dev
should be replaced with the PHP version. For example, "74" for PHP 7.4, or "82" for PHP 8.2.
-
-
Install Microsoft ODBC driver for SQL Server on which PHP extensions
sqlsrv
andpdo_sqlsrv
rely on to handle the low-level communication with SQL Server:-
on CentOS/AlmaLinux/RHEL/CloudLinux/Rocky Linux:
# curl https://packages.microsoft.com/config/rhel/X/prod.repo > /etc/yum.repos.d/mssql-release.repo
# yum remove unixODBC-utf16 unixODBC-utf16-devel
# ACCEPT_EULA=Y yum install msodbcsql18Note: replace "X" in the
curl
command with "7" or "8" depending on the major version of used operating system. -
on Ubuntu and Debian:
Note: The below example is for Ubuntu 22.04. If another Ubuntu or Debian version is used, replace the highlighted URL part accordingly, with, for example, "ubuntu/20.04" for Ubuntu 20.04, or "debian/10" for Debian 10.
# curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
# curl https://packages.microsoft.com/config/ubuntu/22.04/prod.list > /etc/apt/sources.list.d/mssql-release.list
# apt update
# ACCEPT_EULA=Y apt install msodbcsql18
-
-
Install PHP extensions
sqlsrv
andpdo_sqlsrv
:-
for PHP versions 7.3 and later in Plesk Obsidian 18.0.49 and later: in the Plesk interface by following instructions from the article How to install PECL packages in Plesk?
Note: Extensions can be installed using only their names
sqlsrv
andpdo_sqlsrv
only for PHP 8.1-8.3.
For PHP versions 7.3-8.0, specific versions of the extensions must be specified in a dialog window which appears after clicking Install package:sqlsrv-5.9.0
for PHP 7.3sqlsrv-5.10.1
for PHP 7.4sqlsrv-5.11.1
for PHP 8.0sqlsrv-5.11.1
on Ubuntu 18.04 and with PHP 8.1-8.3.Note: Plesk PHP handler 7.3 is available only on CentOS/RHEL/CloudLinux 7.x, AlmaLinux/RHEL/CloudLinux/Rocky Linux 8.x, Ubuntu 18.04, 20.04, and Debian 10.
-
for PHP versions 7.0-7.2:
# /opt/plesk/php/7.X/bin/pecl install pdo_sqlsrv-5.X.X
# echo "extension=pdo_sqlsrv.so" > /opt/plesk/php/7.X/etc/php.d/pdo_sqlsrv.ini
# /opt/plesk/php/7.X/bin/pecl install sqlsrv-5.X.X
# echo "extension=sqlsrv.so" >…
-