CakePHP Install: Multiple Subdirectories & 500 Error
Penguin January 21st, 2007
So, the last few weeks, I started learning how to use the CakePHP framework. It’s been a little slow going, but it’s been pretty good. I’ve been developing on my local machine, which, for some reason, mod_rewrite just refuses to work. So, I decided to make a dev site on the server. Oh, what a disaster.
I created the directory and ftp’d all the files on to the server. When I went to test it, I kept on getting a bunch of 500 errors. I tried moving the files around, looking at the “manual”, but nothing was solving the problem. I spent all afternoon, and narrowed it down to a problem with the .htaccess file.
When I came back from dinner, I found the solution.
The Problem:
- Installing CakePHP in a subdirectory;
- Resolving the 500 errors.
The Solution to Problem 1:
The original manual entry for this, wasn’t very good. I eventually found a tutorial in the bakery that did a better job of explaining how to do it.
Essentially, what you had to do was change 3 Define statements in /apps/webroot/index.php.
First, you create a directory above /public_html called /apps. Within the /apps directory, you put different subdirectorys for your different applications, (ie: gallery, blog, etc). For this example, we’ll use “blog”
Next, you put the /cake and /vendors directory above your /public_html directory.
Then you create a directory in your /public_html named after your application, /blog.
Finally, you move the entire contents of your /apps/blog/app/webroot directory to /public_html/blog
This is what your folders should look like, if you are looking at from the user root:
/
/apps/blog <- contains: config, controllers, models, plugins, tmp, vendors, views. Notice it does NOT contain webroot
/cake <- contains: core CakePHP files
/public_html/blog <- contains: /app/webroot
/vendors
The next step, was to open your /public_html/blog/index.php (originally in /app/webroot) file. You’ll scrow down to about line 41 and see the following (comments removed):
if (!defined(’ROOT’)) {
define(’ROOT’, dirname(dirname(dirname(__FILE__))));
}
if (!defined(’APP_DIR’)) {
define(’APP_DIR’, basename(dirname(dirname(__FILE__))));
}
if (!defined(’CAKE_CORE_INCLUDE_PATH’)) {
define(’CAKE_CORE_INCLUDE_PATH’, ROOT);
}
The first define describes the root of all your different applications. In this case, it will be /apps. The second define specifies the directory of the application core, in this case “blog”. And the last define establishes where the core CakePHP files can be found.
The way that these 3 defines work, is they set some php classpath includes. In other words, when PHP runs CakePHP, it will look in these 3 places for the required files. What that means to you, is you need to determine what the full, absolute path is. In my case, the full path to MY root directory is: /home/ninjavspenguin.
Editing the code above, I will get the following:
if (!defined(’ROOT’)) {
define(’ROOT’, DS.’home’.DS.’ninjavspenguin’.DS.’apps’);
}
if (!defined(’APP_DIR’)) {
define(’APP_DIR’, ‘blog’);
}
if (!defined(’CAKE_CORE_INCLUDE_PATH’)) {
define(’CAKE_CORE_INCLUDE_PATH’, DS.’home’.DS.’ninjavspenguin’);
}
It’s important to use the directory separator for portability and compatibility.
Notice the third define, it’s not saying where the core files are, but where /cake can be found.
And that was it!
The Solution to Problem 2:
For the life of me, I had no idea why I was getting 500 errors. I narrowed it down to a problem with the .htaccess file, but since my mod_rewrite skills are not 1337, I didn’t know how to debug it. I eventually found this google group that had the answer.
It turns out, that there are three key .htaccess files that CakePHP uses to do all its pretty URLs.
First: in the core cakephp directory
/cake
contents of .htaccess (unedited)
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
Second: in the app directory
/apps/blog
contents of .htaccess (unedited)
RewriteEngine on
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
Third: in the webroot directory
/public_html/blog
contents of .htaccss (unedited)
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
And that was it! My 500 woes were over!
Hopefully, this has will fix your issues.
Related posts
