Early 2019 MS SQL and Laravel

I never imagined my first and second blog post would involve MS SQL.

At work we recently had another developer jump into our Laravel projects and setting up the development environment (Homestead) was a breeze. Getting MSSQL drivers installed, was a bit annoying.

We followed the directions line for line from the Microsoft PHP website for PHP, but were still experiencing the ‘driver not found’ error.

What ended up working for our Laravel 5.6 app, running on PHP 7.2, was downloading the compiled PHP extensions directly from Microsoft.

Here are the steps we followed…


#!/bin/sh

PHP_MSSQL_DRIVERS=Ubuntu18-7.2



# Download and extract phpmysql drivers

sudo wget “https://github.com/Microsoft/msphpsql/releases/download/v5.3.0/${PHP_MSSQL_DRIVERS}.tar” -O – | tar -x



# Change Directory

cd $PHP_MSSQL_DRIVERS



# Change owner/group

sudo chown root:root php_pdo_sqlsrv_72_nts.so

sudo chown root:root php_sqlsrv_72_nts.so

# Modify permissions

sudo chmod 644 php_pdo_sqlsrv_72_nts.so

sudo chmod 644 php_sqlsrv_72_nts.so



# Move files

sudo mv php_pdo_sqlsrv_72_nts.so /usr/lib/php/20170718

sudo mv php_sqlsrv_72_nts.so /usr/lib/php/20170718



# Cleanup

cd .. && sudo rm -rf $PHP_MSSQL_DRIVERS



# Create new .ini file

sudo touch /etc/php/7.2/mods-available/pdo_sqlsrv.ini

echo “extension=php_pdo_sqlsrv_72_nts.so” | sudo tee -a /etc/php/7.2/mods-available/pdo_sqlsrv.ini > /dev/null



# Create Symlinks

sudo ln -sfn /etc/php/7.2/mods-available/pdo_sqlsrv.ini /etc/php/7.2/cli/conf.d/20-pdo_sqlsrv.ini

sudo ln -sfn /etc/php/7.2/mods-available/pdo_sqlsrv.ini /etc/php/7.2/fpm/conf.d/20-pdo_sqlsrv.ini



# Enable PHP extension

echo “extension=php_sqlsrv_72_nts.so” | sudo tee -a /etc/php/7.2/cli/php.ini > /dev/null

echo “extension=php_sqlsrv_72_nts.so” | sudo tee -a /etc/php/7.2/fpm/php.ini > /dev/null



# Install Microsoft ODBC Driver 17 for SQL Server requirements

curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add –

sudo sh -c ‘curl https://packages.microsoft.com/config/ubuntu/18.04/prod.list > /etc/apt/sources.list.d/mssql-release.list’

# Update package lists

sudo apt-get update

# Install Microsoft ODBC Driver

yes Y | sudo ACCEPT_EULA=Y apt-get install msodbcsql17



# Restart services

sudo service php7.2-fpm restart

sudo service nginx restart

We found this from an issue on github, here.

Hope spreading the bash helps someone else!

Leave a Reply