Getting Started with PHP 7 + SQL Server and Azure SQL Database on Linux (Ubuntu) with Apache

This blog will show you how to create a sample PHP 7 application and connect to SQL Server or Azure SQL DB from your Linux (Ubuntu) machine.

Step 1: Install  pre-requisites

Ubuntu 15.04

[snippet slug=odbc-ubuntu-15-04 lang=bsh]

Ubuntu 15.10

[snippet slug=odbc-ubuntu-15-10 lang=bsh]

Ubuntu 16.04

[snippet slug=odbc-ubuntu-16-04 lang=bsh]

*Note: On Ubuntu, you need to make sure you install PHP 7 before you proceed to step 2. The Microsoft PHP Drivers for SQL Server will only work for PHP 7+. You can install PHP following the instructions here.

Step 2: Install Apache

[snippet slug=apache lang=bsh]

Step 3: Install the Microsoft PHP Drivers for SQL Server

[snippet slug=pecl lang=bsh]

Step 4: Add the Microsoft PHP Drivers for SQL Server to php.ini

[snippet slug=add-extension lang=bsh]

Restart Apache to load the new php.ini file

[snippet slug=restart-apache lang=bsh]

Step 5: Create your sample app

  • Navigate to /var/www/html and create a new file called testsql.php. Copy and paste the following code in tetsql.php and change the servername, username, password and databasename.

[snippet slug=code-sample lang=bsh]

Step 6: Run your sample app

  • Go to your browser and type in https://localhost/testsql.php
  • You should be able to connect to your SQL Server/Azure SQL Database and see the following results

results

If you have any questions/issues report it on our GitHub repository.

Meet Bhagdev (meetb@microsoft.com)

MSFTlovesPHP