Recibo un error grave de PHP al implementar una aplicación en una plataforma PHP de AWS Elastic Beanstalk que se conecta a una base de datos de Microsoft SQL Server.
Descripción breve
Al implementar una aplicación en una plataforma PHP de Elastic Beanstalk que se conecta a una base de datos de Microsoft SQL Server, puedes recibir el error siguiente:“PHP Fatal error: Uncaught Error: Call to undefined function sqlsrv_connect() in /var/app/current/DB/”
Para conectar PHP a una base de datos de Microsoft SQL Server, primero debes instalar y configurar la biblioteca SQLSRV y su extensión PDO. De forma predeterminada, esta biblioteca y su extensión no están instaladas ni configuradas.
Para instalar la biblioteca SQLSRV y la extensión PDO, puedes usar un archivo de configuración .ebextensions que ejecuta un script en tus instancias de Amazon Elastic Compute Cloud (Amazon EC2). El script hace lo siguiente:
- Instalar las herramientas y el controlador correcto para Microsoft SQL Server
- Activar las bibliotecas y las extensiones PHP necesarias
Resolución
Nota: Los siguientes pasos se aplican a los entornos de Elastic Beanstalk con cualquier versión de la plataforma PHP.
1. En la raíz del paquete de aplicaciones, crea un directorio denominado .ebextensions.
2. Crea un archivo de configuración .ebextensions, como el archivo .ebextensions/pdo_sqlsrv.config siguiente:
Importante: El archivo .ebextensions que se utiliza en la siguiente resolución solo se aplica a las instancias de Amazon Machine Image (AMI) de Amazon Linux 1 y Amazon Linux 2. El archivo .ebextensions no se aplica a Windows ni a las instancias AMI de Ubuntu personalizadas en 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
3. Crea un paquete de código fuente de la aplicación que incluya el archivo .ebextensions del paso 2.
4. Implementa la aplicación Elastic Beanstalk actualizada.