How to install PHP extension for Microsoft SQL Server under Fedora
They said PHP was dead.
PHP replied: ¯\_(ツ)_/¯
Regardless of PHP's health status, I found myself needing to connect to a Microsoft SQL Server via a PHP application running under Fedora. Finding concise details about installing the necessary drivers and extensions was not easy, so here is a blog post detailing how I did it.
NOTE: these instructions worked for me using Fedora versions 41 and 42.
Step 1 - Install Microsoft ODBC driver and required PHP packages
Issue the following commands:
curl https://packages.microsoft.com/config/rhel/9/prod.repo | sudo tee /etc/yum.repos.d/mssql-release.repo
sudo dnf update
sudo dnf install msodbcsql18 unixODBC-devel php-pear php-devel
Step 2 - Install PECL packages
Issue the following commands:
sudo pecl install sqlsrv
sudo pecl install pdo_sqlsrv
Step 3 - Enable the extensions
Issue the following commands:
sudo bash -c "echo 'extension=sqlsrv.so' > /etc/php.d/20-sqlsrv.ini"
sudo bash -c "echo 'extension=pdo_sqlsrv.so' > /etc/php.d/30-pdo_sqlsrv.ini"
Step 4 - Restart httpd and PHP services
Issue the following commands:
sudo systemctl restart httpd
sudo systemctl restart php-fpm
After Fedora release upgrade
Chances are that Fedora will introduce a new PHP version after a release upgrade. For example, when upgrading from Fedora 41 to 42, PHP was bumped from version 8.3 to 8.4. This will require you to uninstall and reinstall the PHP extensions. This can be achieved with the following commands:
sudo pecl uninstall sqlsrv
sudo pecl uninstall pdo_sqlsrv
sudo pecl install sqlsrv
sudo pecl install pdo_sqlsrv
sudo systemctl restart httpd
sudo systemctl restart php-fpm
Comments