- Deep Shift Labs Development Blog - http://www.deepshiftlabs.com/dev_blog -

Install PHP 5.4.6 on IIS 7 to work with MS SQL 2008

Posted By On 2 September 2012 @ 3:19 | Comments Disabled

Instal PHP 5.4.6 on IIS 7 to work with MS SQL 2008

I decided to write this post because I have not found a simple and clear description for installing PHP on Windows, and setting this not very common combination to work with MS SQL 2008.

We are currently working on porting the largest system ever created in Deep Shift Labs from Windows 2000 and MS SQL 2000 to Windows 2008 and MS SQL 2008. I want to speak briefly about the history of this system and why we actually needed to install PHP on Windows. The system is about 800K lines of PHP code and it has been helping people to solve their business needs for the last 10 years. This system is designed for one of our customers. The first few years, the system worked on FreeBSD, Apache and PostgreSQL. Then the client’s company went public, and they needed to switch to commercial software. Sometime in 2004, we have been able, thanks to the fact that we originally used ADODB [1], to move, with a little effort, from FreeBSD/Apache to Windows 2000/IIS 5 (PHP runs as an ISAPI module). Finally, in 2006 we moved the then small database, both in structure and in size, to MS SQL 2000 from PostgreSQL.

The most difficult part in the distant 2006 was to find a driver which is bug free with MS SQL. We had tried everything supported by ADODB back then, and, in the end, we decided in favor of php_dblib.dll suggested and maintained by Frank Kromann. Frank [2] was maintaining [3] MS SQL extensions in PHP project and helped us immensely. The php_mssql.dll driver was using ntwdblib.dll from Microsoft, that does not work with dates and long texts properly. php_dblib.dll used dblib.dll from FreeTDS and we have not found any problems in it and started using it.

And now came the moment when the customer wants to move to the operating system and database that have Microsoft support (Windows 2000 and MS SQL 2000 are not supported anymore) and such a move, in general, is a common requirement in large enterprises.

Naturally, in the ADODB 4.93 we have been using since 2006, there is no mssqlnative driver support and so the challenge was set as follows:

- Move the database from MS SQL 2000 to MS SQL 2008
- Move the web server from Windows 2000 and IIS 5 (ISAPI) on Windows 2008 and IIS 7 (FastCGI)
- Upgrade PHP 5.1.6 to PHP 5.4.6
- Upgrade ADODB from 4.93 to 5.17 with transition to the new driver ‘mssqlnative’
- To test the system, including the two replications processes to our database from servers running MS SQL 2000 and MS SQL 2005.

This is one of the installation methods (obviously the one we used).

1. Create C:\PHP and C:\tmp folders
2. Load non thread safe PHP version recommended for use here [4] and here [5]. The file will contain _nts_ in the name, for example – php-5.4.6-nts-Win32-VC9-x86.zip (latest version available at the time of writing this post) from the site http://windows.php.net/download/ [6].
3. Unpack it in C:\PHP
4. Load latest [7] drivers from MS SQL Microsoft (file SQLSRV30) and extract it to C:\PHP\ext
5. Download the PHP web installer [8] from Microsoft. They offer two versions and I used the latter one available with PHP 5.3.13. Install it.

You can ask a reasonable question why we install 5.3.13 to switch to 5.4.6. There are two reasons. First – I did not manage to set PHP 5.4.6 and manually configure IIS. Despite my attempts to let PHP see its php.ini file inside the C:\PHP I failed to do so. According to all the manuals you need to create a variable PHPRC assigning the path to php.ini, but in my case, someone has done something wrong.

PHPRC did not work for me

Since PHP couldn’t see the configuration file, MS SQL driver was not loaded. Being stuck at this point for a couple of hours I asked for help in the forum and started looking for alternatives, waiting until America wakes up and sees my ‘SOS’ message in the forum. It was then that I accidentally came across a site [8] where I found the alternative I was looking for. Maybe I would not have started using the installer if it had not offered to set this magical and wonderful PHP Manager [9] I could not resist at the thought of trying. In the end I found that the PHP Manager allows adding PHP 5.4.6, making its php.ini file perfectly visible.

Microsoft PHP web installer

It installs all the necessary components as well. It took some 10 years for Microsoft to add some really cool things to run PHP on Windows – here you have MS SQL driver and the installer which adds a special PHP Manager inside IIS, and even Windows Cache [10] PHP accelerator for IIS.

PHP Manager

After installation, we have PHP 5.3.13 installed in the Program Files and PHP Manager inside IIS.

PHP Manager dashboard

As you can see from the PHP Manager, you can control the configuration parameters from php.ini, and the extensions are shown in a separate page. Also here we see what php.ini file is being used and where the error logs are written. There is quick access to the FastCGI handler created by the installer. It is possible to add other versions of PHP and switch between them. We have not tested if it is possible to use PHP Manager to set different PHP versions on different virtual hosts. I think this and much more, such as to use different php.ini files for different virtual hosts can be done manually using the excellent article [5] by Ruslan Yakushev from Microsoft. We just needed to add PHP version 5.4.6 to “live happily ever after”.

Adding PHP 5.4.6

Now we can add PHP 5.4.6, which we put into in C:\PHP. After adding, we see that IIS now uses a new version of PHP, and that PHP Manager created a new handler, which is used (we do not see it here), but we can check with phpinfo(), which PHP manager can dynamically generate for us (how-cool-it-is!).

PHP 5.4.6 can see php.ini now

Now it is time to look at PHP extensions (modules). I removed mysql and added both versions of the MS SQL driver sqlsrv and pdo_sqlsrv. I forgot to check if IIS sees our changes immediately. I am used to executing “iisreset” after changing configuration.

PHP Manager extensions UI

PHP Manager offers two pre-defined configuration options for error handling for development and production.

PHP Manager error reporting UI

When I changed the path to the error log, PHP Manager did not see my changes, and I had to restart the IIS. I really got used to the old fashioned way for a folder for uploaded files, session and logs and store them in PHP C:\tmp. No wonder our periodic scripts are in C:\etc. Options ‘Configure error reporting’ and ‘Set runtime limits’ are kept separately in PHP Manager for convenience only, as they all (except for the extension modules) are available from the ‘Manage all settings’ panel.

PHP Manager points to a wrong location after PHP version switch

If you need to parse HTML files with PHP – create one more mapping by looking at parameters of existing PHP mapping you already have.

Setting PHP to handle HTML files (if needed)

Now we can try to connect to MS SQL with the driver only and via ADODB. First you need to make sure the server is configured to allow TCP/IP as well as port 1433 is open on the Windows firewall.

Checking SQL Server networking

In my case it was not so. If you cannot connect from PHP, it is recommended to install SQLCMD and use it for testing. For example, here [11] we will find the required version of SQLCMD.

I want to draw your attention to the fact that the new Microsoft driver returns datetime as PHP DateTime:: [12] object. But we use ADODB, which does this little trick converting this object into a string on line 794 of adodb-mssqlnative.inc.php file:

View Code [13] PHP
$this->fields[$key] = $value->format("Y-m-d\TH:i:s\Z");

Thus, returning us the string ’2012-08-22T23:42:22Z ‘instead of the usual ’2012-08-22 23:42:22′ and so for backward compatibility, you can correct adodb-mssqlnative.inc.php this way:

View Code [13] PHP
if(is_array [14]($this->fields)) {
    foreach($this->fields as $key=>$value) {
        if (is_object [15]($value) && method_exists [16]($value, 'format')) {//is DateTime object
	    //$this->fields[$key] = $value->format("Y-m-d\TH:i:s\Z");
            //DSL DateTime output format fix
            $this->fields[$key] = $value->format("Y-m-d H:i:s");

Here is an example, where you can find connection and query snippets which use Microsoft driver directly and via ADODB.

View Code [13] PHP
echo [17] "<B>Direct driver connection</b><BR>";
$connectionInfo = array [18](
/* Connect using SQL Server Authentication. */
$conn = sqlsrv_connect(', 1433', $connectionInfo);
if( $conn === false )
   echo [17] "Unable to connect.</br>";
   die [19]( print_r [20]( sqlsrv_errors(), true));
$tsql = "SELECT GETDATE() AS today";
$stmt = sqlsrv_query( $conn, $tsql);
if( $stmt === false )
   echo [17] "Error in executing query.</br>";
   die [19]( print_r [20]( sqlsrv_errors(), true));
$row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
echo [17] "Today is ".$row['today']->format('Y-m-d H:i:s')."</br>";
sqlsrv_free_stmt( $stmt);
sqlsrv_close( $conn);
ini_set [21]( "include_path", ini_get [22]( "include_path" ) . ";" . $_SERVER["DOCUMENT_ROOT"] . "/include" );
$user     = "aws_user";
$password = "Q1w2e3r4t5";
$server=", 1433";
$database = "AWS_Database";
echo [17] "<B>ADODB MSSQL Native connection</b><BR>";
$adoConnection = ADONewConnection('mssqlnative');
//$adoConnection->debug = true;
$adoConnection->Connect($server, $user, $password, $database);
$rs = $adoConnection->Execute($tsql);
echo [17] "Today is ".$rs->fields['today']."</br>";

In conclusion, we want to say that the Microsoft site provides a lot of useful information for running PHP on IIS and using Microsoft drivers to connect to MS SQL. For example, here [23] you will find the entry to the forum PHP/IIS, and other useful links. Forums dedicated to PHP driver for MS SQL databases are located here [24]. I used them more [25]-than-once [26]. They are active and guys helped me a lot.

Finally a couple of handy articles on how to configure PHP on IIS:
- Enable Per-Site PHP Configuration on IIS 7 and IIS 6.0 [27]
- Using FastCGI to Host PHP Applications on IIS [5]
- Using FastCGI to Host PHP Applications on IIS 6.0 [28]


Article printed from Deep Shift Labs Development Blog: http://www.deepshiftlabs.com/dev_blog

URL to article: http://www.deepshiftlabs.com/dev_blog/?p=1880&lang=en-us

URLs in this post:

[1] ADODB: http://adodb.sourceforge.net/

[2] Frank: http://www.linkedin.com/in/webbypixel

[3] maintaining: http://php.net/credits.php

[4] here: http://www.php.net/manual/en/install.windows.iis7.php

[5] here: http://learn.iis.net/page.aspx/246/using-fastcgi-to-host-php-applications-on-iis/

[6] http://windows.php.net/download/: http://windows.php.net/download/

[7] latest: http://www.microsoft.com/en-us/download/details.aspx?id=20098

[8] web installer: http://www.microsoft.com/web/platform/phponwindows.aspx

[9] PHP Manager: http://phpmanager.codeplex.com/

[10] Windows Cache: http://blogs.iis. net/robert_mcmurray/archive/2012/07/26/windows-cache-1-3-for-for-php-5-4.aspx

[11] here: http://www.microsoft.com/en-us/download/details.aspx?id=16978#SQLCMD

[12] DateTime::: http://www.php.net/manual/en/class.datetime.php

[13] View Code: http://www.deepshiftlabs.com/dev_blogjavascript:;

[14] is_array: http://www.php.net/is_array

[15] is_object: http://www.php.net/is_object

[16] method_exists: http://www.php.net/method_exists

[17] echo: http://www.php.net/echo

[18] array: http://www.php.net/array

[19] die: http://www.php.net/die

[20] print_r: http://www.php.net/print_r

[21] ini_set: http://www.php.net/ini_set

[22] ini_get: http://www.php.net/ini_get

[23] here: http://php.iis.net/

[24] here: http://social.msdn.microsoft.com/Forums/en-US/sqldriverforphp/threads

[25] more: http://social.msdn.microsoft.com/Forums/en-US/sqldriverforphp/thread/b8ede10a-a679-49d0-b748-0a5944409d8f

[26] once: http://social.msdn.microsoft.com/Forums/en-US/sqldriverforphp/thread/0e023a13-aac2-4114-a8ac-178e54427f39

[27] Enable Per-Site PHP Configuration on IIS 7 and IIS 6.0: http://learn.iis.net/page.aspx/745/enable-per-site-php-configuration-on-iis-60-and-iis-7-and-above/

[28] Using FastCGI to Host PHP Applications on IIS 6.0: http://learn.iis.net/page.aspx/247/using-fastcgi-to-host-php-applications-on-iis-60/

Content copyright © 2010 Deep Shift Labs.