You can kill the background for speed, if you wish.[x]

Monday, May 3, 2010

CakePHP, cakephp-instaweb, and PHP 5.3

Today I tried to get a CakePHP app up and running on my home computer, with the excellent cakephp-instaweb, a nifty little Python script that lets you run any CakePHP app from anywhere on your computer. Unfortunately, it didn't work, and was giving me a HTTP 500 CGI Script Error: Premature End of Script Headers error. From looking around the internet, this basically means "Ahh! Something went wrong," which isn't terribly helpful. My first guess* was that my .htaccess files got messed up, which is a good first guess when such things go wrong, but everything was as it should be.

After some more digging, I found out that PHP 5.3 by default flipped a switch (cgi-bin.force-redirect) that previously had been defaulted to off. This issue, I suspect, cropped up because I just recently upgraded to Ubuntu 10.04 Lucid Lynx, and I'm assuming my PHP was upgraded in the process - it is indeed now at 5.3.

From what I can tell, it's a security measure that makes sure scripts are running from where they're supposed to be - which is great, except that that's exactly what CakePHP doesn't do. Fortunately, the solution was as simple as turning this off in my php.ini. A quick locate revealed three of those, in three subfolders of /etc/php5: /cgi, /apache2, and /cli. I initially turned them all off, but only the CGI version ended up needing it. Where exactly your php.ini is depends on your operating system, configuration, and all kinds of things. But if you run into an error like this, try un-commenting and changing the section in your php.ini to look like this:

; cgi.force_redirect is necessary to provide security running PHP as a CGI under
; most web servers.  Left undefined, PHP turns this on by default.  You can
; turn it off here AT YOUR OWN RISK
; **You CAN safely turn this off for IIS, in fact, you MUST.**
cgi.force_redirect = 0
Things should start working better, or at least they did for me.

*For general Cake debugging reference, the first thing you should suspect is your cache. If debug is set to 0, it doesn't recreate the cached models and such, so many modifications you make won't actually take effect. Setting debug to 1 or 2, in your core.php, or just clearing your app/tmp/cache folder (only the files inside it though, leave the folder there!) will often resolve some unexpected behaviors.


Anonymous said...

Thanks a lot :)

Matthew Babbs said...

This bit me yesterday when I was setting up a CakePHP dev environment on my netbook - thanks for the fix!

Joel Bradshaw said...

That's exactly where it bit me - trying to get it set up on my laptop. Glad this post could be of help! It's certainly not the only solution to such a vague error, but evidently is a common enough one to be helpful!

Tom Wright said...

Thanks man, worked a treat!

Ben Aylott said...

Thanks for working this out it saved me hours.

Unknown said...

Yea this got me also, thanks! Have you had any issues with holding a session though? Works fine using memcached in my live env but not using the db for sessions locally (nor files, php, cake). Suspecting its also related, keeps changing my session ID on redirect.

Anonymous said...

Thank's man, this works for me!