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

上次更新时间:2020 年 10 月 21 日

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

简短描述

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

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

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

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

解决方法

注意:以下解决方法适用于 PHP 平台版本为 7.3 的 Elastic Beanstalk 环境,并且在其他 PHP 平台版本上可能无法正常工作。

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:
  01_pdo_sqlsrv:
    command: |
      # 0. EXIT if pdo_sqlsrv is already installed
      if php -m | grep -q 'pdo_sqlsrv'; then echo 'hi'; fi;

      # 1. Register the Microsoft Linux repository
      curl https://packages.microsoft.com/config/centos/7/prod.repo | tee /etc/yum.repos.d/mssql-tools.repo;

      # 2. Install MSSQL and tools
      ACCEPT_EULA=N yum install mssql-tools 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.

      # 3. Update the PEAR Archive_Tar package and update PEAR
      curl https://raw.githubusercontent.com/pear/Archive_Tar/1.4.3/Archive/Tar.php > `pear config-get php_dir`/Archive/Tar.php;
      pear upgrade -f Archive_Tar; # Don't forget to actually update it

      # 4. Install SQLSRV and its PDO extension and configure it correctly
      pear config-set php_ini ''; # Disable PECL's php.ini update
      pecl7 install sqlsrv pdo_sqlsrv;
      chmod 0755 `pear config-get ext_dir`/sqlsrv.so; # Fix perms
      chmod 0755 `pear config-get ext_dir`/pdo_sqlsrv.so; # Fix perms
      echo "extension=sqlsrv.so" | tee /etc/php.d/sqlsrv.ini;
      echo "extension=pdo_sqlsrv.so" | tee /etc/php.d/pdo_sqlsrv.ini;
      pear config-set php_ini /etc/php.ini; # Reenable PECL's php.ini update

      # 5. Restart Apache service
      service httpd restart;

3. 创建一个包含步骤 1 中的 .ebextensions 文件的应用程序源包。

4. 部署已更新的 Elastic Beanstalk 应用程序。</p