How do I resolve the error that I receive when I try to deploy an application on an Elastic Beanstalk PHP platform that connects to a Microsoft SQL Server database?

4 minute read
0

I try to deploy application on an AWS Elastic Beanstalk PHP platform that connects to a Microsoft SQL Server database. Then, I receive an error similar to the following: "PHP Fatal error: Uncaught Error: Call to undefined function sqlsrv_connect() in /var/app/current/DB/."

Short description

To connect PHP to a Microsoft SQL Server database, you must first install and configure the SQLSRV library and its PDO extension. The library and extension aren't installed and configured by default.

To resolve this error, you can use a .ebextensions configuration file that runs a script in your Amazon Elastic Compute Cloud (Amazon EC2) instances. The script does the following:

  • Installs the correct driver and tools for Microsoft SQL Server
  • Enables the required PHP libraries and extensions

Important: The .ebextensions file used in the following resolution is applicable only for Amazon Linux 1 and Amazon Linux 2 Amazon Machine Image (AMI) instances. The .ebextensions file doesn't apply to Windows or custom Ubuntu AMI instances in Elastic Beanstalk.

Resolution

Note: The following resolution applies to an Elastic Beanstalk environment with any PHP platform versions.

1.    In the root of your application bundle, create a directory named .ebextensions.

2.    Create a .ebextensions configuration file, such as .ebextensions/pdo_sqlsrv.config. For example:

###################################################################################################
#### Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
#### 
#### Permission is hereby granted, free of charge, to any person obtaining a copy of this
#### software and associated documentation files (the "Software"), to deal in the Software
#### without restriction, including without limitation the rights to use, copy, modify,
#### merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
#### permit persons to whom the Software is furnished to do so.
###################################################################################################

###################################################################################################
#### THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
#### INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
#### PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
#### HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
#### OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
#### SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
###################################################################################################

commands:
  install_mssql:
    command: |
      #!/bin/bash

      # 0. EXIT if pdo_sqlsrv is already installed
      if php -m | grep -q 'pdo_sqlsrv'
      then
        echo 'pdo_sqlsrv is already installed'
      else
        # 1. Register the Microsoft Linux repository
        wget https://packages.microsoft.com/config/rhel/8/prod.repo -O /etc/yum.repos.d/msprod.repo

        # 2. Install MSSQL and tools
        ACCEPT_EULA=N yum install mssql-tools msodbcsql17 unixODBC-devel -y --disablerepo=amzn*
        # The license terms for this product can be downloaded from http://go.microsoft.com/fwlink/?LinkId=746949 and found in /usr/share/doc/mssql-tools/LICENSE.txt . By changing "ACCEPT_EULA=N" to "ACCEPT_EULA=Y", you indicate that you accept the license terms.
        echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bash_profile
        echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
        source ~/.bashrc

        # 3. Install SQLSRV and its PDO extension, and stop pecl/pecl7 from overwriting php.ini
        cp -f "/etc/php.ini" "/tmp/php.ini.bk"
        pecl7 install sqlsrv pdo_sqlsrv || pecl install sqlsrv pdo_sqlsrv
        cp -f "/tmp/php.ini.bk" "/etc/php.ini"

        # 4. Manually add the extensions to the proper php.ini.d file and fix parameters
        sqlvar=$(php -r "echo ini_get('extension_dir');") && chmod 0755 $sqlvar/sqlsrv.so && chmod 0755 $sqlvar/pdo_sqlsrv.so
        echo extension=pdo_sqlsrv.so >> `php --ini | grep "Scan for additional .ini files" | sed -e "s|.*:\s*||"`/30-pdo_sqlsrv.ini
        echo extension=sqlsrv.so >> `php --ini | grep "Scan for additional .ini files" | sed -e "s|.*:\s*||"`/20-sqlsrv.ini
      fi

3.    Create an application source bundle that includes your .ebextensions file from step 1.

4.    Deploy your updated Elastic Beanstalk application.


AWS OFFICIAL
AWS OFFICIALUpdated 2 years ago