Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

lozadakb's avatar

Laravel Sail: SQL Server Connection Issues with SQLSRV and PDO_SQLSRV

Hello, I am currently facing an issue with connecting my Laravel project (deployed using Laravel Sail) to an SQL Server. Although I have successfully installed sqlsrv and pdo_sqlsrv (both extensions appear in phpinfo()), I receive an error whenever I attempt a connection.

Error Message:

Array ( [0] => Array ( [0] => 08001 [SQLSTATE] => 08001 [1] => -1 [code] => -1 [2] => [Microsoft][ODBC Driver 17 for SQL Server]SSL Provider: [error:0A000102:SSL routines::unsupported protocol] [message] => [Microsoft][ODBC Driver 17 for SQL Server]SSL Provider: [error:0A000102:SSL routines::unsupported protocol] ) [1] => Array ( [0] => 08001 [SQLSTATE] => 08001 [1] => -1 [code] => -1 [2] => [Microsoft][ODBC Driver 17 for SQL Server]Client unable to establish connection [message] => [Microsoft][ODBC Driver 17 for SQL Server]Client unable to establish connection ) )

I am trying to stablish conection like this:

$serverName = "server,port";
$connectionOptions = array(
			 "Database" => "datebase",
            "Uid" => "user",
            "PWD" => "password",
 );

//Establishes the connection
$conn = sqlsrv_connect($serverName, $connectionOptions);
if($conn === false) {
            die(print_r(sqlsrv_errors(), true));
}

Which seems to be an error with the TLS, this is the version of SQL server I'm trying to connect

SQL Server version

You might find the creation of the libltdl.la file unusual. However, this was the only method I found that allows sqlsrv and pdo_sqlsrv installation to proceed without errors.

Dockerfile:

FROM ubuntu:22.04

ENV DEBIAN_FRONTEND noninteractive
ENV TZ=UTC

LABEL maintainer="Taylor Otwell"

ARG WWWGROUP
ARG NODE_VERSION=16
ARG POSTGRES_VERSION=14

WORKDIR /var/www/html

RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

RUN apt-get update \
    && apt-get install -y gnupg gosu curl ca-certificates zip unzip git supervisor sqlite3 libcap2-bin libpng-dev python2 \
    && mkdir -p ~/.gnupg \
    && chmod 600 ~/.gnupg \
    && echo "disable-ipv6" >> ~/.gnupg/dirmngr.conf \
    && echo "keyserver hkp://keyserver.ubuntu.com:80" >> ~/.gnupg/dirmngr.conf \
    && gpg --recv-key 0x14aa40ec0831756756d7f66c4f4ea0aae5267a6c \
    && gpg --export 0x14aa40ec0831756756d7f66c4f4ea0aae5267a6c > /usr/share/keyrings/ppa_ondrej_php.gpg \
    && echo "deb [signed-by=/usr/share/keyrings/ppa_ondrej_php.gpg] https://ppa.launchpadcontent.net/ondrej/php/ubuntu jammy main" > /etc/apt/sources.list.d/ppa_ondrej_php.list \
    && apt-get update \
    && apt-get install -y php8.1-cli php8.1-dev \
       php8.1-pgsql php8.1-sqlite3 php8.1-gd \
       php8.1-curl \
       php8.1-imap php8.1-mysql php8.1-mbstring \
       php8.1-xml php8.1-zip php8.1-bcmath php8.1-soap \
       php8.1-intl php8.1-readline \
       php8.1-ldap \
       php8.1-msgpack php8.1-igbinary php8.1-redis php8.1-swoole \
       php8.1-memcached php8.1-pcov php8.1-xdebug \
    && php -r "readfile('https://getcomposer.org/installer');" | php -- --install-dir=/usr/bin/ --filename=composer \
    && curl -sLS https://deb.nodesource.com/setup_$NODE_VERSION.x | bash - \
    && apt-get install -y nodejs \
    && npm install -g npm \
    && curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | gpg --dearmor | tee /usr/share/keyrings/yarn.gpg >/dev/null \
    && echo "deb [signed-by=/usr/share/keyrings/yarn.gpg] https://dl.yarnpkg.com/debian/ stable main" > /etc/apt/sources.list.d/yarn.list \
    && curl -sS https://www.postgresql.org/media/keys/ACCC4CF8.asc | gpg --dearmor | tee /usr/share/keyrings/pgdg.gpg >/dev/null \
    && echo "deb [signed-by=/usr/share/keyrings/pgdg.gpg] http://apt.postgresql.org/pub/repos/apt jammy-pgdg main" > /etc/apt/sources.list.d/pgdg.list \
    && apt-get update \
    && apt-get install -y yarn \
    && apt-get install -y mysql-client \
    && apt-get install -y postgresql-client-$POSTGRES_VERSION \
    && apt-get -y autoremove \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

RUN setcap "cap_net_bind_service=+ep" /usr/bin/php8.1

RUN groupadd --force -g $WWWGROUP sail
RUN useradd -ms /bin/bash --no-user-group -g $WWWGROUP -u 1337 sail

RUN 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

RUN apt-get update 
RUN ACCEPT_EULA=Y apt-get install -y msodbcsql17
RUN ACCEPT_EULA=Y apt-get install -y mssql-tools
RUN apt-get install -y unixodbc-dev
RUN apt-get install -y gcc
RUN apt-get install -y musl-dev
RUN apt-get install -y make

RUN apt-get update
RUN apt-get install -y libltdl-dev

# Create file libltdl.la in /usr/lib/x86_64-linux-gnu/libltdl.a
# libltdl.la was remove in libtool version (2.4.6-15), I know it is not recommended but was the only way I found to run install sqlsrv and pdo_sqlsrv
RUN echo -e "# libltdl.la - a libtool library file\n\
# Generated by libtool (GNU libtool) 2.4.6 Debian-2.4.6-14\n\
#\n\
# Please DO NOT delete this file!\n\
# It is necessary for linking the library.\n\
\n\
# The name that we can dlopen(3).\n\
dlname='libltdl.so.7'\n\
\n\
# Names of this library.\n\
library_names='libltdl.so.7.3.1 libltdl.so.7 libltdl.so'\n\
\n\
# The name of the static archive.\n\
old_library='libltdl.a'\n\
\n\
# Linker flags that cannot go in dependency_libs.\n\
inherited_linker_flags=''\n\
\n\
# Libraries that this one depends upon.\n\
dependency_libs=' -ldl'\n\
\n\
# Names of additional weak libraries provided by this library\n\
weak_library_names=''\n\
\n\
# Version information for libltdl.\n\
current=10\n\
age=3\n\
revision=1\n\
\n\
# Is this an already installed library?\n\
installed=yes\n\
\n\
# Should we warn about portability when linking against -modules?\n\
shouldnotlink=no\n\
\n\
# Files to dlopen/dlpreopen\n\
dlopen=''\n\
dlpreopen=''\n\
\n\
# Directory that this library needs to be installed in:\n\
libdir='/usr/lib/x86_64-linux-gnu'" > /usr/lib/x86_64-linux-gnu/libltdl.la

RUN pecl install sqlsrv
RUN pecl install pdo_sqlsrv

RUN printf "; priority=20\nextension=sqlsrv.so\n" > /etc/php/8.1/mods-available/sqlsrv.ini
RUN printf "; priority=30\nextension=pdo_sqlsrv.so\n" > /etc/php/8.1/mods-available/pdo_sqlsrv.ini
RUN phpenmod sqlsrv pdo_sqlsrv 

COPY start-container /usr/local/bin/start-container
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
COPY php.ini /etc/php/8.1/cli/conf.d/99-sail.ini
RUN chmod +x /usr/local/bin/start-container

EXPOSE 8000

ENTRYPOINT ["start-container"]

Additional details about my setup:

  • Laravel version: Laravel Framework 9.52.7
  • Laravel Sail version: ^1.16
  • PHP version: 8.1.18 (cli)
  • sqlsrv version: 5.11.0
  • pdo_sqlsrv version: 5.11.0
  • ODBC driver version: ODBC Driver 17
0 likes
1 reply
lozadakb's avatar

After a considerable amount of time spent on research, I found a solution from a thread that involved modifying the OpenSSL configuration in the Dockerfile. Here's what I did:

After the RUN phpenmod sqlsrv pdo_sqlsrv line in the Dockerfile, I added the following lines:

RUN sed -i 's/CipherString = DEFAULT:@SECLEVEL=2//' /etc/ssl/openssl.cnf && \
     echo 'MinProtocol = TLSv1.0' >> /etc/ssl/openssl.cnf && \
     echo 'CipherString = DEFAULT@SECLEVEL=1' >> /etc/ssl/openssl.cnf

This change adjusts the TLS version to 1.0, enabling compatibility with lower versions of SQL Server. After making this modification in the Dockerfile, remember to run sail build --no-cache to rebuild the Docker image.

Please note that the order of the MinProtocol and CipherString lines is important and you should not change it. Also, please be aware that changing these settings effectively lowers the security level of the SSL/TLS connections in your Docker container, which might not be suitable for all scenarios.

Please or to participate in this conversation.