如何解決在連線至 Microsoft SQL Server 資料庫的 Elastic Beanstalk PHP 平台上部署應用程式時,我所收到的 PHP 嚴重錯誤?

3 分的閱讀內容
0

在連線至 Microsoft SQL Server 資料庫的 AWS Elastic Beanstalk PHP 平台上部署應用程式時,我收到一個 PHP 嚴重錯誤。

簡短說明

在連線至 Microsoft SQL Server 資料庫的 Elastic Beanstalk PHP 平台上部署應用程式時,您可能會收到以下錯誤:

"PHP Fatal error: Uncaught Error: Call to undefined function sqlsrv_connect() in /var/app/current/DB/ (PHP 嚴重錯誤:無法識別的錯誤:在 /var/app/current/DB/ 呼叫未定義的函數 sqlsrv_connect())"

若要將 PHP 連線至 Microsoft SQL Server 資料庫,您必須先安裝並設定 SQLSRV 程式庫及其 PDO 延伸功能。依預設,系統不會安裝及設定此程式庫和延伸功能。

若要安裝 SQLSRV 程式庫和 PDO 延伸功能,您可以使用 .ebextensions 組態檔在 Amazon Elastic Compute Cloud (Amazon EC2) 執行個體中執行指令碼。該指令碼會執行下列動作:

  • 安裝適用於 Microsoft SQL Server 的正確驅動程式和工具
  • 啟用所需的 PHP 程式庫和延伸功能

解決方法

**注意:**下列步驟適用於任何 PHP 平台版本的 Elastic Beanstalk 環境。

1.    在應用程式套件組合的根目錄中,建立名為 .ebextensions 的目錄。

2.    建立一個 .ebextensions 組態檔,例如下列 .ebextensions/pdo_sqlsrv.config. 檔案:

重要事項: 以下解決方法所用的 .ebextensions 檔案僅適用於 Amazon Linux 1 和 Amazon Linux 2 Amazon Machine Image (AMI) 執行個體。**.ebextensions ** 檔案不適用於 Windows 或 Elastic Beanstalk 中的自訂 Ubuntu AMI 執行個體。

###################################################################################################
#### 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
      set -x

      # 0. EXIT if pdo_sqlsrv is already installed
      if php -m | grep -q 'pdo_sqlsrv'
      then
        echo 'pdo_sqlsrv is already installed'
      else
        # 1. Install libtool-ltdl-devel
        yum -y install libtool-ltdl-devel

        # 2. Register the Microsoft Linux repository
        wget https://packages.microsoft.com/config/rhel/8/prod.repo -O /etc/yum.repos.d/msprod.repo

        # 3. 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

        # 4. 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"

        # 5. 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.    建立一個應用程式來源套件組合,其中包含您從步驟 2 建立的 .ebextensions 檔案。

4.    部署更新後的 Elastic Beanstalk 應用程式

AWS 官方
AWS 官方已更新 1 年前