Create PHP apps using SQL Server on Ubuntu
In this section, you will get SQL Server 2017 running on Ubuntu. After that you will install the necessary dependencies to create PHP apps with SQL Server
Step 1.1 Install SQL Server
Note: To ensure optimal performance of SQL Server, your machine should have at least 4 GB of memory. If you need to get Ubuntu, check out the Ubuntu Downloads website.
-
Register the Microsoft Linux repositories and add their keys.
curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add - curl https://packages.microsoft.com/config/ubuntu/16.04/mssql-server-2017.list | sudo tee /etc/apt/sources.list.d/mssql-server-2017.list
-
Install SQL Server.
sudo apt-get update sudo apt-get install mssql-server
Reading package lists... Done Building dependency tree Reading state information... Done The following NEW packages will be installed: mssql-server ... Unpacking mssql-server ... Setting up mssql-server ...
-
Setup your SQL Server.
sudo /opt/mssql/bin/mssql-conf setup
Microsoft(R) SQL Server(R) Setup To abort setup at anytime, press Ctrl-C. The license terms for this product can be downloaded from http://go.microsoft.com/fwlink/?LinkId=746388 and found in /usr/share/doc/mssql-server/LICENSE.TXT. Do you accept the license terms? If so, please type YES: Please enter a password for the system administrator (SA) account: Please confirm the password for the system administrator (SA) account:
You now have SQL Server running locally on your Ubuntu machine! Check out the next section to continue installing prerequisites.
Step 1.2 Install PHP and other required packages
To install PHP 7.1 or 7.2, replace
7.3
with7.1
or7.2
in the following commands.
sudo su
add-apt-repository ppa:ondrej/php -y
apt-get update
apt-get install php7.3 php7.3-dev php7.3-xml -y --allow-unauthenticated
You have successfully installed PHP on your Ubuntu machine!
Step 1.3 Install the ODBC Driver and SQL Command Line Utility for SQL Server
SQLCMD is a command line tool that enables you to connect to SQL Server and run queries.
For Ubuntu 18.04 or 18.10, replace
16.04
in the following commands with18.04
(the 18.04 repository is for both Ubuntu 18.04 and 18.10).
sudo su
curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
curl https://packages.microsoft.com/config/ubuntu/16.04/prod.list > /etc/apt/sources.list.d/mssql-release.list
exit
sudo apt-get update
sudo ACCEPT_EULA=Y apt-get install msodbcsql17 mssql-tools
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bash_profile
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
source ~/.bashrc
sudo apt-get install unixodbc-dev
After installing SQLCMD, you can connect to SQL Server using the following command:
sqlcmd -S localhost -U sa -P yourpassword
1> # You're connected! Type your T-SQL statements here. Use the keyword 'GO' to execute each batch of statements.
This how to run a basic inline query. The results will be printed to the STDOUT.
sqlcmd -S localhost -U sa -P yourpassword -Q "SELECT @@VERSION"
--------------------------------------------------------
Microsoft SQL Server vNext (CTP2.0) - 14.0.500.272 (X64)
Apr 13 2017 11:44:40
Copyright (c) Microsoft Corporation
on Linux (Ubuntu 16.04)
1 rows(s) returned
Executed in 1 ns
You have successfully installed SQL Server Command Line Utilities on your Ubuntu machine!
Have Questions?
Happy to help! You can find us on GitHub, MSDN Forums, and StackOverflow. We also monitor the #SQLServerDev hashtag on Twitter.