Setting up the SQLSRV extension for PHP 7.2 on Ubuntu 18.04.2 LTS

I recently needed to access a SQL Server from PHP for a project I was working on, after some online research, I was able to get the extension up & running on the server.

These are the steps I took to get it setup:

First things first, I started by installing some default packages in addition to Apache2 and the latest PHP version available (in this case, it defaulted to PHP 7.2):

sudo apt-get install vim curl git zip unzip htop apache2 php libapache2-mod-php php-mysql

I then found this online tutorial that got me started:

curl -s | sudo apt-key add -
sudo bash -c "curl -s > /etc/apt/sources.list.d/mssql-release.list"
sudo apt-get update
sudo ACCEPT_EULA=Y apt-get -y install msodbcsql17 mssql-tools
sudo apt-get -y install unixodbc-dev

however, the pecl7.X-sp command didn’t worked, so after some more online research, found the way to make it work:

sudo apt-get -y install gcc g++ make autoconf libc-dev pkg-config
sudo apt-get install php-pear php-dev
sudo pecl install sqlsrv
sudo pecl install pdo_sqlsrv

at this point, I had the extension built and ready to go, I then proceeded to create the necessary .ini files so that PHP can pick it up:

sudo bash -c "echo > /etc/php/7.2/mods-available/sqlsrv.ini"
sudo ln -s /etc/php/7.2/mods-available/sqlsrv.ini /etc/php/7.2/apache2/conf.d/sqlsrv.ini
sudo ln -s /etc/php/7.2/mods-available/sqlsrv.ini /etc/php/7.2/cli/conf.d/sqlsrv.ini
sudo bash -c "echo > /etc/php/7.2/mods-available/pdo_sqlsrv.ini"
sudo ln -s /etc/php/7.2/mods-available/pdo_sqlsrv.ini /etc/php/7.2/apache2/conf.d/pdo_sqlsrv.ini
sudo ln -s /etc/php/7.2/mods-available/pdo_sqlsrv.ini /etc/php/7.2/cli/conf.d/pdo_sqlsrv.ini

and final step, was to restart Apache2:

sudo systemctl restart apache2

and we are good to go!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.