Skip to content

Install and configuration

Jenny Tam edited this page Aug 8, 2019 · 34 revisions

Contents

End to End Installation instructions

Linux and macOS Installation Tutorial for the Microsoft Drivers for PHP for SQL Server

Common installation and configuration issues

Please browse the Frequently Asked Questions

Docker files

Dockerfile for adding pdo_sqlsrv and sqlsrv using php-fpm to Ubuntu bionic image.

This is meant to be an example. It is not actively maintained.

FROM ubuntu:bionic

WORKDIR /tmp/

RUN export DEBIAN_FRONTEND=noninteractive && apt-get update && \
    apt-get -y install curl apt-transport-https \
    software-properties-common gnupg && \
    add-apt-repository ppa:ondrej/php -y && \
    apt-get update && \
    apt-get -y install \
    apt-utils \
    autoconf \
    libcurl4 \
    g++ \
    gcc \
    git \
    lcov \
    libxml2-dev \
    locales \
    make \
    php7.3 \
    php7.3-dev \
    php7.3-fpm \
    python-pip \
    re2c \
    unixodbc-dev \
    sed -i 's/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/g' /etc/locale.gen && \
    locale-gen en_US.UTF-8 && \ 
    curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add - && \
    curl https://packages.microsoft.com/config/ubuntu/18.04/prod.list > /etc/apt/sources.list.d/mssql-release.list && \
    apt-get -y update && \
    export ACCEPT_EULA=Y && apt-get -y install msodbcsql17 mssql-tools && \
    update-alternatives --set php /usr/bin/php7.3 
    
ENV PATH="/opt/mssql-tools/bin:${PATH}"	
ENV LANG='en_US.UTF-8' LANGUAGE='en_US:en' LC_ALL='en_US.UTF-8'

RUN pecl config-set php_ini /etc/php/7.3/fpm/php.ini && \
    pear config-set php_ini /etc/php/7.3/fpm/php.ini && \
    pecl channel-update pecl.php.net

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

The section below were contributions by various users. They are not officially tested.

Dockerfile for adding pdo_sqlsrv and sqlsrv to official php image.

Originally created by Diego Gullo

This uses Microsoft repos for msodbcsql to simplify the installation process.

FROM php:fpm

# Add Microsoft repo for Microsoft ODBC Driver 13 for Linux
RUN apt-get update && apt-get install -y \
    apt-transport-https \
    && curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add - \
    && curl https://packages.microsoft.com/config/debian/8/prod.list > /etc/apt/sources.list.d/mssql-release.list \
    && apt-get update

# Install Dependencies
RUN ACCEPT_EULA=Y apt-get install -y \
    unixodbc \
    unixodbc-dev \
    libgss3 \
    odbcinst \
    msodbcsql \
    locales \
    && echo "en_US.UTF-8 UTF-8" > /etc/locale.gen && locale-gen

# Install pdo_sqlsrv and sqlsrv from PECL. Replace pdo_sqlsrv-4.1.8preview with preferred version.
RUN pecl install pdo_sqlsrv-4.1.8preview sqlsrv-4.1.8preview \
    && docker-php-ext-enable pdo_sqlsrv sqlsrv

Dockerfile for getting pdo_sqlsrv for PHP 7.0 on Debian in 3 ways

Originally created by Viktor Szépe

# Prove that the Debian how-to works, please choose one of the 3 installation methods
# and optionally save the resulting extension: sudo cp /usr/lib/php/20151012/pdo_sqlsrv.so /opt/results/

FROM szepeviktor/jessie-build

ENV LC_ALL C
ENV DEBIAN_FRONTEND noninteractive

USER debian
WORKDIR /home/debian
VOLUME ["/opt/results"]

# Add PHP 7 repository
# for Debian jessie
# And System upgrade
RUN echo "deb http://packages.dotdeb.org jessie all" \
    | sudo tee /etc/apt/sources.list.d/dotdeb.list \
    && wget -qO- https://www.dotdeb.org/dotdeb.gpg \
    | sudo apt-key add - \
    && sudo apt-get update \
    && sudo apt-get upgrade -qq

# Install UnixODBC
# Compile odbc_config as it is not part of unixodbc package
RUN sudo apt-get install -y whiptail \
    unixodbc libgss3 odbcinst devscripts debhelper dh-exec dh-autoreconf libreadline-dev libltdl-dev \
    && dget -u -x http://http.debian.net/debian/pool/main/u/unixodbc/unixodbc_2.3.1-3.dsc \
    && cd unixodbc-*/ \
De    && dpkg-buildpackage -uc -us -B \
    && sudo cp -v ./exe/odbc_config /usr/local/bin/

# Fake uname for install.sh
RUN printf '#!/bin/bash\nif [ "$*" == "-p" ]; then echo "x86_64"; else /bin/uname "$@"; fi' \
    | sudo tee /usr/local/bin/uname \
    && sudo chmod +x /usr/local/bin/uname

# Microsoft ODBC Driver 13 for Linux
# Note: There's a copy of this tar on my hubiC
RUN wget -nv -O msodbcsql-13.0.0.0.tar.gz \
    "https://meetsstorenew.blob.core.windows.net/contianerhd/Ubuntu%2013.0%20Tar/msodbcsql-13.0.0.0.tar.gz?st=2016-10-18T17%3A29%3A00Z&se=2022-10-19T17%3A29%3A00Z&sp=rl&sv=2015-04-05&sr=b&sig=cDwPfrouVeIQf0vi%2BnKt%2BzX8Z8caIYvRCmicDL5oknY%3D" \
    && tar -xf msodbcsql-13.0.0.0.tar.gz \
    && cd msodbcsql-*/ \
    && ldd lib64/libmsodbcsql-13.0.so.0.0 \
    && sudo ./install.sh install --accept-license \
    && ls -l /opt/microsoft/msodbcsql/ \
    && odbcinst -q -d -n "ODBC Driver 13 for SQL Server"

# (1) Install PDO driver from pecl
RUN sudo apt-get install -y unixodbc-dev php7.0-dev php-pear \
    && sudo pecl channel-update pecl.php.net \
    && sudo pecl install pdo_sqlsrv \
    && printf "; priority=20\nextension=pdo_sqlsrv.so" \
    | sudo tee /etc/php/7.0/mods-available/pdo_sqlsrv.ini \
    && sudo phpenmod pdo_sqlsrv \
    && php --rextinfo pdo_sqlsrv \
    && sudo phpdismod pdo_sqlsrv \
    && sudo pecl uninstall pdo_sqlsrv

# (2) Or Build PDO driver with phpize
RUN sudo apt-get install -y unixodbc-dev php7.0-dev \
    && wget -nv "https://github.com/Microsoft/msphpsql/archive/PHP-7.0-Linux.tar.gz" \
    && tar -xf PHP-7.0-Linux.tar.gz \
    && cd msphpsql-PHP-7.0-Linux/source/ \
    && cp -r shared/ pdo_sqlsrv/ \
    && cd pdo_sqlsrv/ \
    && phpize \
    && ./configure CXXFLAGS=-std=c++11 \
    && make \
    && sudo make "INSTALL=$(pwd)/build/shtool install -c --mode=0644" install \
    && printf "; priority=20\nextension=pdo_sqlsrv.so" \
    | sudo tee /etc/php/7.0/mods-available/pdo_sqlsrv.ini \
    && sudo phpenmod pdo_sqlsrv \
    && php --rextinfo pdo_sqlsrv \
    && sudo phpdismod pdo_sqlsrv \
    && sudo rm -f /usr/lib/php/20151012/pdo_sqlsrv.so

# (3) Or Download precompiled pdo_sqlsrv extension binaries
RUN sudo apt-get install -y php7.0-cli \
    &&wget -nv "https://github.com/Microsoft/msphpsql/releases/download/4.0.8-Linux/Ubuntu15.tar" \
    && tar -xf Ubuntu15.tar \
    && sudo cp -v ./Ubuntu15/php_pdo_sqlsrv_7_nts.so /usr/lib/php/20151012/pdo_sqlsrv.so \
    && printf "; priority=20\nextension=pdo_sqlsrv.so" \
    | sudo tee /etc/php/7.0/mods-available/pdo_sqlsrv.ini \
    && sudo phpenmod pdo_sqlsrv \
    && php --rextinfo pdo_sqlsrv \
    && sudo phpdismod pdo_sqlsrv \
    && sudo rm -f /usr/lib/php/20151012/pdo_sqlsrv.so

Installing pdo_sqlsrv using Docker image php:7.1

Originally created by Remco

    FROM php:7.1-apache
    
    RUN apt-get install -y whiptail \
        unixodbc libgss3 odbcinst devscripts debhelper dh-exec dh-autoreconf libreadline-dev libltdl-dev \
        && dget -u -x http://http.debian.net/debian/pool/main/u/unixodbc/unixodbc_2.3.1-3.dsc \
        && cd unixodbc-*/ \
        && dpkg-buildpackage -uc -us -B \
        && cp -v ./exe/odbc_config /usr/local/bin/
    
    RUN printf '#!/bin/bash\nif [ "$*" == "-p" ]; then echo "x86_64"; else /bin/uname "$@"; fi' \
        | tee /usr/local/bin/uname \
        && chmod +x /usr/local/bin/uname
    
    RUN apt-get install -y wget \
        && wget -nv -O msodbcsql-13.0.0.0.tar.gz \
        "https://meetsstorenew.blob.core.windows.net/contianerhd/Ubuntu%2013.0%20Tar/msodbcsql-13.0.0.0.tar.gz?st=2016-10-18T17%3A29%3A00Z&se=2022-10-19T17%3A29%3A00Z&sp=rl&sv=2015-04-05&sr=b&sig=cDwPfrouVeIQf0vi%2BnKt%2BzX8Z8caIYvRCmicDL5oknY%3D" \
        && tar -xf msodbcsql-13.0.0.0.tar.gz \
        && cd msodbcsql-*/ \
        && ldd lib64/libmsodbcsql-13.0.so.0.0 \
        && ./install.sh install --accept-license \
        && ls -l /opt/microsoft/msodbcsql/ \
        && odbcinst -q -d -n "ODBC Driver 13 for SQL Server"
    
    RUN apt-get install -y apt-transport-https lsb-release ca-certificates \
        && wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg \
        && echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/php.list \
        && apt-get update
        
    RUN apt-get install -y unixodbc-dev php7.1-dev php-pear \
        && pecl channel-update pecl.php.net \
        && pecl install pdo_sqlsrv-4.1.6.1 \
        && printf "; priority=20\nextension=/usr/lib/php/20160303/pdo_sqlsrv.so" | tee /etc/php/7.1/mods-available/pdo_sqlsrv.ini \
        && echo "extension=pdo_sqlsrv.so" > /usr/local/etc/php/conf.d/pdo_sqlsrv.ini