A Complete Guide to Installing MySQL and PHP with IIS
Posted On September 26, 2015
Update: A video update to this article for Windows Server 2016 / 2019 is available on YouTube. I’m leaving this version up as a general guide for issues you might encounter in this type of installation. Please take this into consideration when downloading components.
One of my latest projects was setting up WordPress, the popular content management system, on a client’s web server which was running Windows Server 2008. WordPress requires installations of MySQL for the database back-end and the PHP scripting language in order to serve up the WordPress content. On a local Windows machine, I’d probably just use a pre-configured WAMP (Windows, Apache, MySQL, PHP) stack package like XAMPP which is installed quickly and includes all the necessary components. That’s not quite an option in a professional environment, though. I was also working with Internet Information Services (IIS) 7.5 for the web server instead of Apache so the process is a little more involved.
In addition to installing the components, PHP must be be configured to work with MySQL so that WordPress, which maintains all the site content in MySQL, can access the database as needed. Having only previously done this in Linux with Apache as the web server, I had to do a little research. It was not painless – WordPress is a little finicky on IIS / Windows and I had to research my way past a couple stubborn errors.
For everyone’s benefit, I thought I’d share what I learned here. I’ve tested this process on both Windows Server 2008 and 2012 and noted the differences where necessary.
I chose the MySQL Installer 5.6 for Windows which installs everything. The installer is 32-bit but installs either the 32-bit or 64-bit version of MySQL depending on your system. There’s also the option to choose a small file that will perform an installation from the web or the large file if you’re not certain you’ll have a connection available.
I chose the Custom setup type from the first option screen so I could choose which components I wanted on the machine. For this project, the server itself, MySQL Workbench and MySQL Notifier were enough. MySQL Notifier alerts the user to any change in the server’s status. I didn’t specifically need Workbench for a WordPress installation but it doesn’t take up much room and might come in handy at some point.
If you’re installing MySQL Workbench, you might need to install the Microsoft Visual C++ 2013 Runtime in order to complete the installation. The Installer will notify you of this or any other failing requirements and give you the chance to fix them without stopping the installation routine.
The rest of the Installation Wizard screens are pretty self-explanatory. Under Type and Networking, I chose Server Machine since this would actually be a web server and I went with the standard system account for the Windows service screen.
After the installation finishes, it will offer to open MySQL Workbench where you can ensure that you’re able to login to your new database server. Another option is to open the MySQL 5.6 Command Line Prompt from the Start menu and type in the password that you set for the MySQL root account.
While you’re here, you might as well create the MySQL database for WordPress to use. You can do this through MySQL Workbench if you’ve installed it or through the MySQL Command Line with the following commands:
CREATE DATABASE wordpress; USE wordpress; CREATE USER 'wpuser'@'%' IDENTIFIED BY '<password>; GRANT ALL ON wordpress TO 'wpuser'@'%';
With these commands, you create a WordPress database and set it as the default in your MySQL session. Then you create an administrative user for WordPress (so you’re not using the server root user) and grant that user all privileges on the WordPress database. You can use the user name and passwords of your choosing. Just be sure to store them in a safe place.
If you’re using the command line, type ‘exit’ to leave MySQL and then ‘exit’ again to exit the command prompt.
Note: The 2021 update to this article demonstrates how to perform the configurations of both IIS and PHP through Microsoft’s Web Platform Installer. The information below details a manual configuration which might still be useful for verification of settings.
You can download the Windows PHP files from http://windows.php.net/download. The non-thread safe edition is required when working with IIS and I stuck with the 32-bit version since, as of this writing, the 64-bit editions are still labeled as experimental. I’m using the current stable version, 5.6.13.
After you’ve downloaded the PHP files, unzip the package to the directory of your choice on your hard drive. I chose c:\php to keep things simple. The package includes a php.ini-production file which you will need to rename as php.ini and copy to your c:\windows directory after making a few changes. This file holds keys and settings, many of which are commented out and can be uncommented to apply specific settings as needed.
In your favorite text editor, uncomment the following keys in php.ini by removing the semi-colon from the beginning of the line and change the settings as follows:
cgi.force_redirect = 0 (around line 753)
fastcgi.impersonate = 1 (around line 780)
Set extension_dir to the ‘ext’ folder in the path where you stored the PHP fies (i.e. ‘C:\PHPext\’). (around 736)
date.timezone should be set to the time zone of your server (around line 926)
The php_curl.dll extension is to enable WordPress to retrieve and display product news updates. Without it, you’ll see an error message on your desktop every time you log in.
After you’ve setup PHP, you’ll need to configure IIS to work with PHP.
First verify that the machine has been assigned the role of a web server and that CGI is installed as a service under IIS. On Windows Server, You can do this by opening the Windows Server Manager (not the IIS Manager) and selecting Web Server (IIS) under Roles. If this option does not exist, right-click on Roles under the Server Manager tree and add it.
Be sure to select the CGI option under Application Development in the Add Roles wizard. If the web server is already installed but CGI does not show as installed under the Application Development group, you can add it by clicking on Add Role Services and selecting it.
The process is the same in Windows Server 2012 although the Add Roles Wizard has gotten an overhaul.
After the web server role and CGI service have been established, check under the Internet Information Services Manager for the FastCGI Settings option. This should be present if you’re running IIS 7.5. If your running IIS 7, you might need to install the IIS 7 Administration Pack which can be downloaded from Microsoft.
You will need to add the php-cgi.exe application to IIS under the Fasts CGI settings. Right-click in the FastCGI settings panel and choose Add Application.
The Path setting needs to point the to EXE file. Under the FastCGI Properties, change the Instance MaxRequests setting to 5000. Under the Environment Variables, add a new variable to the collection called PHP_MAX_REQUESTS and set its value to 5000. Click OK on both screens to accept both settings.
Now that PHP has been added as a FastCGI application, you can add a handler mapping so that IIS will know what application to use when it gets a request for a PHP file. Select the Handler Mappings icon under the IIS home screen. Then click on Add Module Mapping under the Actions menu or right click within the list of mappings and select it from the context menu.
Set the options as shown above:
Request path: *.php Module: FastCGIModule Executable: (path to your php-cgi.exe file) Name: PHP
After you click OK, IIS will ask you to verify the creation of a FastCGI application. Click Yes.
Next, select the Default Document icon in IIS and add ‘index.php’ to the list of possible default documents for the site.
At this point, you will need to restart IIS to ensure that all changes are applied. You can restart it from the Actions menu in IIS or open a command window in Administrator mode and type iisreset at the prompt. After IIS stops and restarts, type exit to close the window.
IIS creates a directory called c:\inetpub. The c:\inetpub\wwwroot folder is where your web files can be stored and served from when requests come in to the web server. To ensure that PHP is running and working with MySQL, create a new text file in this directory with the following text.
<?php phpinfo(); ?>
This is a basic PHP command that tells the browser to display all PHP settings. Name the file phpinfo.php and store it in your wwwroot directory. When you enter http://localhost/phpinfo.php in your web browser, IIS should return the PHP settings page. You’ll need to scroll down or search the page to ensure that it has a section for MySQL as shown in the image below.
If the MySQL section is not present, you’ll probably get the following error when you try to install WordPress:
“Your PHP installation appears to be missing the MySQL extension which is required by WordPress.”
You also might get the following error when trying to bring up the PHP settings:
HTTP Error 500.0 – Internal Server Error c:\php\php-cgi.exe – The FastCGI process exited unexpectedly Error Code 0xc0000135
For one final step, you should download and install the URL Rewrite module in IIS. When using IIS, this might be needed to support custom permalinks for your pages and blog posts in WordPress. It can also be obtained directly from IIS.net.
Once you’ve created the database, unzip the WordPress download file into your c:\inetpub\wwwroot folder. You can keep all the files in a WordPress subdirectory of this folder or put them directly in wwwroot. It’s probably best to use the subdirectory in case you want to install other applications.
Make a copy of the wp-config-sample.php file from the WordPress directory and name it wp-config.php. Then edit the file to change the following settings, substituting the user name and password that you select:
/** The name of the database for WordPress */ define('DB_NAME', 'wordpress'); /** MySQL database username */ define('DB_USER', '<username>'); /** MySQL database password */ define('DB_PASSWORD', '<password>'); /** MySQL hostname */ define('DB_HOST', 'localhost');
Unless you maintain the MySQL installation on a separate machine, the host name of ‘localhost’ can stay the same as you’re referring to the local MySQL installation.
After you’ve made these changes, load the wp-config.php file in your browser.
This will actually run the script within the file and start the installation.
This should go smoothly but if you do run into an error, WordPress is not the most helpful with its messages. Here are a couple that you might see.
A blank page with ‘No input file specified’. Re-check the address in your browser and make sure it matches the location of the wp-config.php file.
A redirect loop error page. Again, check the browser address. The initial script should take you to the wp-admin/install.php file within your WordPress directory. Instead, it might be something like “http://localhost/wordpress/wp-config.php/wp-admin/install.php”. You can manually redirect the browser to the actual file if necessary.
Error establishing database connection. Check the settings in your wp-config.php file and make sure they match the WordPress database and user you created. Re-copy the original file from the download file and do it again if necessary.
If everything works, the installation will ask you to enter a user name and password for an administrative user account along with your e-mail and whether you want search engines to index the site. Once you complete these settings, the installation will complete and you can login to your new WordPress site.
I’ve run through this process on a few different machines to try to catch as many issues as possible but I’m sure there are more ideas out there on how to fix various issues with IIS / PHP / WordPress. I didn’t even get into the use of a web.config file in the WordPress directory as I didn’t find it to make any difference during the installation. If you’ve had some experience with creating this kind of stack, I’d be glad to hear from you in the comments.