Fresh out of installing and configuring Apache and PHP so that I could get this WordPress blog up and running, I feel there’s a topic worthy of some brief mention here. Namely, the proper (which I mean not in the “pedantically correct” sense but more in the “do this and it will actually work” sense) way to get Apache and PHP talking to each other after installing them both for the first time. And to be as clear as possible here, this information pertains specifically to Apache version 2.2.17 and PHP version 5.3.5 (thread-safe). Mileage may vary with different software versions.
Now back in the day, I would use a nifty little SourceForge project called phptriad to handle all of the little installation and configuration nonsensicalities for me. Phptriad was quick, easy, and generally worked straight away. There’s just one major problem with it; the project hasn’t seen any updates since September 9th of the year 2000. Using it nowadays consigns one to hopelessly outdated versions of Apache and PHP (and also MySQL). Versions that are so hopelessly outdated that they do not meet the minimum requirements for running WordPress.
So I decided that this time around I would do things the correct way, and install the latest version of Apache, the latest version of PHP, and then configure them to talk with each other. “How hard can it be”, I recall myself saying. The PHP installer even had an Apache configuration step that seemed like it would handle the entire process automatically. But sadly that was not the case; the automatic configurator did nothing useful, and while the configuration process was not hard it was exceedingly poorly documented and circuitous, turning what should have been a 2-minute task into a nearly 2-hour endeavor.
Now, do a quick Google search for Apache and PHP configuration issues and you will find countless posts suggesting adding minor variations of the following directives to Apache’s httpd.conf file:
AddHandler application/x-httpd-php .php .phtml AddType application/x-httpd-php .php
Seems like a simple enough fix, but none of the myriad variations of these two directives did the trick. It makes sense when you think about it, considering that all these directives do is associate .php files with a MIME type. Left wide open is the question of how Apache is supposed to know what that MIME type actually means, how it should be handled, and where to find the handler. Yes, the php5_apache22 module is being loaded as well, but the module isn’t magic; Apache still needs to know when and how it should use it. So essentially what’s missing is a mapping to tell Apache “for MIME type ‘x’, use program ‘y'”. After far too much searching, I found a page that also mentioned adding the following directive:
Action application/x-httpd-php "/path/to/your/php.exe"
And finally there was the missing piece. The AddType (or if you prefer, AddHandler) directive associates .php files with a particular MIME type, and the Action directive maps that MIME type to a concrete resource that Apache can use to process items of that type. Apache now knows both what a .php file is, and how to process it, and like magic the server behaves as it should. All told far too much digging was needed to uncover that one critical line.
For reference, the complete PHP configuration section in my httpd.conf file looks like so:
PHPIniDir "~/apache/php" LoadModule php5_module "~/apache/php/php5apache2_2.dll" AddType application/x-httpd-php .php Action application/x-httpd-php "~/apache/php/php.exe"
I suspect that the actual MIME type assigned to .php files matters very little here, so long as the type is consistent between the ‘AddType’ directive and the ‘Action’ directive.