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

Published Mar 23 2019 02:48 PM 290 Views
Microsoft
First published on MSDN on Oct 10, 2016
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 http://localhost/testsql.php

  • You should be able to connect to your SQL Server/Azure SQL Database and see the following results





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

Meet Bhagdev ( meetb@microsoft.com )

%3CLINGO-SUB%20id%3D%22lingo-sub-384819%22%20slang%3D%22en-US%22%3EGetting%20Started%20with%20PHP%207%20%2B%20SQL%20Server%20and%20Azure%20SQL%20Database%20on%20Linux%20(Ubuntu)%20with%20Apache%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-384819%22%20slang%3D%22en-US%22%3E%0A%20%26lt%3Bmeta%20http-equiv%3D%22Content-Type%22%20content%3D%22text%2Fhtml%3B%20charset%3DUTF-8%22%20%2F%26gt%3B%3CSTRONG%3EFirst%20published%20on%20MSDN%20on%20Oct%2010%2C%202016%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%20This%20blog%20will%20show%20you%20how%20to%20create%20a%20sample%20PHP%207%20application%20and%20connect%20to%20SQL%20Server%20or%20Azure%20SQL%20DB%20from%20your%20Linux%20(Ubuntu)%20machine.%20%3CBR%20%2F%3E%3CH2%20id%3D%22toc-hId-1732411996%22%20id%3D%22toc-hId--1412587656%22%3EStep%201%3A%20Install%20%26nbsp%3Bpre-requisites%3C%2FH2%3E%3CBR%20%2F%3E%20Ubuntu%2015.04%20%3CBR%20%2F%3E%20%5Bsnippet%20slug%3Dodbc-ubuntu-15-04%20lang%3Dbsh%5D%20%3CBR%20%2F%3E%20Ubuntu%2015.10%20%3CBR%20%2F%3E%20%5Bsnippet%20slug%3Dodbc-ubuntu-15-10%20lang%3Dbsh%5D%20%3CBR%20%2F%3E%20Ubuntu%2016.04%20%3CBR%20%2F%3E%20%5Bsnippet%20slug%3Dodbc-ubuntu-16-04%20lang%3Dbsh%5D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20*Note%3A%20On%20Ubuntu%2C%20you%20need%20to%20make%20sure%20you%20install%20%3CSTRONG%3E%20PHP%207%20%3C%2FSTRONG%3E%20before%20you%20proceed%20to%20step%202.%20The%20Microsoft%20PHP%20Drivers%20for%20SQL%20Server%20will%20only%20work%20for%20PHP%207%2B.%20You%20can%20install%20PHP%20following%20the%20%3CSTRONG%3E%20%3CA%20href%3D%22http%3A%2F%2Ftecadmin.net%2Finstall-php-7-on-ubuntu%2F%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3E%20instructions%20here.%20%3C%2FA%3E%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%3CH2%20id%3D%22toc-hId--819744965%22%20id%3D%22toc-hId-1074925177%22%3EStep%202%3A%20Install%20Apache%3C%2FH2%3E%3CBR%20%2F%3E%20%5Bsnippet%20slug%3Dapache%20lang%3Dbsh%5D%20%3CBR%20%2F%3E%3CH2%20id%3D%22toc-hId-923065370%22%20id%3D%22toc-hId--732529286%22%3EStep%203%3A%20Install%20the%20Microsoft%20PHP%20Drivers%20for%20SQL%20Server%3C%2FH2%3E%3CBR%20%2F%3E%20%5Bsnippet%20slug%3Dpecl%20lang%3Dbsh%5D%20%3CBR%20%2F%3E%3CH2%20id%3D%22toc-hId--1629091591%22%20id%3D%22toc-hId-1754983547%22%3EStep%204%3A%20Add%26nbsp%3Bthe%20Microsoft%20PHP%20Drivers%20for%20SQL%20Server%20to%20php.ini%3C%2FH2%3E%3CBR%20%2F%3E%20%5Bsnippet%20slug%3Dadd-extension%20lang%3Dbsh%5D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Restart%20Apache%20to%20load%20the%20new%20php.ini%20file%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5Bsnippet%20slug%3Drestart-apache%20lang%3Dbsh%5D%20%3CBR%20%2F%3E%3CH2%20id%3D%22toc-hId-113718744%22%20id%3D%22toc-hId--52470916%22%3EStep%205%3A%20Create%26nbsp%3Byour%20sample%20app%3C%2FH2%3E%3CBR%20%2F%3E%3CUL%3E%3CBR%20%2F%3E%3CLI%3ENavigate%20to%20%3CSTRONG%3E%20%2Fvar%2Fwww%2Fhtml%20%3C%2FSTRONG%3E%20and%20create%20a%20new%20file%20called%20%3CSTRONG%3E%20testsql.php%20%3C%2FSTRONG%3E%20.%20Copy%20and%20paste%20the%20following%20code%20in%20tetsql.php%20and%20change%20the%20servername%2C%20username%2C%20password%20and%20databasename.%3C%2FLI%3E%3CBR%20%2F%3E%3C%2FUL%3E%3CBR%20%2F%3E%20%5Bsnippet%20slug%3Dcode-sample%20lang%3Dbsh%5D%20%3CBR%20%2F%3E%3CH2%20id%3D%22toc-hId-1856529079%22%20id%3D%22toc-hId--1859925379%22%3EStep%206%3A%20Run%20your%20sample%20app%3C%2FH2%3E%3CBR%20%2F%3E%3CDIV%3E%3CBR%20%2F%3E%3CUL%3E%3CBR%20%2F%3E%3CLI%3EGo%20to%20your%20browser%20and%20type%20in%20%3CSTRONG%3E%20http%3A%2F%2Flocalhost%2Ftestsql.php%3C%2FSTRONG%3E%3C%2FLI%3E%3CBR%20%2F%3E%3CLI%3EYou%20should%20be%20able%20to%20connect%20to%20your%20SQL%20Server%2FAzure%20SQL%20Database%20and%20see%20the%20following%20results%3C%2FLI%3E%3CBR%20%2F%3E%3C%2FUL%3E%3CBR%20%2F%3E%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F98071i31F1C803D6E1665C%22%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3C%2FDIV%3E%3CBR%20%2F%3E%20If%20you%20have%20any%20questions%2Fissues%20report%20it%20on%20our%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FMicrosoft%2Fmsphpsql%2Fissues%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3E%20GitHub%20repository%20%3C%2FA%3E%20.%20%3CBR%20%2F%3E%3CBR%20%2F%3EMeet%20Bhagdev%20(%20%3CA%20href%3D%22mailto%3Ameetb%40microsoft.com%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3E%20meetb%40microsoft.com%20%3C%2FA%3E%20)%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F98072iDAE42DA89875B01B%22%20%2F%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-384819%22%20slang%3D%22en-US%22%3EFirst%20published%20on%20MSDN%20on%20Oct%2010%2C%202016%20This%20blog%20will%20show%20you%20how%20to%20create%20a%20sample%20PHP%207%20application%20and%20connect%20to%20SQL%20Server%20or%20Azure%20SQL%20DB%20from%20your%20Linux%20(Ubuntu)%20machine.%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-384819%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3ESQLServerDrivers%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Version history
Last update:
‎Mar 23 2019 02:48 PM
Updated by: