Skip to content

Instantly share code, notes, and snippets.

@LegitDongo
Last active January 26, 2023 16:57
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save LegitDongo/5f1f3d6f3c2508fcf1e8187d1b740f67 to your computer and use it in GitHub Desktop.
Save LegitDongo/5f1f3d6f3c2508fcf1e8187d1b740f67 to your computer and use it in GitHub Desktop.
SQLSRV PHP Drivers that work for Laravel Homestead
# Best if added to `after.sh` so that this gets run every time the box is provisioned
# Adjust versions as needed; most of this script is forcing php7.2
# composer won't be able to download updates with an active proxy, and will hang trying to get the newest file
# You may want to comment this out based on your network configuration
sudo bash -c 'echo "export NO_PROXY=*" >> ~/.profile'
sudo add-apt-repository ppa:ondrej/php -y
sudo apt-get update
sudo apt-get install php7.2-dev php7.2-xml mcrypt php-pear php-mbstring unixodbc unixodbc-dev -y --allow-unauthenticated
# Set up some pecl information so that it's forced to use php 7.2 instead of defaulting to newest
# This is also done with the "-d php_suffix=7.2" on the pecl commands below
# Use the below three commands to change it for the whole pecl environment
#sudo pecl config-set php_ini /etc/php/7.2/cli/php.ini
#sudo pecl config-set php_bin /usr/bin/php7.2
#sudo pear config-set php_suffix 7.2
sudo pecl -d php_suffix=7.2 install sqlsrv-5.3.0
sudo pecl -d php_suffix=7.2 install pdo_sqlsrv-5.3.0
sudo bash -c 'echo "extension=sqlsrv.so" > /etc/php/7.2/mods-available/sqlsrv.ini'
sudo bash -c 'echo "extension=pdo_sqlsrv.so" > /etc/php/7.2/mods-available/pdo_sqlsrv.ini'
# Link new ini to fpm
sudo bash -c 'ln -s /etc/php/7.2/mods-available/sqlsrv.ini /etc/php/7.2/fpm/conf.d/20-sqlsrv.ini'
sudo bash -c 'ln -s /etc/php/7.2/mods-available/pdo_sqlsrv.ini /etc/php/7.2/fpm/conf.d/30-pdo_sqlsrv.ini'
# Link new ini to cli
sudo bash -c 'ln -s /etc/php/7.2/mods-available/sqlsrv.ini /etc/php/7.2/cli/conf.d/20-sqlsrv.ini'
sudo bash -c 'ln -s /etc/php/7.2/mods-available/pdo_sqlsrv.ini /etc/php/7.2/cli/conf.d/30-pdo_sqlsrv.ini'
sudo service php7.2-fpm restart
sudo bash -c 'curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -'
### CHOOSE ONE FROM BELOW BASED ON VERSION
#Ubuntu 14.04
#sudo bash -c 'curl https://packages.microsoft.com/config/ubuntu/14.04/prod.list > /etc/apt/sources.list.d/mssql-release.list'
#Ubuntu 16.04
#sudo bash -c 'curl https://packages.microsoft.com/config/ubuntu/16.04/prod.list > /etc/apt/sources.list.d/mssql-release.list'
#Ubuntu 17.10
#sudo bash -c 'curl https://packages.microsoft.com/config/ubuntu/17.10/prod.list > /etc/apt/sources.list.d/mssql-release.list'
#Ubuntu 18.04
#sudo bash -c 'curl https://packages.microsoft.com/config/ubuntu/18.04/prod.list > /etc/apt/sources.list.d/mssql-release.list'
#Ubuntu 20.04
#sudo bash -c 'curl https://packages.microsoft.com/config/ubuntu/20.04/prod.list > /etc/apt/sources.list.d/msprod.list'
#Ubuntu 22.04
sudo bash -c 'curl https://packages.microsoft.com/config/ubuntu/22.04/prod.list > /etc/apt/sources.list.d/msprod.list'
sudo apt-get update
sudo ACCEPT_EULA=Y apt-get install msodbcsql17
# Change the default php cli command to point to 7.2 instead of newest
sudo update-alternatives --set php /usr/bin/php7.2
# Install Imagick (in case you need it)
#sudo apt-get install -y imagemagick php7.2-imagick
# Enable PDF for Imagick -- disabled due to security concerns
#sudo sed -i 's/rights="none" pattern="PDF"/rights="read|write" pattern="PDF"/g' /etc/ImageMagick-6/policy.xml
#sudo sed -i 's/rights="none" pattern="PDF"/rights="read|write" pattern="PDF"/g' /etc/ImageMagick/policy.xml
sudo service php7.2-fpm restart
exit
@LESMin
Copy link

LESMin commented Oct 21, 2018

I've been looking for a few days.

Thank you very much.

@jthorpe-marco
Copy link

You're a lifesaver. I wasted way too much time trying to figure this out.

@jthorpe-marco
Copy link

I had to go through this again recently and found the sqlsrv-5.3.0 and pdo_sqlsrv-5.3.0 steps throw an error, likely due to a MS update or possibly the newer version of homestead. The easiest fix I found was running this script except removing the -5.3.0 from the sqlsrv and pdo_sqlsrv lines, and then copying my old sqlsrv.so and pdo_sqlsrv.so files into /usr/lib/php/20170718/.

I put the files here in case anyone has trouble finding them:
pdo_sqlsrv.so
http://s000.tinyupload.com/?file_id=47415146643776589949
sqlsrv.so
http://s000.tinyupload.com/?file_id=19590720215516452158

@andreich1980
Copy link

Will this work for php 7.3? Can I just replace php version in the script?

@LegitDongo
Copy link
Author

Yes, you should be able to just replace the versions and it work fine
Part of the code in here is just for forcing PHP 7.2 anyway.

@andreich1980
Copy link

The latest ver of sqlsrv & pdo_sqlsrv 5.6.1 for today

@kutz4thesoul
Copy link

Worked like a charm! Thank you so much! This saved me a TON of time!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment