To be blunt, getting PHP and MySQL to work correctly together under Windows is an argument for using Linux.
If Linux isn't an option for you, you can minimise the pain by running the Apache web server rather than IIS on your Windows computer. What's more, there are dozens of point-and-click WAMP installers out there that will manage all the details for you—see this Wikipedia list of them.
If you need to manage the details yourself, follow the walkthrough here. If you really must use IIS rather than Apache, take a deep breath and skip now to Installing PHP. Otherwise, if you have not yet installed Apache 2, follow this walkthrough before proceeding further.
Your Apache installation folder under Windows is something like c:\apache. Call
it [APACHEDIR]. The Apache configuration file is [APACHEDIR]\Conf\httpd.conf. Edit it with any text editor, for example TextPad. To change the default
DocumentRoot folder, where Apache looks for Web files to serve, set
DocumentRoot in htppd.conf, for example
There are two more tweaks, for running PHP under Apache and for error logging. Within your text editor, search for
DirectoryIndex index.html index.html.var # index.php
To have Apache serve PHP pages, remove the comment character (#) in that line. You should also permit the use of .htaccess
files in any directory: search for an
AllowOverride setting and change
it from None to All. Save the changes made so far, and leave the file
open in the text editor; more changes are needed once PHP is installed.
If a setting triggers an error, the error may log to [APACHEDIR]\apache2\logs\error.log but more likely will log only to the Windows Event log (Start | Settings | Control Panel | Administrative Tools | Event Viewer), which is not a convenient tool for debugging installation settings. It is much easier to test Apache 2 in a command line window, where error reports appear instantaneously. So open a command line window, navigate to C:\Apache\Apache2\bin, and make sure you can start and stop Apache from there:
httpd -k start httpd -k stop
In Apache 2.0 the command is
apache; in 2.2 it is
If you prefer, right click on the desktop My Computer icon, select Manage, open Services and Management, click on Services, right click on Apache, and select Start.
1. The web server: The web server makes a huge difference. Apache is more reliable than Windows Internet Information Services (IIS). PHP installations under Apache differ radically from PHP installations under IIS. Installations under IIS versions 4, 5, 6 and 7 differ significantly from one another.
2. PHP and PHP-CGI: Generally, Windows can run PHP programs in two main ways: via a CGI interface (eg via php.exe), or internally and faster using PHP DLLs, so for many PHP releases there have been two Windows binaries, a smaller .msi package which installs the CGI executable php.exe and is missing DLLs needed to run under Apache (so you have to download them from http://snaps.php.net), and a more complete .zip package which includes those DLLs.
As of PHP 5.3, if you are running IIS then this choice has been taken out of your hands: Under IIS, PHP 5.3 and later run only under FastCGI.
3. Where MySQL is installed makes a difference. It may be installed on the machine serving PHP scripts, or on another machine on the LAN.
4. Now you see it, now you don't: In late 2005 MySQL began releasing its own builds of the required client library libmysql.dll, and the PHP-MySQL API libraries php_mysql.dll and php_mysqli.dll as Connector/PHP. MySQL strongly recommended that developers running PHP under Windows use Connector/PHP rather than the DLLs which ship with PHP.
In March 2008 MySQL reversed itself, and removed the Connector/PHP download page without comment. In response to an email inquiry, a MySQL spokesman said Connector/PHP is "no longer required". Much later, a page appeared at the MySQL site to explain that Connector/PHP "is no longer necessary".
So forget Connector/PHP.
5. API libraries: As of PHP 5.3, libmysql.dll disappeared from the PHP package! There was instead a built-in replacement called mysqlnd. which contains the mysql, mysqli and PDO APIs. Contrary to earlier suggestions on the MySQL site, there is no mysqlnd download—it's built into PHP 5.3, and as of PHP 5.4 is the default for MySQL.
But mysqlnd is not entirely reliable in some contexts, and cannot connect to MySQL 4.1 or later servers using the older authentication protocol. If you don't want to live with mysqlnd problems or work around them, naturally you'll want to disable mysqlnd and use libmysql.dll. Since PHP 5.3 you can't unless you're willing to recompile PHP from source with a
--disable-mysqlnd flag! So if mysqlnd problems affect you, until they're fixed we suggest you use PHP 5.2 instead of PHP 5.3.
Or, use the new PHP Data Objects (PDO) API included in the mysqlnd package (keep reading).
So there are now three main PHP APIs for MySQL: mysql , mysqli (i means "improved") and PDO . The main mysqli improvements over the mysqli API are support for O-O syntax and multiple stored procedure calls. But those features come at a price: mysqli syntax is very different, so refactoring code for mysqli can be painful, and the mysqli interface is pickier, so for example determining whether a column is a key requires bit twiddling. As its name suggests, PDO is also object-oriented; its main draws are named parameters and support for a dozen RDBMSs.
Because of all these variations and inconsistencies, before installing any version of PHP you should remove all traces of previous PHP installations, including all PHP-related files in %windir% and %windir%\system32.
MSI installer packages are generally reliable, but PHP installation under Windows is not failsafe, so best practice is to take the PHP .zip you want from http://windows.php.net/download/. That page now advises that to run PHP under Apache, you need a VC6 build (built with the legacy Visual Studio compiler). Unpack it to %HOMEDRIVE%\php, preserving paths.
For packages up to 5.2x, if the package does not include libmysql.dll, use the copy of libmysql.dll in the bin folder of your MySQL installation. From PHP 5.3 on, PHP cannot use libmysql.dll.
For IIS 4: Upgrade IIS to version 5 then follow IIS 5 options below.
For PHP 4.4 and IIS 5: Download and run http://www.iis-aid.com/iis_aid_php_installer.
For PHP 5 and IIS 5:
MSI installer packages are generally reliable, but PHP installation under Windows is not failsafe, so best practice is to take the PHP .zip you want from http://windows.php.net/download/. That page now advises that to run PHP under IIS, you need a VC9 build (built with the Visual 2008 compiler). Unpack it to %HOMEDRIVE%\php, preserving paths. Unless the PHP version is 5.3 or later, if the package turns out not to include libmysql.dll, download an earlier .zip.
Create a PHP installation folder with no spaces in its name, for example c:\php.
If you are using Windows Vista, turn off User Account Control (UAC) as described here before installing PHP to prevent files being deposited in the wrong folder.
As explained above, it is no longer necessary (or indeed possible) to download MySQL's own mysql and mysqli extension zips from http://dev.mysql. com/downloads/connector/php
Unpack the PECL package into the newly created PHP subdirectory ext, preserving paths.
For PHP 5 and IIS 6 under Windows Server 2003 see this tutorial or this tutorial.
For PHP 5 and IIS 7 or later:
Follow this tutorial, or this tutorial.
Or having installed IIS 7 or higher, goto http://www.microsoft.com/web/platform/phponwindows.aspx and there click on one of the "Install PHP" buttons (again: if you want to take your chances on the mysqlnd PHP driver, download PHP 5.3; if you prefer to use libmysql, download PHP 5.2).
But wouldn't you know it, this is Windows and PHP, and when the Windows PHP installation says it's done, it isn't! To get IIS to actually run PHP scripts, you then have to run optionalfeatures.exe from Start Menu, and in the dialog that pops up, drill down to Internet Information Services | World Wide Web Services | Application Development Features,
and check the "CGI" checkbox, then click OK. To prove that PHP scripts run in your installation of IIS (under Windows 7 as administrator) save c:\inetpub\wwwroot\default.php containing just this text:
<?php echo "Helllo world from PHP!"; ?>, and run it in your browser as http://localhost/default.php. When you see that working, PHP is running in IIS.
Since Windows 7, IIS authentication is byzantine. If your PHP scripts fail with an error like "You do not have permission to view this directory or page because of the access control list (ACL) configuration ...", from Start run inetmgr, click on the top level in the left navigation panel, select Authentication in the IIS middle panel, right-click on Anonymous Authentication and select Edit, and in the resulting Edit Anonymous Authentication Credentials dialog, click the Application pool identity radio button. Incredible, but there it is.
On some Windows machines it is also necessary to edit %windir%\system32/drivers/etc\hosts and uncomment the line that says:
# 127.0.0.1 localhost
For PHP 5 and Windows Server 2008:
There are installation tutorials here and here.
Again you will have to tweak. In Start Menu run CompMgmtLauncher.exe. In the dialog that comes up, if there is not a "Roles" node called "Web Server (IIS)", click Add Roles and add it. Once it's there, click Add Role Services and enable the "CGI" checkbox under the Application Development group. Then click Next, then click Install. Then follow the instructions at http://www.php.net/manual/en/install/windows/iis7.php under "Configuring IIS to process PHP requests" and "Using IIS Manager user interface to create a handler mapping for PHP".
Test PHP under IIS
We refer to your PHP installation folder as
To test your PHP installation independently of IIS, open a command window over
PHPDIR and type:
If it runs without error, PHP is installed. To prove that PHP scripts run in your installation of IIS (under Windows 7 or Server 2008 as administrator\) save c:\inetpub\wwwroot\default.php containing just this text:
<?php echo "Helllo world from PHP!"; ?>
and run it in your browser as http://localhost/default.php. When you see that working, PHP is running in IIS.
The PHP manual is available for download in many languages and formats at http://www.php.net/docs.php.
If you are installing PHP under IIS, skip now to Installing PHP and MySQL under IIS.
[PHPDIR]is in the
PATH. If you are installing PHP 4, move all DLLs from both [PHPDIR]\dlls and [PHPDIR]\sapi to [PHPDIR]. For PHP 5, that's not necessary---the DLLs are already in
2. Configure Apache 2 for PHP: In the text editor window where Httpd.conf is open, look for the section that contains a list of AddType commands, and add this one:
AddType application/x-httpd-php .php
For PHP 4, in the LoadModule section of Httpd.conf, uncomment or add in:
LoadModule php4_module "c:/php/php4apache2.dll"
For PHP 5 it should be:
LoadModule php5_module "c:/php/php5apache2.dll"
For PHP 5.2 and later it should be:
LoadModule php5_module "c:/php/php5apache2_2.dll"
3. Set up php.ini: Edit a copy of [PHPDIR] \php.ini-recommended as php.ini in your text editor, adjust doc_root and session.save_path if need be, pick a timezone setting from www.php.net/manual/en/timezones.php, and comment out one extension_dir setting:
doc_root = "c:\apache\htdocs" ; MUST BE THE SAME AS IN Httpd.conf session.save_path = "c:/temp" ; DIRECTORY MUST EXIST date.timezone = "America/Vancouver" extension_dir = "c:\php\extensions" ; FOR PHP 4 ONLY extension_dir = "c:\php\ext" ; FOR PHP 5 ONLY
Create c:\temp if it does not exist. Some PHP manuals have said these path arguments need trailing backslashes, but since PHP 4.3 that is not the case.
4. Apache 2, PHP and MySQL: Up to and including PHP 5.2x: if there is a copy of libmysql.dll in [PHPDIR], copy it to [APACHEDIR]\bin, otherwise copy libmysql.dll from the bin folder of your MySQL installation. For PHP 5.3 and later: remove any copy of libmysql.dll from [APACHEDIR]\bin.
Then ensure that php_mysql.dll and php_mysqli.dll are in
extension_dir as set in Step
Some older Windows installations are missing msvcr71.dll, which PHP may need. If Windows cannot find it in
Then in php.ini, uncomment or add:
If you will be using the pdo_mysql interface library (http://forge.mysql.com/wiki/PHP_PDO_MYSQLND), also ensure that php_pdo.dll and php_pdo_mysql.dll are in
extension_dir as set in Step 3, and in php.ini uncomment or add:
The default MySQL host is
localhost. If MySQL is on another machine on your LAN,
set the default MySQL host:
mysql.default_host = "networkmysqlservername"
Be sure to make these changes to the copies of php.ini in both [PHPDIR] and in [APACHEDIR]\bin!
To see the list of modules you have installed for PHP, from [PHPDIR] issue:
5. CGI mode: If you need to run PHP programs in CGI mode, add to Httpd.conf:
ScriptAlias /php/ "c:/php/" Action application/x-httpd-php"/php/php.exe"
6. Test: Save all changes and reboot the machine. To test Apache alone, execute
httpd -k start
[APACHEDIR]\bin. If there is an error, Apache will report it immediately in this window. If
you prefer, run it from Control Panel | Services | Apache and check
To ensure that PHP can connect to MySQL via php_mysql.dll, use a text editor to save this code
mysql_connect('HOST','USR','PWD') or die(mysql_error());
substituting your host, username and password
values to connect.php in
[PHPDIR], open a command window
[PHPDIR], and type:
If it fails, redo Steps 1 through 5.
To test Apache's serving of PHP pages and PHP's connection to MySQL under Apache, use your text editor to create a simple PHP page, called phptest.php, containing just this line:
<?php phpinfo(); ?>
Save to the Apache DocumentRoot folder as phptest.php.
Run it by browsing http://localhost/phptest.php.
If everything is working correctly, you will see a page with the PHP
logo and a long list of settings. Whether PHP is running through CGI or
inside Apache is indicated by the environment variable
script_name. If PHP is running through CGI, this variable is set to
/Php/Php.exe. If Apache is running the PHP script directly, this variable will contain the name of your script,
/Phptest.php. In either case, if PHP can connect to MySQL then the page shows a MySQL section.
If MySQL and MySQLi sections do not appear on the page, check that the
setting labelled Loaded Configuration File
in the top section references the php.ini you prepared for this
IIS is in %HOMEDRIVE%\Inetpub. The web folder is %HOMEDRIVE%\Inetpub\wwwroot
If you are installing PHP 4.4 and ran the PHP installer from iis-aid.com, skip to Step 3.
For PHP 5 through 5.2 and IIS 5 or 6, follow this walkthrough, then skip to step 6.
For PHP 5.3 and later under IIS 5.1 and 6.0, follow this walkthrough, or this one, then skip to Step 6.
For PHP 5 under Windows 7 follow this walkthrough.
If you installed PHP 5 under IIS 7 as described here, or PHP 5 and IIS 6 under Windows Server 2003 as described here, skip to Step 6.
1. Set environment variables: In My Computer | Properties | Advanced | Environment Variables | System variables (in Windows 7, right click on Computer in Windows Explorer and drill down to Properties | Advanced system settings | Environment variables), scroll down to path, click Edit, add the [PHPDIR] spec and click OK. Under System variables click New, add a variable called PHPRC, and set it to the [PHPDIR] spec.
2. Install PHP application mapping: Download David Wang's Visual Basic Script from http://www.iis-aid.com/files/chglist.zip, unpack chglist.vbs to a folder in the path, then run this command:
chglist.vbs W3SVC/ScriptMaps "" ".php,[PHPDIR]\php5isapi.dll,5" /INSERT /COMMIT
substituting for [PHPDIR] your PHP installation path. In My
Computer | Manage | Services and Applications | Internet Information
Service | Web Sites | Properties | Home Directory | Configuration, use the Add or Edit button to ensure that there is an entry for which Extension is .php and Executable Path is
[PHPDIR]\ext\php5isapi.dll (eg c:\php\ext\php5isapi.dll).
For IIS 6.0, you must also configure a Web extension. In My Computer | Manage | Services and Applications | Internet Information Service | SERVER | Web Service Extensions, click on Add a new Web service extension, enter a name for Extension Name, set extension Status to
Allowed, and in the Add File applet click on Browse, navigate to
[PHPDIR], and select
3. DLLs: Make sure libmysql.dll is in [PHPDIR], and that php_mysql.dll and php_mysqli.dll are in [PHPDIR]\ext.
Some older Windows installations are missing msvcr71.dll, which Connector/PHP
needs. If Windows cannot find it in
4. Set up php.ini to talk to MySQL: In
[PHPDIR] make a copy of php.ini-recommended, name it php.ini, open it in your text editor, and touch up these entries:
extension_dir="c:\php\ext" ; or whatever yours is extension=php_mysql.dll extension=php_mysqli.dll extension=php_pdo_mysql.dll
The default MySQL host is
localhost. If MySQL lives on another machine on your LAN, set the default MySQL host:
mysql.default_host = "your_network_mysql_server_name"
5. Under Windows Vista restore UAC as described here.
6. Test: Reboot the machine, or in My Computer | Manage | Services and Applications | Internet Information Service | All Tasks, click Restart. Write Inetpub\wwwroot\test.php that just says
<?php phpinfo(); ?>
browse http://localhost/test.php, and scroll down the page to see the MySQL entries.
[Latest rev: 1 Mar 2013]