The specified CGI application misbehaved by not returning a complete set of HTTP headers

As we were looking at blogging as a new Internet Marketing vehicle/medium for our clients to take advantage of, we followed the  rest of the SEO herd to WordPress. All of the SEO’s that are promoting blogging to their clients seem to prefer WordPress ( with what seems to be good reason, but that is a topic for another post ), and what’s more, pretty much every blog we looked at worth looking at had the tell-tale signs of a hosted WordPress application ( WordPress.org as opposed to the WordPress.com option ).

So, to make that long story short, we settled on WordPress as the blogging application we would recommend to our own clients.

However, WordPress is a PHP/MySQL application, and we know what that means . . . . it would be “happiest” in a “LAMP” ( Linux, Apache, MySQL, PHP ) environment while we run Windows, IIS servers supporting MySQL & PHP . . . but even though WordPress is a pretty darned sophisticated application, we wanted to prove that we can run it just as well on a Windows servers.

And, of course, we hit some bumps.

The biggest “bump” we hit was an intermitent issue which throws the error:
“The specified CGI application misbehaved by not returning a complete set of HTTP headers.”

So, the  first thing we did was to query issues of this nature vis a vis WordPress. Found some folks pondering this, but in the end the issue is not a WordPress issue at all. WordPress is just a lot more “active” an application than the relatively simple PHP applications our clients had been using. None of of the suggestions we found looking at the issue this way helped, so we won’t mention them.

Pressing on further, we found more general discussions of the issue with some folks claiming it’s a PHP issue, others that it’s a MySQL issue, and of course plenty of folks blaming Microsoft Windows servers for the issue. At the end of the day, though, it appears to us to be a little bit of “all of the above.”

The fundamental issue, as we have come to see it, seems to be that PHP/MySQL applications can’t always run quite fast enough to keep up with fast, multi-threaded processors such as we run, especially when the applicaiton is making many requests to MySQL in the cource of its processing and/or potentially several PHP applications are making simultaneous connections/transactions.

It appears to us that unlike ASP classic and ASP.NET applications, PHP application cannot be isolated to their own application pools, but rather run globally on the server . . . . so, when one PHP application throws up this error it affects all PHP sites on the server for anywhere to a couple minutes to several hours.

Having pulled all of these clues together, we finally found this hot fix from Microsoft:
Errors may occur when you run CGI applications on a fast multi-processor computer that is running IIS 5.0 and IIS 6.0

However, when we attempted to install this hot fix, we found that it was already addressed in Windows 2003 Server Service Pack 2, which, of course, we were already running.

So, we had to go back to the drawing board and dig deeper in our research.

What we FINALLY discovered is that the problem is with running PHP as a CGI on a Windows server. It does go back to the issue of CGI’s not being able to keep up with the speed of our servers. The solution, ultimately, was to run PHP as an ISAPI rather than a CGI. Running PHP as an ISAPI has the advantage of using DLL’s to process PHP processes, which is more native to Windows servers, and, well, run faster. Doing this finally solved our problem with that nasty error: “The specified CGI application misbehaved by not returning a complete set of HTTP headers.”

Here’s how to do it:

1) Go to IIS Manager - > Web Service Extensions. Add a new Web service extension. In the New Web Service Extension dialog, name the new extentions ( can be anything; we chose “PHP5-ISAPI” ), click Add and browse to C:\PHP5\php5isapi.dll, and check “Set extension status to Allowed”.
Add the Web service extension

2) Find the website you want to run on PHP as an ISAPI, right click on it and select “Properties”, then go to the “Home Directory” tab and click the “Configuration” button. In the Application Configuration dialog, scroll down to the .php extension and click “Edit”. In the Add/Edit Applicaiton Extension Mapping dialog, click “Browse”, and navigate to C:\PHP5\php5isapi.dll again. Check “All Verbs”.
Change the application configuration for the website

Voi la, you’re website will now fire PHP as an ISAPI and you will be rid of that dreaded CGI error.

If you use a hosting control panel, such as HELM, you can skip the second step and just configure the IIS Settings in HELM to assign the PHP 5 application path to C:\PHP5\php5isapi.dll, and then just use HELM to assign PHP 5 to the web site ( which will then perform step two above for you ).

Tags: , , ,

4 Responses to “The specified CGI application misbehaved by not returning a complete set of HTTP headers”

  1. Nigel Says:

    Thank you! Was getting desperate, this fixed my problem.

  2. Edward Anil Joseph Says:

    Thank you very much.
    I too was searching for the exact fix and had the same issue of having SP2 installed in Windows 2003 with multi-processor setup.

  3. Luigi Fulk Says:

    Can I have this copied? I will link back to your blog. Is that ok?

  4. Sean@WMS Says:

    Sure, Luigi. Please do provide the link back to acknowledge authorship.

Leave a Reply

Subscribe without commenting