How to Configure Microsoft® ODBC Driver 11 for SQL Server® on RedHat Linux with PHP

Sri picture Sri · Aug 30, 2013 · Viewed 20.4k times · Source

This is about how to install Microsoft® ODBC Driver 11 for SQL Server® on RedHat Linux with PHP

Answer

Sri picture Sri · Aug 30, 2013

Below are steps to install, configure and start using the Microsoft SQL Server ODBC Driver for Linux, and using it from PHP - it assumes that you have SQL Server already available and configured to accept connections over TCP/IP, also that you have some familiarity with Linux. First, the SQL Server (and the appropriate database) must be configured for Windows and SQL Server Authentication. This requires a restart of the SQL Server service if changed. In addition, the server must also have TCP/IP connections enabled with a static port defined (I will be using the default of 1433), and the firewall on the host for the SQL Server must allow connections to SQL Server on the static port.

Load necessary modules:

  1. Run the following command line to remove previous installations.

     yum remove php httpd php-odbc php-pear.noarch php-pecl-apc php-xml php-xmlrpc php-tidy     php-intl php-imap php-pecl-memcache glibc libuuid1 krb5 openssl gcc unixodbc
    
  2. To install new packages, run following command line (The installing user must have write privileges to the /opt directory by default.)

    yum install php httpd php-odbc php-pear.noarch php-pecl-apc php-xml php-xmlrpc php-tidy php-intl php-imap php-pecl-memcache glibc libuuid1 krb5 openssl gcc unixodbc
    
  3. Add these two lines to /etc/httpd/conf/httpd.conf

    SetEnv ODBCSYSINI /etc
    
    SetEnv ODBCINI /etc/odbc.ini
    

Load unixODBC

  1. Go to http://www.unixodbc.org/.

  2. Click the Download link (unixODBC-2.3.0), on the left side of the page.

  3. Click the Download link, on the next page, and save the file in '~/Download' folder

  4. On your Linux computer, execute the following command:

    cd ~/Downloads/
    
    tar xvzf unixODBC-2.3.0.tar.gz
    
  5. Change to the unixODBC-2.3.0 directory.

    cd unixODBC-2.3.0/
    
  6. At a command prompt, type the following command:

    CPPFLAGS="-DSIZEOF_LONG_INT=8"
    
  7. At a command prompt, type the following command:

    export CPPFLAGS
    
  8. At a command prompt, type the following command:

     ./configure --prefix=/usr --libdir=/usr/lib64 --sysconfdir=/etc --enable-gui=no --enable-drivers=no --enable-iconv --with-iconv-char-enc=UTF8 --with-iconv-ucode-enc=UTF16LE
    
  9. At a command prompt (logged in as root), type the following command

    make
    

    and press enter, and then

    make install
    

    and press enter.

Install Microsoft® ODBC Driver 11

  1. Run the following series of commands,

     wget http://download.microsoft.com/download/B/C/D/BCDD264C-7517-4B7D-8159- C99FC5535680/RedHat6/msodbcsql-11.0.2270.0.tar.gz
    and press enter, and then
    
    
     tar xzvf msodbcsql-11.0.2270.0.tar.gz
    

    and press enter, and then

     cd  msodbcsql-11.0.2270.0
    

    and press enter, and then

     ./install.sh install --lib-dir=/usr/local/lib64 --accept-license
    
     odbcinst -q -d -n "SQL Server Native Client 11.0"
    
  2. Now edit /etc/odbc.ini and add a section like this (change [server address] to your database server IP):

    [DSNname]
    Driver=SQL Server Native Client 11.0
    Description=My Sample ODBC Database Connection
    Trace=Yes
    Server=[server address]
    Port=1433
    Database=NSCDB_3
    
  3. Save it, and exit the editor. At a command prompt type:

    isql -v <DSN Name> <sql server authentication user name> <password>
    
  4. Next, we execute three commands from the shell. (these can take up to 15 seconds each). The third restarts the Apache web server.

    setsebool -P httpd_can_network_connect on
    setsebool -P httpd_can_network_connect_db on
    /etc/init.d/httpd restart
    

    if the installation was successful, you should see something like this:

    +---------------------------------------+
    | Connected!                            |
    |                                       |
    | sql-statement                         |
    | help [tablename]                      |
    | quit                                  |
    |                                       |
    +---------------------------------------+
    SQL>