Skip to content

Instantly share code, notes, and snippets.

Last active March 31, 2022 04:00
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
How to get Microsoft PHP drivers working with Amazon Linux 2 (pdo_sqlsrv, sqlsrv_connect)


  • Amazon Linux 2 EC2 instance
  • Firewall port 80 open
  • Base Linux sysadmin ability


  • SSH into your Amazon Linux 2 instance
  • Make a test sqlsrv_connect page in your document root (eg /var/www/html/) see below
  • Follow the commands below
  • Run "php -m" and you should see pdo_sqlsrv and sqlsrv in the list of PHP modules
  • Test all is working by running the test sqlsrv_connect page (eg http://InstanceIP/index.php)


sudo su
sudo yum-config-manager --add-repo
sudo yum update
sudo ACCEPT_EULA=Y yum install -y php msodbcsql mssql-tools unixODBC-devel php-devel php-pear php-pdo php-xml re2c gcc-c++ gcc
amazon-linux-extras install -y lamp-mariadb10.2-php7.2 php7.2
sudo pecl install sqlsrv
sudo pecl install pdo_sqlsrv
sudo sudo amazon-linux-extras install -y lamp-mariadb10.2-php7.2 php7.2
echo "extension=sqlsrv" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"`
echo "extension=pdo_sqlsrv" >> /etc/php.d/30-pdo_sqlsrv.ini
sudo setsebool -P httpd_can_network_connect_db 1
sudo systemctl restart httpd && sudo apachectl restart

sqlsrv_connect test page


ini_set('display_errors', 1);

$serverName = "[YOUR_DB_ADDRESS]";
$connectionOptions = array(
    "Database" => "[DB_NAME]",
    "Uid" => "[USER]",
    "PWD" => "[PASSWORD]"

$conn = sqlsrv_connect($serverName, $connectionOptions);
if($conn === false )
{ print "Working"; } else { print "Error"; }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment