Je reçois une erreur PHP fatale lorsque je déploie une application sur une plateforme PHP AWS Elastic Beanstalk qui se connecte à une base de données Microsoft SQL Server.
Brève description
Lorsque vous déployez une application sur une plateforme PHP Elastic Beanstalk qui se connecte à une base de données Microsoft SQL Server, vous pouvez recevoir l'erreur suivante :
« Erreur fatale ** PHP : Erreur non détectée : Appel à la fonction non définie sqlsrv \ _connect () dans /var/app/Current/DB/**»
Pour connecter PHP à une base de données Microsoft SQL Server, vous devez d'abord installer et configurer la ** bibliothèque ** SQLSRV et son extension ** PDO**. Par défaut, cette bibliothèque et cette extension ne sont ni installées ni configurées.
Pour installer la bibliothèque ** SQLSRV ** et l'extension PDO, vous pouvez utiliser un fichier de ** configuration ** .ebextensions qui exécute un script dans vos instances Amazon Elastic Compute Cloud (Amazon EC2). Le script effectue les opérations suivantes :
- Installe le pilote et les outils appropriés pour Microsoft SQL Server
- Active les bibliothèques et extensions PHP requises
Résolution
**Remarque :**Les étapes suivantes s'appliquent aux environnements Elastic Beanstalk avec toutes les versions de plateforme PHP.
-
À la racine de votre bundle d'applications, créez un répertoire nommé ** .ebextensions**.
-
Créez un fichier de configuration .ebextensions, tel que le fichier ** .ebextensions**/pdo \ _sqlsrv.config. suivant :
**Important :**Le fichier .ebextensions utilisé dans la résolution suivante s'applique uniquement aux instances Amazon Machine Image (AMI) Amazon Linux 1 et Amazon Linux 2. Le fichier .ebextensions ne s'applique pas à Windows ni aux instances AMI Ubuntu personnalisées dans Elastic Beanstalk.
###################################################################################################
#### 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
-
Créez un bundle source d'applications qui inclut votre fichier .ebextensions à partir de l'étape 2.
-
Déployez votre application Elastic Beanstalk mise à jour.