Installation, updates, general problem solving and assistance.
User avatar
!
30%
Posts: 3259
Joined: 2013-02-25 18:36

2013-07-12 10:56 »

Updated (2016-08-01), see bottom of this post.

In this thread, I will explain how to "currently" get PHP 5.5.0 x86 to work with Windows Server 2012 x64 + IIS 8.x + Microsoft SQL Server 2012 x64. Took me many many many hours to finally get this thing working. I will first explain how to install PHP on the Windows Server 2012 and then how to get it to connect to MS SQL with the "native" SQL login and also with the Windows authentication.

I know the thread seems long but if you read it slowly and carefully, you won't panic! :lol:

Hopefully, this thread will prevent people to waste many hours of their lives. :D

1. How to install PHP on IIS 8.x in Windows Server 2012.

We currently use the VC11 x86 Non Thread Safe version 5.5.0, downloaded from http://windows.php.net/download.

You must install x86 Visual C++ Redistributable for Visual Studio 2012. (Otherwise, you will get odd security errors)

I will attach a sample working php.ini at the bottom of this thread as well for you. Please note that it has the MS SQL server extensions activated in it. It uses "c: emp" as temp folder and error logging "error_log = "c: emperror.php.txt"".

So, you download the PHP .zip-file, unpack it in some folder of your choice.

To get PHP working in IIS 8.x, you need to install CGI and ISAPI stuff:

iis01.PNG
iis01.PNG (45.28 KiB) Viewed 187523 times

From your Internet Information Services (IIS) Manager, you find this icon:

iis02.PNG
iis02.PNG (38.32 KiB) Viewed 187523 times

Inside that icon, you click on "Add Module Mapping..." and add .php as an extension:

iis03.PNG
iis03.PNG (65.5 KiB) Viewed 187523 times

Like this:

iis04.PNG
iis04.PNG (11.01 KiB) Viewed 187523 times

There is also this icon "FastCGI Settings" where you can alter some settings, for example "Instance MaxRequests" etc.:

iis05.PNG
iis05.PNG (24.25 KiB) Viewed 187523 times

iis06.PNG
iis06.PNG (34.58 KiB) Viewed 187523 times

2. How to get PHP work with MS SQL.

**********
UPDATE: Attached updated drivers for 5.6.0 "sqlsrv_unofficial_3.0.2.2.zip", found it from http://robsphp.blogspot.co.uk/2012/06/u ... river.html and cached it in here. Bottom of this post.
**********

I use MS SQL Express 2012. You must have the SQL 2012 Native Client installed. As you can see, I have removed a lot of "crap" from our SQL installation. :lol:

installed.PNG
installed.PNG (44.55 KiB) Viewed 187523 times

Normally you need to download Microsoft Drivers X.X for PHP for SQL Server from Microsoft but they currently only host the version 3.0.1 (it says 3.0 for some reason) [Yeah, I know. Complete fools over there at Microsoft.]

This version does NOT work with PHP 5.5.0 x86, however! Lovely, isn't it? (I never tried PHP x64 because it states that they are experimental for now)

Thanks to a somewhat obscure thread at http://social.technet.microsoft.com/For ... -2012-iis8 I got it working. Some guy called "hmelihkara" recompiled them to work with the 5.5.0 version! Bless his soul for this!

I will attach his drivers to bottom of this thread in case the links on those threads die in the future. The .zip-file (php_sqlsrv_55.zip) contains 4 DLLs, 2 for "Thread Safe" and 2 for "Non Thread Safe" versions of PHP 5.5.0 x86. (Maybe it works with x64 too, I have no idea) These drivers seem to have version 3.0.2.

I hope the following can assist the Microsoft MVPs roll out a PHP 5.5.

It is a simple issue. PHP 5.5 was compiled with 2012 C++ Runtime. Therefore when trying to load php_pdo_sqlsrv_54_nts.dll on Windows 2012 IIS 8.0 the following error is reported in the PHP Error Log....

PHP Warning: PHP Startup: pdo_sqlsrv: Unable to initialize module Module compiled with module API=20100525 PHP compiled with module API=20121212 These options need to match in Unknown on line 0

I believe php_pdo_sqlsrv_54_nts.dll needs to be recompiled to VC11 2012 C++ Runtime and become php_pdo_sqlsrv_55_nts.dll.

At this time, we cannot move to PHP 5.5 due to our sqlsrv driver not working.

John
Monday, June 24, 2013 7:00 PM

Ok, I compile for both normal and pdo drivers with Thread Safe (x86) / Non Thread Safe (x86).
I testes Non Thread Safe PDO driver with php 5.5 (x86) and it's working.
You can download files here:
http://www.hmelihkara.com/files/php_sqlsrv_55.rar
Also I change file versions to 3.0.2 to solve confusion with the old versions.
----------------------------------
Legal:
This is a project of Microsoft Corporation and as the version.h file writes, I've permission to change the source code and distribute it with giving the details that I made and giving the copy of the license here: http://www.apache.org/licenses/LICENSE-2.0
Changes:
Line 77 of php_sqlsrv.h changes from:

#if PHP_MAJOR_VERSION > 5 || PHP_MAJOR_VERSION < 5 || ( PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION < 3 ) || ( PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION > 4 )

to:

#if PHP_MAJOR_VERSION > 5 || PHP_MAJOR_VERSION < 5 || ( PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION < 4 ) || ( PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION > 5 )

Wednesday, June 26, 2013 9:43 AM

To make PHP use the drivers, you need to add these two lines to your php.ini file:

[PHP_SQLSRV]
extension=php_sqlsrv_55_nts.dll
[PHP_PDO_SQLSRV]
extension=php_pdo_sqlsrv_55_nts.dll


Bear in mind that the DLL-files must be inside your "ext" folder. I got my drivers in "c:<folder>phpext" and my php.ini says:
extension_dir = "./ext/"

To see if the drivers actually loaded by your PHP, try running this code:

<? echo phpinfo(); ?>

If you do not see the following two sections, it means PHP has NOT loaded your drivers!

Remember: You must restart IIS for it to load the drivers after you added the lines for them in your php.ini file.


sqlsrv1.PNG
sqlsrv1.PNG (6.58 KiB) Viewed 187523 times

sqlsrv2.PNG
sqlsrv2.PNG (8.55 KiB) Viewed 187523 times

Here is a sample code to test the SQL Server connection:

Code: Select all

//You can probably change (local) to your database server's IP etc.
$serverName = "(local)";
//If you are using Windows authentication, then you don't need to give it any user id and password
//$connectionInfo = array( "Database"=>"YOURDATABASENAME");
$connectionInfo = array("UID" => "YOURDATABASELOGINUSERNAME", "PWD" => "YOURDATABASELOGINPASSWORD", "Database"=>"YOURDATABASENAME");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn ) {
    echo "Connection established.
";
} else {
    echo "Connection could not be established.
";
    die( print_r( sqlsrv_errors(), true));
}
sqlsrv_close( $conn);

3. How to use Windows authentication with MS SQL and PHP instead of regular SQL logins.

You must set this variable in php.ini like this:
fastcgi.impersonate = 0;

You must create a Windows user account which you can create from the "Computer management".

iisapppool01.PNG
iisapppool01.PNG (12.3 KiB) Viewed 187523 times

Then you must create a new application pool in IIS (From IIS Manager) and allow your site to run with that application pool.

You need to set an identity to your application pool. This "identity" is the Windows user account that you created from the "Computer management". Upon creation of your Windows user account, you also set a password to it. You will need that password when you add the user to your application pool in IIS.

iisapppool02.PNG
iisapppool02.PNG (14.62 KiB) Viewed 187523 times

iisapppool03.PNG
iisapppool03.PNG (59.51 KiB) Viewed 187523 times

iisapppool04.PNG
iisapppool04.PNG (12.36 KiB) Viewed 187523 times

Now you can create your new site and choose so it uses the application pool which you created earlier:

iisapppool05.PNG
iisapppool05.PNG (16.95 KiB) Viewed 187523 times

Of course, you must add the Windows account which you created as an SQL login and allow it to have access to the chosen database. From the "SQL Server Management Studio" where you manage your databases (you get the Management Studio from the MS SQL Express install, from their Web site you can choose to download it), you go to "Security", "Logins", "New login". There you add your Windows account user, choose some "User mapping" to give it access to databases and also set a "Default database" for the user.

sql01.PNG
sql01.PNG (10.03 KiB) Viewed 187523 times

sql02.PNG
sql02.PNG (38.2 KiB) Viewed 187523 times

Here is the code to test it:

Code: Select all

//You can probably change (local) to your database server's IP etc.
$serverName = "(local)";
$connectionInfo = array( "Database"=>"YOURDATABASENAME");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn ) {
    echo "Connection established.
";
} else {
    echo "Connection could not be established.
";
    die( print_r( sqlsrv_errors(), true));
}
sqlsrv_close( $conn);

As you can see, the code is almost the same as the one I mentioned above but this line is now removed:

$connectionInfo = array("UID" => "YOURDATABASELOGINUSERNAME", "PWD" => "YOURDATABASELOGINPASSWORD", "Database"=>"YOURDATABASENAME");

and we use this instead:

$conn = sqlsrv_connect( $serverName, $connectionInfo);

Because now IIS will use the Windows account which we gave to the application pool in IIS, which is connected to the Web site you created. To be clear:

You created an application pool.
You attached a Windows user account to that application pool.
You created a Web site and attached that application pool to that Web site in IIS.
By setting "fastcgi.impersonate = 0;" you make PHP use the attached Windows user account when running your PHP and connecting to MS SQL Server.

:idea: I will most likely update and edit this thread in the future but for now, this will do. :idea:

php_sqlsrv_55.zip
(279.52 KiB) Downloaded 1856 times

php.ini.zip
(21.43 KiB) Downloaded 1365 times

sqlsrv_unofficial_3.0.2.2.zip
(1.82 MiB) Downloaded 1087 times

Update (2015-10-06):

The following is tested with PHP version 5.6.14 and it works.

They have updated the MSSQL driver for PHP but you also need the Microsoft� ODBC Driver 11 for SQL Server� for Windows installed on the server to make it work.

https://www.microsoft.com/en-us/downloa ... x?id=36434
https://www.microsoft.com/en-us/downloa ... x?id=36434

msodbcsqlx64.zip
(3.35 MiB) Downloaded 2845 times

msodbcsqlx86.zip
(1.85 MiB) Downloaded 1205 times

https://msdn.microsoft.com/en-us/data/ff657782.aspx
https://www.microsoft.com/en-us/downloa ... x?id=20098
https://github.com/azure/msphpsql

php_sqlsrv.zip
(976.62 KiB) Downloaded 1232 times

Update (2016-08-01):

The following is tested with PHP version 7.0.9 and it works.

Updated MSSQL drivers for PHP. You also need the Microsoft ODBC Driver 11 or 13 for SQL Server. I used the x64 version of the ODBC driver.

For PHP, 7.0.9, I tried the NTS (Non Thread Safe), both x86 and x64, both work. ( I used the native, not the "PDO" dll files. )

For PHP, 7.0.9, you need the "Visual C++ Redistributable for Visual Studio 2015", version 14 of it.

https://github.com/Azure/msphpsql/tree/PHP-7.0
https://www.microsoft.com/en-us/downloa ... x?id=36434
https://www.microsoft.com/en-us/downloa ... x?id=50420
https://www.microsoft.com/en-us/downloa ... x?id=48145

php_sqlsrv_7_ts_x86.dll
(326.69 KiB) Downloaded 2383 times

php_sqlsrv_7_ts_x64.dll
(400.19 KiB) Downloaded 1403 times

php_sqlsrv_7_nts_x86.dll
(322.69 KiB) Downloaded 1485 times

php_sqlsrv_7_nts_x64.dll
(396.19 KiB) Downloaded 1046 times

php_pdo_sqlsrv_7_ts_x86.dll
(314.19 KiB) Downloaded 1146 times

php_pdo_sqlsrv_7_ts_x64.dll
(381.19 KiB) Downloaded 1071 times

php_pdo_sqlsrv_7_nts_x86.dll
(312.69 KiB) Downloaded 1122 times

php_pdo_sqlsrv_7_nts_x64.dll
(379.69 KiB) Downloaded 1470 times

msodbcsql.11.x64.msi
(3.68 MiB) Downloaded 1604 times

msodbcsql.13.x64.msi
(4.07 MiB) Downloaded 2003 times

vc_redist.x86.14.exe
(13.13 MiB) Downloaded 886 times

vc_redist.x64.14.exe
(13.9 MiB) Downloaded 901 times

vcredist_x64.11.exe
(6.85 MiB) Downloaded 2629 times

vcredist_x86.11.exe
(6.25 MiB) Downloaded 2306 times

User avatar
TmEE
VIP
Posts: 229
Joined: 2013-08-09 16:52
Contact:

2013-08-16 12:12 »

I could have used this thread in the end of last year when I was doing stuff at school ^^

Very nice writeup on the procedure !

User avatar
!
30%
Posts: 3259
Joined: 2013-02-25 18:36

2013-08-16 14:54 »

Hehe thnx! Yeah, very hard to find a complete guide. It took me so many hours, it wasn't even funny! Had to collect bits and pieces from here or there. I decided it was time to make a real guide for people in the future with the same problem. I hate guides where some small but crucial step is missing. :lol:

User avatar
!
30%
Posts: 3259
Joined: 2013-02-25 18:36

2013-08-19 01:44 »

I can confirm that the above works with PHP 5.5.1 and 5.5.2 as well.

Sv2

2013-09-17 18:17 »

This also works for PHP 5.5.3 (x32) + Apache 2.4.6 (x32, VC11) on Win7x64.

User avatar
!
30%
Posts: 3259
Joined: 2013-02-25 18:36

2013-10-02 11:51 »

The above still works, PHP 5.5.4.

User avatar
!
30%
Posts: 3259
Joined: 2013-02-25 18:36

2013-10-18 05:20 »

The above solution still works, PHP 5.5.5.

aar0nsky

2013-10-27 07:06 »

Very helpful thank you tons.

User avatar
!
30%
Posts: 3259
Joined: 2013-02-25 18:36

2013-11-14 17:53 »

Still works, PHP 5.5.6. ::thumbup::

User avatar
!
30%
Posts: 3259
Joined: 2013-02-25 18:36

2013-12-16 14:32 »

PHP 5.5.7! ::thumbup::

Post Reply