2018-07-13 00:16:28 +02:00
# Linux and macOS Installation Tutorial for the Microsoft Drivers for PHP for SQL Server
2022-01-20 00:30:28 +01:00
The following instructions assume a clean environment and show how to install PHP 8.1, the Microsoft ODBC driver, the Apache web server, and the Microsoft Drivers for PHP for SQL Server on Ubuntu, RedHat, Debian, Suse, Alpine, and macOS. These instructions advise installing the drivers using PECL, but you can also download the prebuilt binaries from the [Microsoft Drivers for PHP for SQL Server ](https://github.com/Microsoft/msphpsql/releases ) Github project page and install them following the instructions in [Loading the Microsoft Drivers for PHP for SQL Server ](https://docs.microsoft.com/sql/connect/php/loading-the-php-sql-driver ). For an explanation of extension loading and why we do not add the extensions to php.ini, see the section on [loading the drivers ](https://docs.microsoft.com/sql/connect/php/loading-the-php-sql-driver#loading-the-driver-at-php-startup ).
2018-02-09 23:26:08 +01:00
2022-01-20 00:30:28 +01:00
The following instructions install PHP 8.1 by default using `pecl install` , if the PHP 8.1 packages are available. You may need to run `pecl channel-update pecl.php.net` first. Note that some supported Linux distros default to PHP 7.1 or earlier, which is not supported for the latest version of the PHP drivers for SQL Server -- please see the notes at the beginning of each section to install PHP 7.4 or 8.0 instead.
2020-01-31 07:50:30 +01:00
2021-01-15 00:37:44 +01:00
Also included are instructions for installing the PHP FastCGI Process Manager, PHP-FPM, on Ubuntu. This is needed if you are using the nginx web server instead of Apache.
2018-02-09 23:26:08 +01:00
2021-01-15 00:37:44 +01:00
While these instructions contain commands to install both SQLSRV and PDO_SQLSRV drivers, the drivers can be installed and function independently. Users comfortable with customizing their configuration can adjust these instructions to be specific to SQLSRV or PDO_SQLSRV. Both drivers have the same dependencies except where noted below.
## Contents of this page
2018-02-09 23:26:08 +01:00
2021-09-08 19:19:45 +02:00
- [Installing the drivers on Ubuntu ](#installing-the-drivers-on-ubuntu )
2020-01-31 07:50:30 +01:00
- [Installing the drivers with PHP-FPM on Ubuntu ](#installing-the-drivers-with-php-fpm-on-ubuntu )
2021-09-08 19:19:45 +02:00
- [Installing the drivers on Red Hat ](#installing-the-drivers-on-red-hat )
- [Installing the drivers on Debian ](#installing-the-drivers-on-debian )
- [Installing the drivers on Suse ](#installing-the-drivers-on-suse )
- [Installing the drivers on Alpine ](#installing-the-drivers-on-alpine )
- [Installing the drivers on macOS ](#installing-the-drivers-on-macos )
2018-02-09 23:26:08 +01:00
2021-09-08 19:19:45 +02:00
## Installing the drivers on Ubuntu
2018-02-09 23:26:08 +01:00
2018-05-25 21:01:08 +02:00
> [!NOTE]
2022-01-20 00:30:28 +01:00
> To install PHP 7.4 or 8.0, replace 8.1 with 7.4 or 8.0 in the following commands.
2018-02-09 23:26:08 +01:00
### Step 1. Install PHP
2020-01-31 07:50:30 +01:00
```bash
2018-02-09 23:26:08 +01:00
sudo su
2018-12-07 00:23:19 +01:00
add-apt-repository ppa:ondrej/php -y
2018-02-09 23:26:08 +01:00
apt-get update
2022-01-20 00:30:28 +01:00
apt-get install php8.1 php8.1-dev php8.1-xml -y --allow-unauthenticated
2018-02-09 23:26:08 +01:00
```
### Step 2. Install prerequisites
2021-01-15 00:37:44 +01:00
Install the ODBC driver for Ubuntu by following the instructions on the [Install the Microsoft ODBC driver for SQL Server (Linux) ](https://docs.microsoft.com/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-ver15 ).
2018-02-09 23:26:08 +01:00
### Step 3. Install the PHP drivers for Microsoft SQL Server
2021-01-15 00:37:44 +01:00
```bash
sudo pecl install sqlsrv
sudo pecl install pdo_sqlsrv
2018-02-09 23:26:08 +01:00
sudo su
2022-01-20 00:30:28 +01:00
printf "; priority=20\nextension=sqlsrv.so\n" > /etc/php/8.1/mods-available/sqlsrv.ini
printf "; priority=30\nextension=pdo_sqlsrv.so\n" > /etc/php/8.1/mods-available/pdo_sqlsrv.ini
2018-02-09 23:26:08 +01:00
exit
2022-01-20 00:30:28 +01:00
sudo phpenmod -v 8.1 sqlsrv pdo_sqlsrv
2018-02-09 23:26:08 +01:00
```
2020-01-31 07:50:30 +01:00
If there is only one PHP version in the system, then the last step can be simplified to `phpenmod sqlsrv pdo_sqlsrv` .
2019-09-05 00:13:43 +02:00
2018-02-09 23:26:08 +01:00
### Step 4. Install Apache and configure driver loading
2021-01-15 00:37:44 +01:00
```bash
2018-02-09 23:26:08 +01:00
sudo su
2022-01-20 00:30:28 +01:00
apt-get install libapache2-mod-php8.1 apache2
2018-02-09 23:26:08 +01:00
a2dismod mpm_event
a2enmod mpm_prefork
2022-01-20 00:30:28 +01:00
a2enmod php8.1
2018-07-13 00:16:28 +02:00
exit
2018-02-09 23:26:08 +01:00
```
### Step 5. Restart Apache and test the sample script
2021-01-15 00:37:44 +01:00
```bash
2018-02-09 23:26:08 +01:00
sudo service apache2 restart
```
To test your installation, see [Testing your installation ](#testing-your-installation ) at the end of this document.
2020-01-31 07:50:30 +01:00
## Installing the drivers with PHP-FPM on Ubuntu
2018-02-09 23:26:08 +01:00
2018-05-25 21:01:08 +02:00
> [!NOTE]
2022-01-20 00:30:28 +01:00
> To install PHP 7.4 or 8.0, replace 8.1 with 7.4 or 8.0 in the following commands.
2020-01-31 07:50:30 +01:00
### Step 1. Install PHP
```bash
sudo su
add-apt-repository ppa:ondrej/php -y
apt-get update
2022-01-20 00:30:28 +01:00
apt-get install php8.1 php8.1-dev php8.1-fpm php8.1-xml -y --allow-unauthenticated
2020-01-31 07:50:30 +01:00
```
Verify the status of the PHP-FPM service by running
2021-01-15 00:37:44 +01:00
```bash
2022-01-20 00:30:28 +01:00
systemctl status php8.1-fpm
2020-01-31 07:50:30 +01:00
```
### Step 2. Install prerequisites
2021-01-15 00:37:44 +01:00
Install the ODBC driver for Ubuntu by following the instructions on the [Install the Microsoft ODBC driver for SQL Server (Linux) ](https://docs.microsoft.com/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-ver15 ).
2020-01-31 07:50:30 +01:00
### Step 3. Install the PHP drivers for Microsoft SQL Server
2021-01-15 00:37:44 +01:00
```bash
2022-01-20 00:30:28 +01:00
sudo pecl config-set php_ini /etc/php/8.1/fpm/php.ini
2021-01-15 00:37:44 +01:00
sudo pecl install sqlsrv
sudo pecl install pdo_sqlsrv
2020-01-31 07:50:30 +01:00
sudo su
2022-01-20 00:30:28 +01:00
printf "; priority=20\nextension=sqlsrv.so\n" > /etc/php/8.1/mods-available/sqlsrv.ini
printf "; priority=30\nextension=pdo_sqlsrv.so\n" > /etc/php/8.1/mods-available/pdo_sqlsrv.ini
2020-01-31 07:50:30 +01:00
exit
2022-01-20 00:30:28 +01:00
sudo phpenmod -v 8.1 sqlsrv pdo_sqlsrv
2020-01-31 07:50:30 +01:00
```
If there is only one PHP version in the system, then the last step can be simplified to `phpenmod sqlsrv pdo_sqlsrv` .
2022-01-20 00:30:28 +01:00
Verify that `sqlsrv.ini` and `pdo_sqlsrv.ini` are located in `/etc/php/8.1/fpm/conf.d/` :
2021-01-15 00:37:44 +01:00
```bash
2022-01-20 00:30:28 +01:00
ls /etc/php/8.1/fpm/conf.d/*sqlsrv.ini
2020-01-31 07:50:30 +01:00
```
Restart the PHP-FPM service:
2021-01-15 00:37:44 +01:00
```bash
2022-01-20 00:30:28 +01:00
sudo systemctl restart php8.1-fpm
2020-01-31 07:50:30 +01:00
```
### Step 4. Install and configure nginx
2021-01-15 00:37:44 +01:00
```bash
2020-01-31 07:50:30 +01:00
sudo apt-get update
sudo apt-get install nginx
sudo systemctl status nginx
```
To configure nginx, you must edit the `/etc/nginx/sites-available/default` file. Add `index.php` to the list below the section that says `# Add index.php to the list if you are using PHP` :
```
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html index.php;
```
2021-01-15 00:37:44 +01:00
Next, uncomment and modify the section following `# pass PHP scripts to FastCGI server` as follows:
2020-01-31 07:50:30 +01:00
```
# pass PHP scripts to FastCGI server
#
location ~ \.php$ {
include snippets/fastcgi-php.conf;
2022-01-20 00:30:28 +01:00
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
2020-01-31 07:50:30 +01:00
}
```
### Step 5. Restart nginx and test the sample script
2021-01-15 00:37:44 +01:00
```bash
2020-01-31 07:50:30 +01:00
sudo systemctl restart nginx.service
```
To test your installation, see [Testing your installation ](#testing-your-installation ) at the end of this document.
2021-09-08 19:19:45 +02:00
## Installing the drivers on Red Hat
2018-02-09 23:26:08 +01:00
### Step 1. Install PHP
2020-01-31 07:50:30 +01:00
To install PHP on Red Hat 7, run the following:
> [!NOTE]
2022-01-20 00:30:28 +01:00
> To install PHP 7.4 or 8.0, replace remi-php81 with remi-php74 or remi-php80 respectively in the following commands.
2021-01-15 00:37:44 +01:00
```bash
2018-02-09 23:26:08 +01:00
sudo su
2020-01-31 07:50:30 +01:00
yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
yum install https://rpms.remirepo.net/enterprise/remi-release-7.rpm
2018-02-09 23:26:08 +01:00
subscription-manager repos --enable=rhel-7-server-optional-rpms
2018-12-07 00:23:19 +01:00
yum install yum-utils
2022-01-20 00:30:28 +01:00
yum-config-manager --enable remi-php81
2018-02-09 23:26:08 +01:00
yum update
2021-01-15 00:37:44 +01:00
# Note: The php-pdo package is required only for the PDO_SQLSRV driver
2022-01-20 00:30:28 +01:00
yum install php php-pdo php-pear php-devel
2018-02-09 23:26:08 +01:00
```
2020-01-31 07:50:30 +01:00
To install PHP on Red Hat 8, run the following:
> [!NOTE]
2022-01-20 00:30:28 +01:00
> To install PHP 7.4 or 8.0, replace remi-8.1 with remi-7.4 or remi-8.0 respectively in the following commands.
2021-01-15 00:37:44 +01:00
```bash
2020-01-31 07:50:30 +01:00
sudo su
dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
dnf install https://rpms.remirepo.net/enterprise/remi-release-8.rpm
dnf install yum-utils
dnf module reset php
2022-01-20 00:30:28 +01:00
dnf module install php:remi-8.1
2020-01-31 07:50:30 +01:00
subscription-manager repos --enable codeready-builder-for-rhel-8-x86_64-rpms
dnf update
2021-01-15 00:37:44 +01:00
# Note: The php-pdo package is required only for the PDO_SQLSRV driver
2020-01-31 07:50:30 +01:00
dnf install php-pdo php-pear php-devel
2018-02-09 23:26:08 +01:00
```
2020-01-31 07:50:30 +01:00
### Step 2. Install prerequisites
2021-01-15 00:37:44 +01:00
Install the ODBC driver for Red Hat 7 or 8 by following the instructions on the [Install the Microsoft ODBC driver for SQL Server (Linux) ](https://docs.microsoft.com/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-ver15 ).
2020-01-31 07:50:30 +01:00
2018-02-09 23:26:08 +01:00
### Step 3. Install the PHP drivers for Microsoft SQL Server
2021-01-15 00:37:44 +01:00
```bash
sudo pecl install sqlsrv
sudo pecl install pdo_sqlsrv
2018-02-09 23:26:08 +01:00
sudo su
echo extension=pdo_sqlsrv.so >> `php --ini | grep "Scan for additional .ini files" | sed -e "s|.*:\s*||"` /30-pdo_sqlsrv.ini
echo extension=sqlsrv.so >> `php --ini | grep "Scan for additional .ini files" | sed -e "s|.*:\s*||"` /20-sqlsrv.ini
exit
```
2020-01-31 07:50:30 +01:00
You can alternatively install from the Remi repo:
2021-01-15 00:37:44 +01:00
```bash
2018-12-07 00:23:19 +01:00
sudo yum install php-sqlsrv
2018-02-09 23:26:08 +01:00
```
### Step 4. Install Apache
2021-01-15 00:37:44 +01:00
```bash
2018-02-09 23:26:08 +01:00
sudo yum install httpd
```
SELinux is installed by default and runs in Enforcing mode. To allow Apache to connect to databases through SELinux, run the following command:
2021-01-15 00:37:44 +01:00
```bash
2018-02-09 23:26:08 +01:00
sudo setsebool -P httpd_can_network_connect_db 1
```
### Step 5. Restart Apache and test the sample script
2021-01-15 00:37:44 +01:00
```bash
2018-02-09 23:26:08 +01:00
sudo apachectl restart
```
To test your installation, see [Testing your installation ](#testing-your-installation ) at the end of this document.
2021-09-08 19:19:45 +02:00
## Installing the drivers on Debian
2018-02-09 23:26:08 +01:00
2018-05-25 21:01:08 +02:00
> [!NOTE]
2022-01-20 00:30:28 +01:00
> To install PHP 7.4 or 8.0, replace 8.1 in the following commands with 7.4 or 8.0.
2018-02-09 23:26:08 +01:00
### Step 1. Install PHP
2021-01-15 00:37:44 +01:00
```bash
2018-02-09 23:26:08 +01:00
sudo su
apt-get install curl apt-transport-https
wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg
echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/php.list
apt-get update
2022-01-20 00:30:28 +01:00
apt-get install -y php8.1 php8.1-dev php8.1-xml php8.1-intl
2018-02-09 23:26:08 +01:00
```
### Step 2. Install prerequisites
2021-01-15 00:37:44 +01:00
Install the ODBC driver for Debian by following the instructions on the [Install the Microsoft ODBC driver for SQL Server (Linux) ](https://docs.microsoft.com/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-ver15 ).
2018-02-09 23:26:08 +01:00
You may also need to generate the correct locale to get PHP output to display correctly in a browser. For example, for the en_US UTF-8 locale, run the following commands:
2021-01-15 00:37:44 +01:00
```bash
2018-02-09 23:26:08 +01:00
sudo su
sed -i 's/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/g' /etc/locale.gen
locale-gen
```
2020-01-31 07:50:30 +01:00
You may need to add `/usr/sbin` to your `$PATH` , as the `locale-gen` executable is located there.
2018-02-09 23:26:08 +01:00
### Step 3. Install the PHP drivers for Microsoft SQL Server
2021-01-15 00:37:44 +01:00
```bash
sudo pecl install sqlsrv
sudo pecl install pdo_sqlsrv
2018-02-09 23:26:08 +01:00
sudo su
2022-01-20 00:30:28 +01:00
printf "; priority=20\nextension=sqlsrv.so\n" > /etc/php/8.1/mods-available/sqlsrv.ini
printf "; priority=30\nextension=pdo_sqlsrv.so\n" > /etc/php/8.1/mods-available/pdo_sqlsrv.ini
2018-02-09 23:26:08 +01:00
exit
2022-01-20 00:30:28 +01:00
sudo phpenmod -v 8.1 sqlsrv pdo_sqlsrv
2018-02-09 23:26:08 +01:00
```
2020-01-31 07:50:30 +01:00
If there is only one PHP version in the system, then the last step can be simplified to `phpenmod sqlsrv pdo_sqlsrv` . As with `locale-gen` , `phpenmod` is located in `/usr/sbin` so you may need to add this directory to your `$PATH` .
2019-09-05 00:13:43 +02:00
2018-02-09 23:26:08 +01:00
### Step 4. Install Apache and configure driver loading
2021-01-15 00:37:44 +01:00
```bash
2018-02-09 23:26:08 +01:00
sudo su
2022-01-20 00:30:28 +01:00
apt-get install libapache2-mod-php8.1 apache2
2018-02-09 23:26:08 +01:00
a2dismod mpm_event
a2enmod mpm_prefork
2022-01-20 00:30:28 +01:00
a2enmod php8.1
2018-02-09 23:26:08 +01:00
```
### Step 5. Restart Apache and test the sample script
2021-01-15 00:37:44 +01:00
```bash
2018-02-09 23:26:08 +01:00
sudo service apache2 restart
```
To test your installation, see [Testing your installation ](#testing-your-installation ) at the end of this document.
2021-09-08 19:19:45 +02:00
## Installing the drivers on Suse
2018-02-09 23:26:08 +01:00
2018-05-25 21:01:08 +02:00
> [!NOTE]
2022-01-20 00:30:28 +01:00
> In the following instructions, replace `<SuseVersion>` with your version of Suse - if you are using Suse Enterprise Linux 15, it will be SLE_15_SP3 or SLE_15_SP4 (or above). For Suse 12, use SLE_12_SP5 (or above). Not all versions of PHP are available for all versions of Suse Linux - please refer to `http://download.opensuse.org/repositories/devel:/languages:/php` to see which versions of Suse have the default version PHP available, or check `http://download.opensuse.org/repositories/devel:/languages:/php:/` to see which other versions of PHP are available for which versions of Suse.
2019-02-08 20:07:04 +01:00
> [!NOTE]
2022-01-27 22:33:25 +01:00
> Packages for PHP 7.4 or above are not available for Suse 12, as of today.
2018-02-09 23:26:08 +01:00
### Step 1. Install PHP
2021-01-15 00:37:44 +01:00
```bash
2018-02-09 23:26:08 +01:00
sudo su
2019-02-08 20:07:04 +01:00
zypper -n ar -f https://download.opensuse.org/repositories/devel:languages:php/< SuseVersion > /devel:languages:php.repo
2018-02-09 23:26:08 +01:00
zypper --gpg-auto-import-keys refresh
2022-01-20 00:30:28 +01:00
zypper -n install php8 php8-pdo php8-devel php8-openssl
2018-02-09 23:26:08 +01:00
```
### Step 2. Install prerequisites
2021-01-15 00:37:44 +01:00
Install the ODBC driver for Suse by following the instructions on the [Install the Microsoft ODBC driver for SQL Server (Linux) ](https://docs.microsoft.com/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-ver15 ).
2018-02-09 23:26:08 +01:00
### Step 3. Install the PHP drivers for Microsoft SQL Server
2021-01-15 00:37:44 +01:00
```bash
sudo pecl install sqlsrv
sudo pecl install pdo_sqlsrv
2018-02-09 23:26:08 +01:00
sudo su
echo extension=pdo_sqlsrv.so >> `php --ini | grep "Scan for additional .ini files" | sed -e "s|.*:\s*||"` /pdo_sqlsrv.ini
echo extension=sqlsrv.so >> `php --ini | grep "Scan for additional .ini files" | sed -e "s|.*:\s*||"` /sqlsrv.ini
exit
```
### Step 4. Install Apache and configure driver loading
2021-01-15 00:37:44 +01:00
```bash
2018-02-09 23:26:08 +01:00
sudo su
2022-01-20 00:30:28 +01:00
zypper install apache2 apache2-mod_php8
a2enmod php8
echo "extension=sqlsrv.so" >> /etc/php8/apache2/php.ini
echo "extension=pdo_sqlsrv.so" >> /etc/php8/apache2/php.ini
2018-07-13 00:16:28 +02:00
exit
2018-02-09 23:26:08 +01:00
```
### Step 5. Restart Apache and test the sample script
2021-01-15 00:37:44 +01:00
```bash
2018-02-09 23:26:08 +01:00
sudo systemctl restart apache2
```
To test your installation, see [Testing your installation ](#testing-your-installation ) at the end of this document.
2021-09-08 19:19:45 +02:00
## Installing the drivers on Alpine
2020-01-31 07:50:30 +01:00
> [!NOTE]
2022-01-20 00:30:28 +01:00
> PHP 8.1 or above may be available from testing or edge repositories for Alpine. You can instead compile PHP from source.
2020-01-31 07:50:30 +01:00
### Step 1. Install PHP
2020-09-10 01:02:41 +02:00
PHP packages for Alpine can be found in the `edge/community` repository. Please check [Enable Community Repository ](https://wiki.alpinelinux.org/wiki/Enable_Community_Repository ) on their WIKI page. Add the following line to `/etc/apk/repositories` , replacing `<mirror>` with the URL of an Alpine repository mirror:
2021-01-15 00:37:44 +01:00
```bash
2020-01-31 07:50:30 +01:00
http://< mirror > /alpine/edge/community
```
Then run:
2021-01-15 00:37:44 +01:00
```bash
2020-01-31 07:50:30 +01:00
sudo su
apk update
2022-01-20 00:30:28 +01:00
# Note: The php*-pdo package is required only for the PDO_SQLSRV driver
# For PHP 7.*
2020-01-31 07:50:30 +01:00
apk add php7 php7-dev php7-pear php7-pdo php7-openssl autoconf make g++
2022-01-20 00:30:28 +01:00
# For PHP 8.*
apk add php8 php8-dev php8-pear php8-pdo php8-openssl autoconf make g++
# The following symbolic links are optional but useful
ln -s /usr/bin/php8 /usr/bin/php
ln -s /usr/bin/phpize8 /usr/bin/phpize
ln -s /usr/bin/pecl8 /usr/bin/pecl
ln -s /usr/bin/php-config8 /usr/bin/php-config
2020-01-31 07:50:30 +01:00
```
2022-01-20 00:30:28 +01:00
2020-01-31 07:50:30 +01:00
### Step 2. Install prerequisites
2021-01-15 00:37:44 +01:00
Install the ODBC driver for Alpine by following the instructions on the [Install the Microsoft ODBC driver for SQL Server (Linux) ](https://docs.microsoft.com/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-ver15 ).
2020-01-31 07:50:30 +01:00
### Step 3. Install the PHP drivers for Microsoft SQL Server
2021-01-15 00:37:44 +01:00
```bash
sudo pecl install sqlsrv
sudo pecl install pdo_sqlsrv
2020-01-31 07:50:30 +01:00
sudo su
echo extension=pdo_sqlsrv.so >> `php --ini | grep "Scan for additional .ini files" | sed -e "s|.*:\s*||"` /10_pdo_sqlsrv.ini
2022-01-20 00:30:28 +01:00
echo extension=sqlsrv.so >> `php --ini | grep "Scan for additional .ini files" | sed -e "s|.*:\s*||"` /20_sqlsrv.ini
2020-01-31 07:50:30 +01:00
```
2020-04-09 00:06:33 +02:00
2020-01-31 07:50:30 +01:00
### Step 4. Install Apache and configure driver loading
2021-01-15 00:37:44 +01:00
```bash
2022-01-20 00:30:28 +01:00
# For PHP 7.*
2020-01-31 07:50:30 +01:00
sudo apk add php7-apache2 apache2
2022-01-20 00:30:28 +01:00
# For PHP 8.*
sudo apk add php8-apache2 apache2
2020-01-31 07:50:30 +01:00
```
### Step 5. Restart Apache and test the sample script
2021-01-15 00:37:44 +01:00
```bash
2020-01-31 07:50:30 +01:00
sudo rc-service apache2 restart
```
To test your installation, see [Testing your installation ](#testing-your-installation ) at the end of this document.
2021-09-08 19:19:45 +02:00
## Installing the drivers on macOS
2018-02-09 23:26:08 +01:00
2021-09-08 19:19:45 +02:00
If you do not already have it, install Homebrew as follows:
2021-01-15 00:37:44 +01:00
```bash
2018-02-09 23:26:08 +01:00
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
```
2021-09-08 19:19:45 +02:00
> [!NOTE]
> If using Apple M1 ARM64 hardware, please install Homebrew and PHP directly without using the emulator Rosetta 2.
2018-05-25 21:01:08 +02:00
> [!NOTE]
2022-01-20 00:30:28 +01:00
> To install PHP 7.4 or 8.0, replace php@8.1 with php@7.4 or php@8.0 respectively in the following commands.
2018-02-09 23:26:08 +01:00
### Step 1. Install PHP
2021-01-15 00:37:44 +01:00
```bash
2018-02-09 23:26:08 +01:00
brew tap
2018-05-25 20:59:22 +02:00
brew tap homebrew/core
2022-01-20 00:30:28 +01:00
brew install php@8.1
2018-02-09 23:26:08 +01:00
```
2021-01-15 00:37:44 +01:00
PHP should now be in your path. Run `php -v` to verify that you are running the correct version of PHP. If PHP is not in your path or it is not the correct version, run the following:
```bash
2022-01-20 00:30:28 +01:00
brew link --force --overwrite php@8.1
2018-05-25 20:59:22 +02:00
```
2022-01-27 22:33:25 +01:00
If using Apple M1 ARM64, you might need to set the path:
```bash
export PATH="/opt/homebrew/bin:$PATH"
```
2018-02-09 23:26:08 +01:00
### Step 2. Install prerequisites
2021-01-15 00:37:44 +01:00
Install the ODBC driver for macOS by following the instructions on the [Install the Microsoft ODBC driver for SQL Server (macOS)](
2020-12-01 02:40:22 +01:00
https://docs.microsoft.com/sql/connect/odbc/linux-mac/install-microsoft-odbc-driver-sql-server-macos?view=sql-server-ver15).
2018-02-09 23:26:08 +01:00
2021-09-08 19:19:45 +02:00
> [!NOTE]
> If using Apple M1 ARM64 hardware, please install Microsoft ODBC driver 17.8+ directly without using the emulator Rosetta 2.
2018-02-09 23:26:08 +01:00
In addition, you may need to install the GNU make tools:
2021-01-15 00:37:44 +01:00
```bash
2018-02-09 23:26:08 +01:00
brew install autoconf automake libtool
```
### Step 3. Install the PHP drivers for Microsoft SQL Server
2021-01-15 00:37:44 +01:00
```bash
sudo pecl install sqlsrv
sudo pecl install pdo_sqlsrv
2018-02-09 23:26:08 +01:00
```
2021-09-08 19:19:45 +02:00
If using Apple M1 ARM64, do the following instead:
```bash
sudo CXXFLAGS="-I/opt/homebrew/opt/unixodbc/include/" LDFLAGS="-L/opt/homebrew/lib/" pecl install sqlsrv
sudo CXXFLAGS="-I/opt/homebrew/opt/unixodbc/include/" LDFLAGS="-L/opt/homebrew/lib/" pecl install pdo_sqlsrv
```
2018-02-09 23:26:08 +01:00
### Step 4. Install Apache and configure driver loading
2021-09-08 19:19:45 +02:00
> [!NOTE]
> The latest macOS 11.0 Big Sur comes with Apache 2.4 pre-installed, but Apple has also removed some required scripts. The solution is to install Apache 2.4 via Homebrew and then configure it, but this is out of scope for this installation guide, so please check Apache or Homebrew for detailed instructions.
2021-01-15 00:37:44 +01:00
```bash
2018-05-25 20:59:22 +02:00
brew install apache2
```
2020-01-31 07:50:30 +01:00
To find the Apache configuration file, `httpd.conf` , for your Apache installation, run
2021-01-15 00:37:44 +01:00
```bash
2020-01-31 07:50:30 +01:00
/usr/local/bin/apachectl -V | grep SERVER_CONFIG_FILE
2018-12-07 00:23:19 +01:00
```
2020-01-31 07:50:30 +01:00
The following commands append the required configuration to `httpd.conf` . Be sure to substitute the path returned by the preceding command in place of `/usr/local/etc/httpd/httpd.conf` :
2021-01-15 00:37:44 +01:00
```bash
2022-01-20 00:30:28 +01:00
echo "LoadModule php7_module /usr/local/opt/php@8.1/lib/httpd/modules/libphp7.so" >> /usr/local/etc/httpd/httpd.conf
2018-02-09 23:26:08 +01:00
(echo "< FilesMatch . php $ > "; echo "SetHandler application/x-httpd-php"; echo "< / FilesMatch > ";) >> /usr/local/etc/httpd/httpd.conf
```
2022-01-20 00:30:28 +01:00
2018-02-09 23:26:08 +01:00
### Step 5. Restart Apache and test the sample script
2021-01-15 00:37:44 +01:00
```bash
2018-02-09 23:26:08 +01:00
sudo apachectl restart
```
2022-01-20 00:30:28 +01:00
2018-02-09 23:26:08 +01:00
To test your installation, see [Testing your installation ](#testing-your-installation ) at the end of this document.
## Testing Your Installation
2020-04-09 00:06:33 +02:00
To test this sample script, create a file called testsql.php in your system's document root. This is `/var/www/html/` on Ubuntu, Debian, and Redhat, `/srv/www/htdocs` on SUSE, `/var/www/localhost/htdocs` on Alpine, or `/usr/local/var/www` on macOS. Copy the following script to it, replacing the server, database, username, and password as appropriate.
2021-01-15 00:37:44 +01:00
### SQLSRV example
```php
2018-02-09 23:26:08 +01:00
< ?php
$serverName = "yourServername";
$connectionOptions = array(
2018-07-13 00:16:28 +02:00
"database" => "yourDatabase",
"uid" => "yourUsername",
"pwd" => "yourPassword"
2018-02-09 23:26:08 +01:00
);
2021-01-15 00:37:44 +01:00
function exception_handler($exception) {
echo "< h1 > Failure< / h1 > ";
echo "Uncaught exception: " , $exception->getMessage();
echo "< h1 > PHP Info for troubleshooting< / h1 > ";
phpinfo();
}
set_exception_handler('exception_handler');
2018-07-13 00:16:28 +02:00
// Establishes the connection
2018-02-09 23:26:08 +01:00
$conn = sqlsrv_connect($serverName, $connectionOptions);
2018-07-13 00:16:28 +02:00
if ($conn === false) {
die(formatErrors(sqlsrv_errors()));
2018-02-09 23:26:08 +01:00
}
2018-07-13 00:16:28 +02:00
// Select Query
$tsql = "SELECT @@Version AS SQL_VERSION";
2018-02-09 23:26:08 +01:00
2018-07-13 00:16:28 +02:00
// Executes the query
$stmt = sqlsrv_query($conn, $tsql);
2018-02-09 23:26:08 +01:00
2018-07-13 00:16:28 +02:00
// Error handling
if ($stmt === false) {
die(formatErrors(sqlsrv_errors()));
}
2018-02-09 23:26:08 +01:00
?>
2021-01-15 00:37:44 +01:00
< h1 > Success Results : < / h1 >
2018-02-09 23:26:08 +01:00
< ?php
2018-07-13 00:16:28 +02:00
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
echo $row['SQL_VERSION'] . PHP_EOL;
2018-02-09 23:26:08 +01:00
}
2018-07-13 00:16:28 +02:00
sqlsrv_free_stmt($stmt);
sqlsrv_close($conn);
2018-02-09 23:26:08 +01:00
2018-07-13 00:16:28 +02:00
function formatErrors($errors)
2018-02-09 23:26:08 +01:00
{
2018-07-13 00:16:28 +02:00
// Display errors
2021-01-15 00:37:44 +01:00
echo "< h1 > SQL Error:< / h1 > ";
2018-02-09 23:26:08 +01:00
echo "Error information: < br / > ";
2018-07-13 00:16:28 +02:00
foreach ($errors as $error) {
echo "SQLSTATE: ". $error['SQLSTATE'] . "< br / > ";
echo "Code: ". $error['code'] . "< br / > ";
echo "Message: ". $error['message'] . "< br / > ";
2018-02-09 23:26:08 +01:00
}
}
?>
```
2021-01-15 00:37:44 +01:00
### PDO_SQLSRV example
```php
< ?php
try {
$serverName = "yourServername";
$databaseName = "yourDatabase";
$uid = "yourUsername";
$pwd = "yourPassword";
$conn = new PDO("sqlsrv:server = $serverName; Database = $databaseName;", $uid, $pwd);
// Select Query
$tsql = "SELECT @@Version AS SQL_VERSION";
// Executes the query
$stmt = $conn->query($tsql);
} catch (PDOException $exception1) {
echo "< h1 > Caught PDO exception:< / h1 > ";
echo $exception1->getMessage() . PHP_EOL;
echo "< h1 > PHP Info for troubleshooting< / h1 > ";
phpinfo();
}
?>
< h1 > Success Results : < / h1 >
< ?php
try {
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo $row['SQL_VERSION'] . PHP_EOL;
}
} catch (PDOException $exception2) {
// Display errors
echo "< h1 > Caught PDO exception:< / h1 > ";
echo $exception2->getMessage() . PHP_EOL;
}
unset($stmt);
unset($conn);
?>
```
Point your browser to https://localhost/testsql.php (https://localhost:8080/testsql.php on macOS). You should now be able to connect to your SQL Server/Azure SQL database. If you don't see a success message showing SQL version information, you can do some basic troubleshooting by running the script from the command line:
```bash
php testsql.php
```
If running from the command line is successful but nothing shows in your browser, check the [Apache log files ](https://linuxize.com/post/apache-log-files/#location-of-the-log-files ). For additional help, see [Support resources ](support-resources-for-the-php-sql-driver.md ) for places to go.