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:

  1. Installing CakePHP in a subdirectory;
  2. 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
  • obert
    Thanks alot, but what is the index.php file supposed to look like in the root directory? There still must be one there, but you only detail what to do with the index.php in /blog. Can you elaborate further?
  • Roland
    Ups, solved
  • Roland
    Hi!

    Thx for the post, helped me a lot!
    now everything works, expect that I don't have a clue on where to find the logs.
    Where should the tmp folder be.
    There are no logs written to /apps/blog/tmp/logs nor http/tmp nor http/blog/tmp :-/

    Any tipps?

    thx
    Roland
  • Phil
    I can't thank you enough, I have been stuck trying to get my cake app onto a test site for weeks and this post helped me solve the problem. Thanks a million!
  • mariojulia4
    Your blog is very much useful and rocking dear. you have provided detailed knowledge and information about today's latest technologies, online communications and other businesses. If you want to get the knowledge of latest technologies, then you must take a latest information. For the some developing success, you can get information about cheap web hosting. Thanks a lot for your great post. I really enjoy your post and also wait for your latest posts. Well done.
  • Hey there,

    Just a short note for CakePHP 1.2: If you want to solve the 500 Error with least changes possible just add the appropriate RewriteBase command to the app/webroot/.htaccess file. This is where any request lands somewhere during the "bootstrap process" anyway and it is easy to remove once an application goes live on a single domain.

    Cheers
  • Damn, I wish I had found this post two weeks ago. I would have saved so much configuration time. My .htaccess files had all disappeared, so copying and pasting the Apache code from this post still saved the day.

    Awesome job.
  • NinjaVsPenguin
    Hi rollins,

    That's some nice .htaccess ninja you got going. I'm glad you were able to find a solution that works for you :)

    -Penguin
  • Thanks for the tips. I ended up just sticking the entire cake directory into ~/public_html, and named it for my application. Then I took Alun's advice and added RewriteBase /~user/application to each of the .htaccess files. Then I just stuck a redirect from ~user to ~user/applicaiton an I was good to go.
  • NinjaVsPenguin
    Hi Scott,

    I never actually got Bake to work, not even in the "normal" configuration. So, unfortunately, I can't be of any help.

    Good luck with your projects!
    -Penguin
  • scott
    I changed my setup to match this configuration and ALMOST everything was working. I couldn't get the bake console script to work. It wouldn't copy files when creating a project or write the models files.

    I moved the app files below the webroot and bake starting working again.

    Any idea why?
  • The locations of the three files are

    root/
    root/app
    root/app/webroot

    not cake as you suggested above ;)

    Thanks for the articel though it's helped me sort my problem out. I had to add RewriteBase / to each .htaccess and it all works great now :)
  • joren
    addition :
    due to posting the ’ needs to be changed to ' (the single apos next to enter)
  • NinjaVsPenguin
    Thanks, Joren! It looks great!
  • joren
    hey thanks alot for the help to start, i have made some modifications that might help alot of people ..

    these small modifications will save you lots of works!!

    what they do are the following :
    generate on-the-fly the ROOT-location and more!!!

    i have tested it and i loved it myself :)

    ===========================

    so what you need to do is, open the original /app/webroot/index.php
    go to line 41 and replace the following lines with the original!


    chdir("../../");
    $root = getcwd();

    if (!defined('ROOT')) {
    //define('ROOT', 'FULL PATH TO DIRECTORY WHERE APP DIRECTORY IS LOCATED DO NOT ADD A TRAILING DIRECTORY SEPARATOR';
    //You should also use the DS define to seperate your directories
    define('ROOT', $root.DS.'apps');
    }
    if (!defined('APP_DIR')) {
    //define('APP_DIR', 'DIRECTORY NAME OF APPLICATION';
    define('APP_DIR', basename(dirname(__FILE__)));
    }
    /**
    * This only needs to be changed if the cake installed libs are located
    * outside of the distributed directory structure.
    */
    if (!defined('CAKE_CORE_INCLUDE_PATH')) {
    //define ('CAKE_CORE_INCLUDE_PATH', FULL PATH TO DIRECTORY WHERE CAKE CORE IS INSTALLED DO NOT ADD A TRAILING DIRECTORY SEPARATOR';
    //You should also use the DS define to seperate your directories
    define('CAKE_CORE_INCLUDE_PATH', $root);
  • Dave
    Nice article. Very helpful. Can you attach your htaccess per your article? Since you moved your webroot dir you can't be using:

    RewriteEngine on
    RewriteRule ^$ app/webroot/ [L]
    RewriteRule (.*) app/webroot/$1 [L]

    I am a newbie to working with rewrite rules and I can't figure out how to path it properly.
  • Thanks for this post. Part 2 should be included into cake manual! :-)
  • NinjaVsPenguin
    Yes, the solution to problem 2 is complete. You shouldn't have to edit the .htaccess files. The only one you might have to edit is the first one:

    First: in the core cakephp directory
    /cake

    contents of .htaccess (unedited)

    RewriteEngine on
    RewriteRule ^$ app/webroot/ [L]
    RewriteRule (.*) app/webroot/$1 [L]

    This tells cakePHP where you webroot is. If you have a basic install, then this shouldn't need to change. If you have a different directory for your apps, then you'll need to change the directories accordingly.

    HTH.

    -Penguin
  • jahypee09
    Hi. Is the solution for problem 2 complete? I can't seem to find the edited versions of the 3 .htaccess files? Can I have the link if it's posted someplace else? Thanks! =)
blog comments powered by Disqus