当我尝试在连接到 Microsoft SQL Server 数据库的 Elastic Beanstalk PHP 平台上部署应用程序时,如何解决收到的错误?

上次更新日期:2021 年 8 月 2 日

我尝试在连接到 Microsoft SQL Server 数据库的 AWS Elastic Beanstalk PHP 平台上部署应用程序。然后,我收到类似于以下内容的错误:“PHP 致命错误:未捕获错误:调用 /var/app/Currentt/dB/ 中未定义的函数 sqlsrv_connect ()。”

简短描述

若要将 PHP 连接到 Microsoft SQL Server 数据库,您必须首先安装并配置 SQLSRV 库及其 PDO 扩展。默认情况下不会安装和配置库和扩展。

要解决此错误,您可以使用 .ebextensions 配置文件在 Amazon Elastic Compute Cloud (Amazon EC2) 实例中运行脚本。该脚本将执行以下操作:

  • 为 Microsoft SQL Server 安装正确的驱动程序和工具
  • 启用所需的 PHP 库和扩展

重要提示:以下解决方法中使用的 .ebextensions 文件仅适用于 Amazon Linux 1 和 Amazon Linux 2 Amazon Machine Image (AMI) 实例。.ebextensions 文件不适用于 Windows 或 Elastic Beanstalk 中的自定义 Ubuntu AMI 实例。

解决方法

注意:以下解决方法适用于任何 PHP 平台版本的 Elastic Beanstalk 环境。

1.    在应用程序包的 root 目录中,创建一个名为 .ebextensions 的目录。

2.    创建一个 .ebextensions 配置文件,如 .ebextensions/pdo_sqlsrv.config。例如:

###################################################################################################
#### 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.    创建一个包含步骤 1 中的 .ebextensions 文件的应用程序源包。

4.    部署已更新的 Elastic Beanstalk 应用程序


这篇文章对您有帮助吗?


您是否需要账单或技术支持?