Applicable to:
- Plesk Onyx for Linux
- Plesk for Linux
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
-
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
-
Connect to the server via SSH.
-
Install necessary packages:
# apt-get install apt-transport-https make gcc g++ unixodbc unixodbc-dev plesk-php8X-dev
Note: "8X" is to be replaced with the PHP version. For example, "81" for PHP 8.1
-
Install the
msodbcsql
package:# 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-get update
# ACCEPT_EULA=Y apt-get install msodbcsql18 -
Add the
pdo_sqlsrv
andsqlsrv-5.8.1
modules:-
For PHP 8.1 provided by Plesk:
# /opt/plesk/php/8.1/bin/pecl install pdo_sqlsrv
# echo "extension=pdo_sqlsrv.so" > /opt/plesk/php/8.1/etc/php.d/pdo_sqlsrv.ini
# /opt/plesk/php/8.1/bin/pecl install sqlsrv
# echo "extension=sqlsrv.so" > /opt/plesk/php/8.1/etc/php.d/sqlsrv.ini -
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
-
-
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/8.X/bin/php -m | grep sqlsrv
pdo_sqlsrv
sqlsrvNote: "8X" is to be replaced with the PHP version. For example, "81" for PHP 8.1
-
Restart the PHP-FPM master process to apply changes to FPM sites
-
Connect to the server via SSH.
-
Install necessary packages:
# apt-get install apt-transport-https make gcc g++ unixodbc unixodbc-dev plesk-php8X-dev
Note: "8X" is to be replaced with the PHP version. For example, "81" for PHP 8.1
-
Install the
msodbcsql
package:# curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
# curl https://packages.microsoft.com/config/debian/11/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.1 provided by Plesk:
# /opt/plesk/php/8.1/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.1/bin/pecl install sqlsrv
# echo "extension=sqlsrv.so" > /opt/plesk/php/8.1/etc/php.d/sqlsrv.ini -
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
-
-
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/8.X/bin/php -m | grep sqlsrv
pdo_sqlsrv
sqlsrvNote: "8X" is to be replaced with the PHP version. For example, "81" for PHP 8.1
-
Restart the PHP-FPM master process to apply changes to FPM sites
-
Connect to the server via SSH.
-
Install the
msodbcsql
package:# curl https://packages.microsoft.com/config/rhel/7/prod.repo > /etc/yum.repos.d/mssql-release.repo
# yum remove unixODBC-utf16 unixODBC-utf16-devel
# ACCEPT_EULA=Y yum install msodbcsql18 -
Install necessary packages:
# yum install unixODBC-devel make gcc-c++ gcc autoconf automake plesk-phpXX-devel libtool-ltdl.x86_64 libtool-ltdl-devel.x86_64
Note: "XX" is to be replaced with the PHP version. For example, "74" for PHP 7.4 or "80" for PHP 8.0
-
Add the
pdo_sqlsrv
andsqlsrv-5.8.1
modules:-
For PHP 7.2 provided by Plesk:
# /opt/plesk/php/7.2/bin/pecl install pdo_sqlsrv-5.8.1
# echo "extension=pdo_sqlsrv.so" > /opt/plesk/php/7.2/etc/php.d/pdo_sqlsrv.ini
# /opt/plesk/php/7.2/bin/pecl install sqlsrv-5.8.1
# echo "extension=sqlsrv.so" > /opt/plesk/php/7.2/etc/php.d/sqlsrv.ini -
For later PHP 7.X and PHP 8.X versions provided by Plesk:
# /opt/plesk/php/X7.X/bin/pecl install pdo_sqlsrv
# echo "extension=pdo_sqlsrv.so" > /opt/plesk/php/X.X/etc/php.d/pdo_sqlsrv.ini
# /opt/plesk/php/X.X/bin/pecl install sqlsrv
# echo "extension=sqlsrv.so" > /opt/plesk/php/X.X/etc/php.d/sqlsrv.iniNote: "X.X" is to be replaced with the PHP version. For example, "7.4" for PHP 7.4 or "8.0" for PHP 8.0
-
-
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/X.X/bin/php -m | grep sqlsrv
pdo_sqlsrv
sqlsrvNote: "X.X" is to be replaced with the PHP version. For example, "7.4" for PHP 7.4 or "8.0" for PHP 8.0
- Restart the PHP-FPM master process to apply changes to FPM sites
Additional information
Instructions are taken from the Microsoft documentation page and adapted to Plesk PHP:
Installing the Microsoft ODBC Driver for SQL Server on Linux and macOS
It is also possible to use an older system version of PHP. For example, in order to connect to MSSQL on CentOS 7 with system PHP version 5.4, the following command needed to be executed:
# yum install epel-release
# yum install php-mssql
Comments
29 comments
Note in case someone stumbles across this article: I had to run the reread command for them to show up: https://www.danhendricks.com/2017/11/installing-microsoft-sql-server-php-extensions-plesk-onyx/
@Daniel, Thank you for your input! You are right, that command is necessary indeed. The article has been modified accordingly.
Hi,
can you make the same proccess for CENTOS?
i followed this: https://www.danhendricks.com/2017/11/installing-microsoft-sql-server-php-extensions-plesk-onyx/
and got some errors
Noam
@Noam, Hi!
The article was updated with the corresponding steps for CentOS 7.
Good morning,
I've installed sqlsrv extension for php 7.0 but I still have PDO Exception driver not found.
@Federica Della Monica
Hi!
Make sure pdo_mysql is also installed and enabled.
In case you will have difficulties, feel free to contact Plesk Support.
Hi!
pdo_mysql is installed and enabled.
See image below
@Federica Della Monica
Hi!
Due to issue complexity, I would suggest contacting Plesk Technical Support for deeper investigation.
Works also great with PHP 7.3 on Ubuntu / Debian:
2. Install necessary packages:
4. Add the
pdo_sqlsrv
andsqlsrv
modules:6. Make sure that the required modules are enabled:
Hint for Debian 9:
In Step 3 just adapt the repo url:
Hi @Linus!
Thanks so much for the input!
Trying this on Ubuntu18.04, Plesk Onyx, PHP7.2.
Have modified the CURL URL in #3 to reflect Ubuntu18.04
Struggling with the pecl install commands in #4 which fail with
running: make
sh: 1: make: not found
ERROR: `make' failed
After adding the Make package the install completed successfully. Is the expectation that Make is available in a default Plesk Onyx install?
Hello @Peter,
Plesk is not in charge of this package installation, this depends on the used OS.
Thank you for sharing your user experience, it may be helpful for other Pleskians.
Hello..
how to install the sqlsrv extension for php 5.6 with ubuntu 16
Hello, sqlsrv no longer supports PHP 5.x. Microsoft support who provides sqlsrv decided to drop it, so it is not possible currently to install it for PHP 5.x
i do this answer on my plesk latest
but now when i want to create a database i got this error:
You seems like specific incorrect username and password for the database in PHP code. Plesk don't user 'root' user for the database.
Can someone explain to me why CentOS6 is not supported (anymore) or if there is an option to install it on CentOS6 ?
Hello Sonny Spaan
Only OS for which the solution was tested are listed in this article.
For CentOS 6 CentOS 7 solution also has to work. However, the recommendation is to try a solution on the test clone on your production environment first to avoid downtimes.
In case of issues, you may submit a support request.
this is not working
can somebody please help??? Why i can't even pay for support to do it? can somebody help?
those instructions don't work (debian).
thank you
Hi Peter Shaw, I don't know if you've already solve this.
You need to install 'make' package at your server:
apt install make
Akatsouros you may create a request to our Professional Services Team to implement this for you on paid basis: https://www.plesk.com/professional-services/administrative-services
#/opt/plesk/php/7.2/bin/pecl install pdo_sqlsrv
when run
ERROR
PHP Warning: PHP Startup: Unable to load dynamic library 'pdo_sqlsrv.so' (tried: /opt/plesk/php/7.2/lib64/php/modules/pdo_sqlsrv.so (/opt/plesk/php/7.2/lib64/php/modules/pdo_sqlsrv.so: cannot open shared object file: No such file or directory), /opt/plesk/php/7.2/lib64/php/modules/pdo_sqlsrv.so.so (/opt/plesk/php/7.2/lib64/php/modules/pdo_sqlsrv.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
PHP Warning: PHP Startup: Unable to load dynamic library 'sqlsrv.so' (tried: /opt/plesk/php/7.2/lib64/php/modules/sqlsrv.so (/opt/plesk/php/7.2/lib64/php/modules/sqlsrv.so: cannot open shared object file: No such file or directory), /opt/plesk/php/7.2/lib64/php/modules/sqlsrv.so.so (/opt/plesk/php/7.2/lib64/php/modules/sqlsrv.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
Notice: Undefined variable: errstr in PEAR/Proxy.php on line 185
PHP Notice: Undefined variable: errstr in /opt/plesk/php/7.2/share/pear/PEAR/Proxy.php on line 185
Notice: Undefined variable: errno in PEAR/Proxy.php on line 185
PHP Notice: Undefined variable: errno in /opt/plesk/php/7.2/share/pear/PEAR/Proxy.php on line 185
No releases available for package "pecl.php.net/pdo_sqlsrv"
install failed
[root@host-185-19-93-51 ~]# /opt/plesk/php/7.2/bin/pecl install pdo_sqlsrv
PHP Warning: PHP Startup: Unable to load dynamic library 'pdo_sqlsrv.so' (tried: /opt/plesk/php/7.2/lib64/php/modules/pdo_sqlsrv.so (/opt/plesk/php/7.2/lib64/php/modules/pdo_sqlsrv.so: cannot open shared object file: No such file or directory), /opt/plesk/php/7.2/lib64/php/modules/pdo_sqlsrv.so.so (/opt/plesk/php/7.2/lib64/php/modules/pdo_sqlsrv.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
PHP Warning: PHP Startup: Unable to load dynamic library 'sqlsrv.so' (tried: /opt/plesk/php/7.2/lib64/php/modules/sqlsrv.so (/opt/plesk/php/7.2/lib64/php/modules/sqlsrv.so: cannot open shared object file: No such file or directory), /opt/plesk/php/7.2/lib64/php/modules/sqlsrv.so.so (/opt/plesk/php/7.2/lib64/php/modules/sqlsrv.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
Notice: Undefined variable: errstr in PEAR/Proxy.php on line 185
PHP Notice: Undefined variable: errstr in /opt/plesk/php/7.2/share/pear/PEAR/Proxy.php on line 185
Notice: Undefined variable: errno in PEAR/Proxy.php on line 185
PHP Notice: Undefined variable: errno in /opt/plesk/php/7.2/share/pear/PEAR/Proxy.php on line 185
No releases available for package "pecl.php.net/pdo_sqlsrv"
install failed
Hello Mehmet Erdoğan
It looks like the library was incorrectly installed or some step was missed.
Try these commands to add the missing configuration:
echo "extension = pdo_sqlsrv.so"> /opt/plesk/php/7.2/etc/php.d/pdo_sqlsrv.ini
echo "extension = sqlsrv.so"> /opt/plesk/php/7.2/etc/php.d/sqlsrv.ini
If this won't help rollback the proposed changes and submit support request.
For those wanting to use this on PHP 7.4 on Centos 7, I ran the following:
and tested it with:
Alex Gough, you are a hero, man!!
thank you!!
Alex Gough
thank youuu
for the actual setup on Centos 7 you need to install libtool-ltdl with
yum install libtool-ltdl.x86_64 libtool-ltdl-devel.x86_64
otherwise you'll gonna get:
/usr/bin/ld: cannot find -lltdl
collect2: error: ld returned 1 exit status
make: *** [pdo_sqlsrv.la] Error 1
ERROR: `make' failed
at the end of pecl install command.
Hello @Tech Team,
Thank you for your findings, Indeed the mentioned packages are required.
The article was updated.
Hi, I try to setup on Ubuntu 22.04 ad plesk 18.0.44 and I get error...
root@localhost:/# apt-get install apt-transport-https make gcc g++ unixodbc unixodbc-dev plesk-php74-dev
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package plesk-php74-dev
root@localhost:/#
Please sign in to leave a comment.