Install MSSQL plugin for PHP on Ubuntu with Docker
If you need to connect to a Microsoft SQL Server in PHP on Ubuntu, you need to install a pdo_sqlsrv PHP module. In this article, I’ll show my steps that create such environment with Docker.
According to the document from the Microsoft, you’ll need two tools for installing ODBC driver of MS SQL, which are msodbcsql and mssql-tools.
Before the installation, you need to specify the repository location and add the corresponding public key into the apt key rings.
After install these pre-requirements, you’ll need PDO (PHP Data Object) driver for MS SQL for enable the PHP support. You can find information in detail from Microsoft’s GitHub repository. But since we don’t need a SQL server inside a Docker, so we can skip this step and install the PDO driver here.
You can install MS SQL PDO driver in several ways: pecl, phpize (build from the source code) and from pre-build binary of share object module. I’ll use pre-build binary in the following example.
Here’s very straightforward process that downloads the binary tarball and decompress it then copy two share object files to PHP module directory. Be aware there’s a trick that you need enable and setup the system locale in order to avoid an error message like “PHP Fatal error: pdo_sqlsrv_db_handle_factory: Unknown exception caught”. You may refer to the discussions in this thread.
That’s it. You’ve done the part of the install of Microsoft SQL Server support on Ubuntu Xenial and Docker. Once you complete your configuration, try use phpinfo(); and you’ll get a result of an enabled pdo_sqlsrv module.