PHP is great for quick and dirty productivity web apps. Recently I needed to get some data entry done for SQL Server, and it seemed like a great chance to install PHP 7 too.
Here are some common problems when trying to get IIS 10 (Win Server 2016), PHP 7 and the Microsoft SQL Server driver.
I mention specifically Windows Server, however this would work equally validly on regular desktop Windows.
When Web Platform Installer doesn’t work 💩
Something’s busted with Web Platform Installer (WebPI) – which up until now was how I always installed PHP on IIS.
Upon closer inspection, WebPI installs PHP 5.3 as default, which is way too old. And the error doesn’t look like something I can fix.
Recommendation: Don’t use WebPI to install PHP – it’s simple enough do it yourself with the steps listed here.
- Hit up your Server Manager (usually just sitting in your Start menu)
- Dashboard > Configure this local server > (2) Add roles and features
- Keep hitting Next until you get to Server Roles
- Tick Web Server (IIS) from the list, and also expand that selection because we need to choose a few more things
- Expand the Web Server subsection, then the Application Development subsection, and tick CGI
- In the other subsections, tick whatever else looks good for you
- Keep clicking through Next, then Install
Now IIS is installed – look for Internet Information Services (IIS) Manager in your start menu. The shortcut name is “inetmgr”
Install PHP 7
Find which download
The official source of downloads is the PHP for Windows site.
Specifically in our scenario, for IIS you will want to download binaries that are “non-thread-safe”. This is mentioned a fair bit in documentation.
So you’d pick the download which is x64, non-thread-safe and the most current version of PHP 7.1 (as of Dec 2016).
Note: I recommend picking the most recent version of PHP7 that aligns with the latest Microsoft SQL Server drivers. No point in choosing the newest PHP 7 release if there aren’t any available SQL Server drivers!
Copy the download over
You don’t really install it, but (as of 2016) you get a .zip folder with the contents inside. Extract and place these onto your hard drive. I prefer placing these at extracted contents into
C:\Program Files\PHP 7.1.0 but it wouldn’t matter if you chose differently.
Configure IIS and php.ini
You also need to go through the usual PHP for IIS configuration rigmarole:
Configure PHP FastCGI handler (presuming you installed CGI as part of IIS earlier). Note: When choosing the Executable, make sure you choose php-cgi.exe and not one of the DLLs. FastCGI must use that executable in order to work. If using the explorer dialog to pick the executable, you’ll need to select the bottom-right dropdown to show *.exe files.
Go into that folder where you installed PHP (e.g.
C:\Program Files\PHP 7.1.0) and you’ll see php.ini-development and php.ini-production.
Pick one, copy it and rename it to php.ini and this will become the main config file. In my situation, this was a non-production server, so I chose to copy the development .ini file.
Then open that .ini file. Here’s the basic stuff required for PHP on IIS to work:
fastcgi.impersonate = 1
cgi.fix_pathinfo = 1
cgi.force_redirect = 0
Here’s some stuff that will make your life easier:
extension_dir = "C:\Program Files\PHP 7.1.0\ext" ; or your PHP install location
error_log = "C:\inetpub\logs\php7\error.log"
open_basedir = "C:\inetpub\wwwroot\" ; or wherever your docroot is
That error log directory won’t exist yet, so go ahead and create the directory, and don’t forget to give the right permissions for the directory to be IIS-writeable.
Pro tip: Whenever you do some php.ini config changes on Windows/IIS, run an
iisresetto get these changes to show up. Open an Administrator command prompt/Powershell, and type
Test that bad boy works in one of two quick ways:
- Hit up command prompt, go to your PHP install folder, type
.\php-cgi.exe -vand see if it works
- Create a test webpage in your docroot (e.g.
C:\inetpub\wwwroot\info.php) and insert the
<?php phpinfo() ?>one-liner into the doc; then access it via browser.
Install and configure Microsoft SQL Server driver
I’ve found their GitHub page is the best and most reliable source of binary downloads: MSPHPSQL GitHub page
You could try your luck with the official-looking Microsoft driver download pages… but they’re sometimes often out of date.
After you download the zip package from GitHub, open it up to get the relevant driver files. Again we have a naming scheme for the binaries that’s similar to the PHP downloads. Choose what’s right for you.
sqlsrv are two ways of doing database interaction. I don’t use PHP Data Objects much, and prefer the specific sqlsrv_* syntax. Might as well install both.
Copy the driver (or drivers) into the ext subdirectory, then enable them by adding extra lines into php.ini. Note that the value name must be 100% the same as the filename, including the .dll file extension
extension = php_pdo_sqlsrv_71_nts.dll
extension = php_sqlsrv_71_nts.dll
Now do an
iisreset and check for startup errors. If none, you should be good to go.
If you happened to make a
phpinfo() page earlier, you can also quickly scroll through to see that the
pdo_sqlsrv extensions have correctly registered.
Common error messages
Going through this reminds me that I’ve been through the painful bits before…
If logging is enabled, you might see the following warnings in your log. Here’s how you fix those.
You selected the wrong architecture, or bits are missing
[17-Dec-2016 07:28:57 UTC] PHP Warning: PHP Startup: Unable to load dynamic library 'C:\Program Files\PHP 7.1.0\ext\php_pdo_sqlsrv_7_nts_x86.dll' - %1 is not a valid Win32 application.
For example you placed the x86 instead of the x64 on a 64-bit Windows Server, or vice versa.
Or it might silently fail if Visual C++ Redistributable for Visual Studio 2015 isn’t installed on your server (although Win Server 2016 should have this installed already). It might also fail if you don’t have the ODBC driver installed. One quick way is to check your installed applications (Win+R appwiz.cpl) and see if those are installed.
You’ve chosen the wrong version of the drivers
[17-Dec-2016 07:28:57 UTC] PHP Warning: PHP Startup: Unable to load dynamic library 'C:\Program Files\PHP 7.1.0\ext\php_sqlsrv_7_nts_x64.dll' - The specified <em><strong>procedure</strong> </em>could not be found.
This message means that it’s the right target architecture (x86 or x64) and all prerequisite .NET framework components are installed… BUT you’ve probably chosen the wrong version of the drivers (e.g. SQLSRV for PHP 7 rather than PHP 7.1, as in the error message above)
You’ve used the wrong DLL
[17-Dec-2016 07:43:45 UTC] PHP Warning: PHP Startup: Unable to load dynamic library 'C:\Program Files\PHP 7.1.0\ext\php_sqlsrv_7_ts_x64.dll' - The specified <em><strong>module</strong> </em>could not be found.
You’ve got a non-thread-safe installation of PHP, but you installed the thread-safe (note the “ts” in the filename).
Hopefully this clears up some of the confusing bits of PHP + SQL Server, and any thoughts you have would be welcome in the comments. 🎉