Configuring PHP with MySQL for Apache 2 or IIS in Win 10

For a long time, getting PHP and MySQL to work correctly together under Windows was an argument for using Linux. Many of the kinks have been worked out, but this path still has several potholes.

Windows IIS has always had its challenges. If Linux isn't an option for you, you can minimise the pain of running PHP on a Windows server by using Apache rather than IIS as a web server—there are several Apache-MySQL-PHP installation stacks requiring you to do little more than point-and click to install and manage the web server, PHP and MySQL.

If you need to manage the details yourself ...

  1. Install required Microsoft Visual C++ redistributables
  2. Install MySQL with the Windows installer
  3. Install Apache or Enable IIS
  4. Install PHP to run under Apache

Install Visual C++ redistributables

Download the version of vc_redist*.exe that matches your computer architecture, and run it.

Install MySQL

If MySQL is not already installed,
download the MySQL installer, run it, follow the prompts.

Install Apache

Download Apache files and unzip them to C:\Apache\. Navigate to c:\Apache\conf\, open httpd.conf in a text editor (not Word!) ...

Change # ServerName www.example.com:80 to ...

ServerName localhost
Uncomment or edit AllowOverride directives so they read ...
AllowOverride None
AllowOverride All
Uncomment #LoadModule rewrite_module modules/mod_rewrite.so so it reads ...
LoadModule rewrite_module modules/mod_rewrite.so
Save httpd.conf and leave it open it your text editor. In a commandline window, navigate to c:\apache\bin and type ...
httpd -k start
If there is an error message, re-edit httpd.conf and retry till Apache runs error-free.

In your browser's navigation control, type ...

localhost 
The web page should say "It works!". If an app other than Apache responds, that app has already captured port 80. Fix that by running regedit, then navigating in regedit to ...
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\ 
... and find the ServiceName that is the name of the Windows service which responded to your web browser localhost. That's the progeam that's taken control of port 80. In the right pane, right click on DependsOnService, select Modify from the context menu, add Apache24 to the list of dependencies; click OK; close Regedit.

Once Apache responds correctly to localhost, skip to Install PHP.

Enable IIS

If you've decided to use IIS instead of Apache, run appwiz.cpl, select "Turn Windows features on or off", select "Internet Information Services", click the checkbox. Windows will whir for a while then will tell you it's done. In your browser's navigation control, type ...
localhost 
The web page should say "IIS" in huge letters.

To install PHP to run under IIS see here.

Install PHP for Apache

If you propose to use the MySQL version 8.0.22 and later default caching_sha2_password authemtication plugin, you will need PHP 7.4 or later; download it from here. Otherwise, choose your preferred PHP version from here. Unzip the download to c:\php.

Copy php-ini-development.ini to php.ini.

Edit Windows environmental variables, select Path, add "C:\php". Click OK and exit.

Again editing c:\Apache\conf\httpd.conf in your text editor, add ...

PHPIniDir “C:/PHP”
AddHandler application/x-httpd-php .php
LoadModule php7_module “C:/PHP72/php7apache2_4.dll”
Save, and in the commandline window over c:\apache, run ...
httpd -k stop
httpd -k start
While errors occur, fix php.ini or httpd.conf and retry.

When Apache runs without errors, write the following to c:\apache\htdocs\phpinfo.php:

<?php
  phpinfo();
?>
In your browser's navigation control, type localhost/phpinfo.php ... and you will see your PHP settings.

Configure PHP to access MySQL and SSL

In your text editor, edit php.ini to uncomment these directives ...
extension=openssl
extension=mysqli
Restart Apache as before, run localhost/phpinfo.php as before, and this time check that mysqli is enabled.

Finally, run services.msc, scroll to Apache, make sure the service is "Automatic", and restart it. You're done.

[Latest rev: 27 Oct 2020]