Text size +/-

31 Aug 07 _ CakePHP and Internal Server Errors


By casey
in Casey's Corner

There’s nothing quite like feverishly refining your Cake app on your local machine, only to find out that upon installation on your production server, you’re greeted with a frightening Apache message: Internal Server Error (500). Not only is the message extremely unhelpful, but you have no feedback at all from Cake’s debug. It’s a bad, frustrating, stupid thing, especially when you know, KNOW, everything should work just fine.

So what to do in a situation like this?

I recently had this problem with one of our clients using the MediaTemple hosting service. A standard upload of our Cake app, complete with the current version of Cake (1.2.0.5427alpha) would immediately result in the 500 Server Error. A fresh install of the same Cake core, however, would load fine. After adding controllers, views, and models of our app one file at a time, the whole system seemed to work. Rushing forward with a sense of heady self-satisfaction, I uploaded the site data (javascripts, images, and css files) only to discover that upon completion, the site no longer worked. Back to 500 Server Errors!

It was a stupid, stupid problem that had no business being a problem. After doing some furious Googling, it turns out that quite a few people have had errors like this, but under completely different circumstances.

Errors like the 500 Internal Server Error are most commonly caused by badly-written .htaccess files. Usually it’s from a redirect that redirects back to itself, which redirects back to itself, which redirects back to….well, you get the idea. Apache is smart enough to catch on to this situation pretty quickly and spit out the error.

With Cake, this can occur under the right server conditions in the .htaccess file located in the /app/webroot/ folder. If you’re having an error situation like this, try changing the .htaccess file from:


RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]

to:


RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php?url=$1 [QSA,L]

Got that? You have to add a preceding slash to index.php.

That may not fix your problem. It didn’t fix mine. Ultimately, it turned out that the very PHP version running on MediatTemple (4.4.7) was causing the problem. Honestly, I don’t know what, exactly, it was doing. I don’t know how it was causing the server error. All I know is that when I switched PHP versions to the 5.0 branch, the problem went away.

So there you have it. Use PHP 5. I think this can be a lesson to all of us. Especially those are considering a future in web development.

Spread the Word:
  • Slashdot
  • Digg
  • Facebook
  • Reddit
  • del.icio.us
  • StumbleUpon
  • Technorati
  • NewsVine


Comments are closed.