How do I resolve the error that I receive when I try to deploy a Flask application on an Elastic Beanstalk PHP 7.3 platform that connects to a Microsoft SQL Server database?
Last updated: 2020-10-21
When I try to deploy a Flask application on an AWS Elastic Beanstalk PHP 7.3 platform that connects to a Microsoft SQL Server database, I receive an error similar to the following: "PHP Fatal error: Uncaught Error: Call to undefined function sqlsrv_connect() in /var/app/current/DB/."
To connect PHP to a Microsoft SQL Server database, you must first install and configure the SQLSRV library and its PDO extension. The library and extension aren't installed and configured by default.
To resolve this error, you can use a .ebextensions configuration file that runs a script in your Amazon Elastic Compute Cloud (Amazon EC2) instances. The script does the following:
- Installs the correct driver and tools for Microsoft SQL Server
- Enables the required PHP libraries and extensions
Important: The .ebextensions file used in the following resolution is applicable only for Amazon Linux Amazon Machine Image (AMI) instances. The .ebextensions file doesn't apply to Windows or custom Ubuntu AMI instances in Elastic Beanstalk.
Note: The following resolution applies to an Elastic Beanstalk environment with a PHP platform version 7.3, and may not work as expected on other PHP platform versions.
1. In the root of your application bundle, create a directory named .ebextensions.
2. Create a .ebextensions configuration file (such as .ebextensions/pdo_sqlsrv.config). For example:
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;