To install the sqlsrv and pdo_sqlsrv drivers on Ubuntu using Apache, follow these steps. These instructions use PHP 8.4 by default; replace 8.4 with 8.3 or 8.5 if using those versions.
- Install PHP: Add the
ppa:ondrej/php repository and install PHP 8.4, dev tools, and XML support. - Install Prerequisites: You must manually install the Microsoft ODBC driver for SQL Server for Linux following the official Microsoft documentation.
- Install Drivers via PECL: Use
pecl install for both sqlsrv and pdo_sqlsrv, then create the necessary .ini files in mods-available and enable them using phpenmod. - Configure Apache: Install
libapache2-mod-php8.4 and switch the MPM module from mpm_event to mpm_prefork to support PHP. - Restart: Restart the Apache service to apply changes.
# Step 1: Install PHP
sudo su
add-apt-repository ppa:ondrej/php -y
apt-get update
apt-get install php8.4 php8.4-dev php8.4-xml -y --allow-unauthenticated
# Step 3: Install Drivers
sudo pecl install sqlsrv
sudo pecl install pdo_sqlsrv
sudo su
printf "; priority=20\nextension=sqlsrv.so\n" > /etc/php/8.4/mods-available/sqlsrv.ini
printf "; priority=30\nextension=pdo_sqlsrv.so\n" > /etc/php/8.4/mods-available/pdo_sqlsrv.ini
exit
sudo phpenmod -v 8.4 sqlsrv pdo_sqlsrv
# Step 4: Configure Apache
sudo su
apt-get install libapache2-mod-php8.4 apache2
a2dismod mpm_event
a2enmod mpm_prefork
a2enmod php8.4
exit
# Step 5: Restart
sudo service apache2 restart