Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP connect to MS SQL with SSL

Tags:

sql

php

ssl

ubuntu

What I mean to achieve is very simple. I want to connect to an external MS SQL database from a PHP script over a secure connection. This has however proven problematic and, with three hours put in to research so far, I am at a loss.

The platform for the client is Ubuntu, which means I can not use SQLSRV. The secure connection has been tested with different clients and it works fine. I am currently using PDO and DBlib to connect to the database, which also works fine.

I was not able to find any method to force a secure connection. I have tried multiple other drivers, to no avail.

What are my options?

Edit: I am left with the following FreeTDS logs...

config.c:543:   Got a match.
config.c:565:   host = 'XXXXXXXXXX'
config.c:595:   Found host entry XXXXXXXXXX.
config.c:599:   IP addr is XXXXXXXXXX.
config.c:565:   port = '1433'
config.c:565:   encryption = 'require'
config.c:565:   check certificate hostname = 'no'
config.c:629:   UNRECOGNIZED option 'check certificate hostname' ... ignoring.
config.c:565:   ca file = 'XXXXXXXXXX.pem'
config.c:629:   UNRECOGNIZED option 'ca file' ... ignoring.
like image 676
Samuel Willems Avatar asked Sep 26 '22 15:09

Samuel Willems


1 Answers

If you want to use PDO, you could set up PDO ODBC. You will need to setup the configuration files /etc/odbc.ini, /etc/odbcinst.ini and /etc/freetds/freetds.conf.

You'll also have to install unixodbc and freetds: apt-get install unixodbc tdsodbc.

You can see more info here: Connect PHP to MSSQL via PDO ODBC

EDIT: To enforce SSL in ODBC, add the Encrypt keyword and set it to true in your connection string. And setup your SQL Server to use SSL: https://support.microsoft.com/en-us/kb/316898

EDIT 2: According to the OP, adding encryption=require and check certificate hostname to freetds.config as per the following specification: http://www.freetds.org/userguide/freetdsconf.htm along with the above steps will fix the problem

like image 112
Don Rhummy Avatar answered Sep 28 '22 04:09

Don Rhummy