Question
It is required to connect to SQLServer (MSSQL) databases via PHP script on a Linux system.
What extension should be installed and how it can be done?
Answer
Modules sqlsrv
and pdo_sqlsrv
should be enabled for PHP engine.
Warning: These extensions were not properly tested with Plesk and are not officially supported. Perform all operations at your own risk.
Note: The installation of pecl/sqlsrv requires PHP version >= 7.2.0. sqlsrv
extension for PHP 7.1 was removed from pecl repository. Precompiled binaries for older sqlsrv
which support PHP 7.1 can be found at https://github.com/microsoft/msphpsql/releases/tag/v5.6.1. Precompiled binaries for older sqlsrv
which support PHP 7.0 can be found at https://github.com/microsoft/msphpsql/releases/tag/v5.3.0
Ubuntu 18.04/20.04 with Plesk PHP 8.0/7.3/7.4
-
Connect to the server via SSH.
-
Install necessary packages:
# apt-get install apt-transport-https make gcc g++ unixodbc unixodbc-dev plesk-php7X-dev
Note: "7X" is to be replaced with the PHP version. For example, "74" for PHP 7.4
-
Install the
msodbcsql
package:Note: This step is unique for each OS. The following example is for Ubuntu 18.04. If Debian is used, replaced the URL accordingly (for ie: "/debian/10/" or "/ubuntu/20.04/")
# curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
# curl https://packages.microsoft.com/config/ubuntu/18.04/prod.list > /etc/apt/sources.list.d/mssql-release.list
# apt-get update
# ACCEPT_EULA=Y apt-get install msodbcsql18 -
Add the
pdo_sqlsrv
andsqlsrv-5.8.1
modules:-
For PHP 8.0 provided by Plesk:
# /opt/plesk/php/8.0/bin/pecl install pdo_sqlsrv
# echo "extension=pdo_sqlsrv.so" > /opt/plesk/php/8.0/etc/php.d/pdo_sqlsrv.ini
# /opt/plesk/php/8.0/bin/pecl install sqlsrv
# echo "extension=sqlsrv.so" > /opt/plesk/php/8.0/etc/php.d/sqlsrv.ini -
For PHP 7.3/7.4 provided by Plesk:
# /opt/plesk/php/7.X/bin/pecl install pdo_sqlsrv
# 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
# echo "extension=sqlsrv.so" > /opt/plesk/php/7.X/etc/php.d/sqlsrv.ini
-
-
Reread PHP handlers:
# plesk bin php_handler --reread
- Reread the components properties to make modules visible in GUI:
# plesk sbin packagemng -sdf
-
Make sure that the required modules are enabled:
# /opt/plesk/php/7.X/bin/php -m | grep sqlsrv
pdo_sqlsrv
sqlsrvNote: "7X" is to be replaced with the PHP version. For example, "74" for PHP 7.4
-
Restart the PHP-FPM master process to apply changes to FPM sites
Ubuntu 22.04 with Plesk PHP 8.1/8.0
-
Connect to the server via SSH.
-
Install necessary packages:
# apt-get install apt-transport-https make gcc g++ unixodbc unixodbc-dev plesk…