[PHP] Install PHP on Windows with IIS

This Tutorial assumes that you are running Windows 7 64-bit (x64) or Windows 8.1 64-bit (x64).

TL;DR All download links:

PHP 5.6
http://windows.php.net/download/
http://windows.php.net/downloads/releases/php-5.6.9-nts-Win32-VC11-x86.zip

VC++ 11 Redistributable
http://www.microsoft.com/en-us/download/details.aspx?id=30679

SQLSRV Extension for PHP
https://onedrive.live.com/?cid=669ee24817961774&id=669EE24817961774!720

SQL Native Client 11
http://www.microsoft.com/download/en/details.aspx?id=29065
sqlncli32.msi http://go.microsoft.com/fwlink/?LinkID=239647&clcid=0x409
sqlncli64.msi http://go.microsoft.com/fwlink/?LinkID=239648&clcid=0x409


Windows 7

Windows 8


##Enable IIS with the following Features##

Windows 7

Control Panel\Programs\Programs and Features > Turn Windows Features On or Off

Windows 8

Control Panel\All Control Panel Items\Programs and Features > Turn Windows Features On or Off


##Ensure IIS is functioning##

Use a Browser and navigate to http://localhost/

Windows 7

Windows 8

3 Likes

This topic is now unlisted. It will no longer be displayed in any topic lists. The only way to access this topic is via direct link.

##Download required components for PHP##

PHP 5.6 NTS 32-bit VC11

Even though you are running a 64-bit OS and probably SQL Express 64-bit, the recommendation for running PHP on IIS is to use the 32-bit version of PHP. You want the Non Thread-Safe version (NTS) as opposed to the Thread-Safe (TS) version.

http://windows.php.net/download/
http://windows.php.net/downloads/releases/php-5.6.9-nts-Win32-VC11-x86.zip


Visual C++ Redistributable for Visual Studio 2012 (VC11)

These are required by PHP. If you are getting some strange error that IIS cannot display the PHP page, chances are you forgot to install this.

You really only need the 32-bit version since we are running 32-bit PHP. However, you can safely install both and I recommend that you do so.

http://www.microsoft.com/en-us/download/details.aspx?id=30679

##Download components for SQL Express connectivity via PHP##

Unofficial SQL Drivers for PHP

This is a PHP Extension that allows connecting to SQL Express Databases using PHP.


https://onedrive.live.com/?cid=669ee24817961774&id=669EE24817961774!720


SQL Native Client 2012 (v11) or 2008 R2 (v10)

The SQL Native Client is required by the SQL Drivers for PHP.

2012 (v11) is used for SQL Express 2012 or 2014


sqlncli32.msi http://go.microsoft.com/fwlink/?LinkID=239647&clcid=0x409
sqlncli64.msi http://go.microsoft.com/fwlink/?LinkID=239648&clcid=0x409

2008 (v10) is used for SQL Express 2008 or 2008 R2
http://www.microsoft.com/download/en/details.aspx?id=16978
sqlncli32.msi http://go.microsoft.com/fwlink/?LinkID=188400&clcid=0x409
sqlncli64.msi http://go.microsoft.com/fwlink/?LinkID=188401&clcid=0x409

1 Like

##PHP Installation and Configuration##

Extract PHP

Extract your PHP ZIP file to your desired location. Most people recommend C:\php but it can be anywhere you like. It is best to keep the path short. In this case, I wanted my installation in D:\SITES\php … here is a partial list of the files that you should see - there are many more than this however…


Run the VC++ 11 Redistributable installer ( vcredist_x86.exe )

This is required by PHP. You must install this for anything to work.


Edit your PHP configuration file ( php.ini )

These are some of the settings in the configuration file that you should set:

; PHP's default character set is set to empty.
; http://php.net/default-charset
default_charset = "UTF-8"

; Directory in which the loadable extensions (modules) reside.
; http://php.net/extension-dir
; extension_dir = "./"
; On windows:
; extension_dir = "ext"
extension_dir = "D:\SITES\php\ext"

; cgi.force_redirect is necessary to provide security running PHP as a CGI under
; most web servers.  Left undefined, PHP turns this on by default.  You can
; turn it off here AT YOUR OWN RISK
; **You CAN safely turn this off for IIS, in fact, you MUST.**
; http://php.net/cgi.force-redirect
cgi.force_redirect = 0

; FastCGI under IIS (on WINNT based OS) supports the ability to impersonate
; security tokens of the calling client.  This allows IIS to define the
; security context that the request runs under.  mod_fastcgi under Apache
; does not currently support this feature (03/17/2002)
; Set to 1 if running under IIS.  Default is zero.
; http://php.net/fastcgi.impersonate
fastcgi.impersonate = 1

[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
;date.timezone =
date.timezone = "Canada/Saskatchewan"

;;;;;;;;;;;;;;;;;;;;;;
; Dynamic Extensions ;
;;;;;;;;;;;;;;;;;;;;;;

; If you wish to have an extension loaded automatically, use the following
; syntax:
;
;   extension=modulename.extension
;
; For example, on Windows:
;
;   extension=msql.dll
extension=php_sqlsrv_56_nts.dll
extension=php_pdo_sqlsrv_56_nts.dll

Create a phpinfo file ( phpinfo.php )

This file will allow us to test that PHP is running correctly.

Create a text file and place the following code inside it. Save it as phpinfo.php

<?php
echo date("Y-m-d H:i:s");
phpinfo();
?>

Place the file in your root web folder. By default, the path will be:

C:\inetpub\wwwroot\

1 Like

##Configure IIS to handle PHP files##

We need to configure IIS so that it knows how to handle PHP files. Run the IIS Manager Snap-in:

Windows 7

Control Panel\System and Security\Administrative Tools\Internet Information Services (IIS) Manager

Windows 8

Control Panel\All Control Panel Items\Administrative Tools\Internet Information Services (IIS) Manager


Add a Module Mapping

Double-click Handler Mappings
Click Add Module Mapping
Use the following settings for the Module:

Request path: *.php
Module: FastCgiModule
Executable: (path to PHP CGI executable) C:\php\php-cgi.exe
Name: PHP

Then click on Request Restrictions… and set the following:

Invoke handler only if request is mapped to: File or folder
Verbs: All verbs
Access: Script

When prompted with “Do you want to create a Fast CGI Application for this Executable?”, click Yes.


Restart IIS to load PHP

You may need to restart IIS to load the PHP Module. To do this, simply restart IIS via the IIS Manager snap-in. Click on the root node on the left (your computer name), then on the far right, click Restart.

1 Like

##Test the PHP Install

Using a Browser, navigate to your test file: http://localhost/phpinfo.php

You should see a page like this:

##Troubleshooting PHP Installation##

This is what you will see if you failed to configure the Handler Mapping correctly.

This is what you will see if you did not install the VC++ 2012 Redistributatble x86 (32-bit).

##SQLSRV PHP Extension Installation##

SQLSRV is a PHP extension that enables connectivity to SQL Express Databases.

PHP versions up to 5.3 contained built-in support for MSSQL, but later versions have dropped support entirely. Microsoft developed its own official extension that supports PHP versions 5.3.6 to 5.4, but they have not updated their extension to work with later versions of PHP.

However, there is an unofficial version of this extension developed by a 3rd party which supports PHP version 5.3 to 5.6. This section explains how to install this extension on PHP 5.6. You can read more about it here:


Extract SQLSRV ZIP file

Extract the file sqlsrv_unofficial_3.0.2.2.zip

The archive contains many files, but we only need 2 of them, namely:

php_pdo_sqlsrv_56_nts.dll
php_sqlsrv_56_nts.dll

These files are designed for PHP 5.6.x NTS 32-bit. Copy the 2 files to your PHP installation Extension folder, usually located in:

C:\php\ext\

Now edit your php.ini file (C:\php\php.ini) so that it knows to load the extension files:

;;;;;;;;;;;;;;;;;;;;;;
; Dynamic Extensions ;
;;;;;;;;;;;;;;;;;;;;;;

; If you wish to have an extension loaded automatically, use the following
; syntax:
;
;   extension=modulename.extension
;
; For example, on Windows:
;
;   extension=msql.dll
;
; If you only provide the name of the extension, PHP will look for it in its
; default extension directory.
;
; Windows Extensions
; Note that ODBC support is built in, so no dll is needed for it.
; Note that many DLL files are located in the extensions/ (PHP 4) ext/ (PHP 5)
; extension folders as well as the separate PECL DLL download (PHP 5).
; Be sure to appropriately set the extension_dir directive.
;
extension=php_sqlsrv_56_nts.dll
extension=php_pdo_sqlsrv_56_nts.dll

Install SQL Native Client

This installation is required for SQLSRV Extension to work. Simply run the msi installer.


sqlncli32.msi http://go.microsoft.com/fwlink/?LinkID=239647&clcid=0x409
sqlncli64.msi http://go.microsoft.com/fwlink/?LinkID=239648&clcid=0x409


Restart IIS to reload PHP

Now we need to reload PHP so that it can load our new extensions. To do this, simply restart IIS via the IIS Manager snap-in. Click on the root node on the left (your computer name), then on the far right, click Restart.


Test SQLSRV Extension

Create a new text file containing the following code, and save it as testsql.php, in your root web folder which by default is:

C:\inetpub\wwwroot\

NOTE: you need to change the connection details at the top of the file!

<?php
/////////////////////////////////////////////
// DB connectivity info
/////////////////////////////////////////////

$mssql_dbhost='localhost\SQLEXPRESS';
$mssql_dbuser='sa';
$mssql_dbpass='sambapos';
$mssql_dbname='SambaPOS5';

$dbconn = msdbconnect();

echo $dbconn ? 'Connection successful!' : 'Connection FAILED!';

function msdbconnect () {
    global $mssql_dbhost, $mssql_dbuser, $mssql_dbpass, $mssql_dbname;

    $connectionInfo = array("UID"=>$mssql_dbuser,
                            "PWD"=>$mssql_dbpass,
                            "Database"=>$mssql_dbname, "ReturnDatesAsStrings"=>true);

    $mssql_dbresource = sqlsrv_connect( $mssql_dbhost, $connectionInfo);
    if( $mssql_dbresource === false ) {
        echo '<b style="color:#FF0000">ERROR connecting to MSSQL:</b>';
        echo '<PRE><CODE>';
        print_r(sqlsrv_errors(), false);
        echo '</CODE></PRE>';
        die('<b style="color:#FF0000">EXECUTION TERMINATED!</b>');
    }
    Return $mssql_dbresource;
}
?>

Now open a Browser and navigate to http://localhost/testsql.php

4 Likes

This topic is now listed. It will be displayed in topic lists.

… reserved for updates …

… reserved for more updates …

I am loving this , we can use this to create custom reports

Under the heading Test SQLSRV Extension I’ve created a file named ‘testsql.php’ of the following code with changed connection details and copied it to the root web folder C:\inetpub\wwwroot.

<?php ///////////////////////////////////////////// // DB connectivity info ///////////////////////////////////////////// $mssql_dbhost='localhost\SQLEXPRESS'; $mssql_dbuser='sa'; $mssql_dbpass='sambapos'; $mssql_dbname='SambaPOS5'; $dbconn = msdbconnect(); echo $dbconn ? 'Connection successful!' : 'Connection FAILED!'; function msdbconnect () { global $mssql_dbhost, $mssql_dbuser, $mssql_dbpass, $mssql_dbname; $connectionInfo = array("UID"=>$mssql_dbuser, "PWD"=>$mssql_dbpass, "Database"=>$mssql_dbname, "ReturnDatesAsStrings"=>true); $mssql_dbresource = sqlsrv_connect( $mssql_dbhost, $connectionInfo); if( $mssql_dbresource === false ) { echo 'ERROR connecting to MSSQL:'; echo '
';
        print_r(sqlsrv_errors(), false);
        echo '
'; die('EXECUTION TERMINATED!'); } Return $mssql_dbresource; } ?>

But when navigate to http://localhost/testsql.php, a message is shown below:

Fatal error: Call to undefined function sqlsrv_connect() in C:\inetpub\wwwroot\testsql.php on line 22

Please help !!

Note: I didn’t installed mysql server so far.

You have not installed or enabled the SQLSRV extension for PHP. Any code that uses sqlsrv_xxx() functions will not work.

Review the section on downloading, installing and enabling the SQLSRV extension for PHP.

NOTE: MySQL has nothing to do with this Tutorial. This topic details how to work with MSSQL (Microsoft SQL Server), not MySQL.

i have problem i do what you say but when i try to enter local host that what happen


white screeen why ???

i have problem i do what you say but when i try to enter local host that what happen
https://forum.sambapos.com/uploads/default/original/3X/4/8/48959c7c81f0d4ec5bad65e0e59443fcb11d71c2.png

why it show white screeen ???

Try this: edit your phpinfo file to turn on error messages:

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
echo date("Y-m-d H:i:s");
phpinfo();
?>

I have problem when navigate to localhost/testsql.php, a message is shown below. Help me to fix it.