Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save denihida1216/07cbe54eb5e5261e6fb24476add63c0c to your computer and use it in GitHub Desktop.
Save denihida1216/07cbe54eb5e5261e6fb24476add63c0c to your computer and use it in GitHub Desktop.
Linux and macOS Installation Tutorial for the Microsoft Drivers for PHP for SQL Server
https://docs.microsoft.com/en-us/sql/connect/php/installation-tutorial-linux-mac?view=sql-server-ver15
https://docs.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-ver15
Step 1. Install PHP (Ubuntu)
sudo su
add-apt-repository ppa:ondrej/php -y
apt-get update
apt-get install php8.0 php8.0-dev php8.0-xml -y --allow-unauthenticated
Step 2. Install prerequisites (Ubuntu)
Install the ODBC driver for Ubuntu by following the instructions
https://docs.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-ver15
Step 3. Install the PHP drivers for Microsoft SQL Server (Ubuntu)
sudo pecl install sqlsrv
sudo pecl install pdo_sqlsrv
sudo su
printf "; priority=20\nextension=sqlsrv.so\n" > /etc/php/8.0/mods-available/sqlsrv.ini
printf "; priority=30\nextension=pdo_sqlsrv.so\n" > /etc/php/8.0/mods-available/pdo_sqlsrv.ini
exit
sudo phpenmod -v 8.0 sqlsrv pdo_sqlsrv
Step 4. Install Apache and configure driver loading (Ubuntu)
sudo su
apt-get install libapache2-mod-php8.0 apache2
a2dismod mpm_event
a2enmod mpm_prefork
a2enmod php8.0
exit
Step 5. Restart Apache and test the sample script (Ubuntu)
sudo service apache2 restart
Step 1. Install PHP (Ubuntu with PHP-FPM)
sudo su
add-apt-repository ppa:ondrej/php -y
apt-get update
apt-get install php8.0 php8.0-dev php8.0-fpm php8.0-xml -y --allow-unauthenticated
Verify the status of the PHP-FPM service by running:
systemctl status php8.0-fpm
Step 2. Install prerequisites (Ubuntu with PHP-FPM)
Install the ODBC driver for Ubuntu by following the instructions on
https://docs.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-ver15
Step 3. Install the PHP drivers for Microsoft SQL Server (Ubuntu with PHP-FPM)
sudo pecl config-set php_ini /etc/php/8.0/fpm/php.ini
sudo pecl install sqlsrv
sudo pecl install pdo_sqlsrv
sudo su
printf "; priority=20\nextension=sqlsrv.so\n" > /etc/php/8.0/mods-available/sqlsrv.ini
printf "; priority=30\nextension=pdo_sqlsrv.so\n" > /etc/php/8.0/mods-available/pdo_sqlsrv.ini
exit
sudo phpenmod -v 8.0 sqlsrv pdo_sqlsrv
If there is only one PHP version in the system, then the last step can be simplified to phpenmod sqlsrv pdo_sqlsrv.
Verify that sqlsrv.ini and pdo_sqlsrv.ini are located in /etc/php/8.0/fpm/conf.d/:
ls /etc/php/8.0/fpm/conf.d/*sqlsrv.ini
Restart the PHP-FPM service:
sudo systemctl restart php8.0-fpm
Step 4. Install and configure nginx (Ubuntu with PHP-FPM)
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;
Next, uncomment, and modify the section following # pass PHP scripts to FastCGI server as follows:
# pass PHP scripts to FastCGI server
#
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.0-fpm.sock;
}
Step 5. Restart nginx and test the sample script (Ubuntu with PHP-FPM)
sudo systemctl restart nginx.service
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment