Deploying Cake PHP 2.0

This was updated for CakePHP 2.0 on 1/15/2012. I am also pretty active on github and cakePHP IRC if you have questions.

CakePHP is a popular PHP framework that many view as an alternative to ruby on rails. I was recently in need of a guide on how to deploy a CakePHP website. The documentation for 2.0 is quite lacking when it comes to this, so I thought I'd share what I found to be the correct order of things.

1. Prepare Your Application

Once you've finished any debugging, you should go into app/config/core.php and set your debug variable to 0.

Change this line:

Configure::write('debug', 2);

To the following:

Configure::write('debug', 0);

You also want to make sure you have some kind of caching on. You can find these settings at the bottom of the file. If you're using the default cache module, make sure the settings are as follows:

Cache::config('default', array('engine' => 'File'));

As the comments above this line in core.php note, you can change this to any of the other caching engines (if you have reasons for doing so) and you should probably specify some parameters. For more information on chaching in cakephp, read [this manual entry][2].

2. Upload your folders

There are three required folders that you can move around on your server. These are cake, app, and webroot. Your webroot directory shout go somewhere inside your document root directory for your domain, so if your host is Dreamhost, for example, and you are set up with /home/username/domain.com/ as your document root, the webroot directory should go in the domain.com/ directory as it is publically accessible.

Your cake and app folder should be in /home/username/ to better secure the application. It's fine that you take the webroot folder out of the app folder and place it in /home/username/domain.com/.

3. Make Changes to Index.php

Inside the webroot folder, edit Index.php and scroll down to the three global constants: ROOT, APP\_DIR, and CAKE\_CORE\_INCLUDE\_PATH. No matter where you have laid out your folder (as long as webroot is publically accessible), this is where you tell the application where its pieces are. This is the code you're looking for:

/**
 * The full path to the directory which holds "app",
 * WITHOUT a trailing DS.
 */
    if (!defined('ROOT')) {
        define('ROOT', dirname(dirname(dirname(__FILE__))));
    }
/**
 * The actual directory name for the "app".
 *
 */
    if (!defined('APP_DIR')) {
        define('APP_DIR', basename(dirname(dirname(__FILE__))));
    }
/**
 * The absolute path to the "cake" directory,
 * WITHOUT a trailing DS.
 */
    if (!defined('CAKE_CORE_INCLUDE_PATH')) {
        define('CAKE_CORE_INCLUDE_PATH', ROOT);
    }

Given the example above, I would set ROOT to /home/username, APP\_DIR to domain.com, and CAKE\_COre\_INCLUDE\_PATH to home/username. Cake uses DS as the separator, so you can just concatenate it on to create the address as following:

/**
 * The full path to the directory which holds "app",
 * WITHOUT a trailing DS.
 */
    if (!defined('ROOT')) {
        define('ROOT', DS . 'home' . DS . 'username');
    }
/**
 * The actual directory name for the "app".
 *
 */
    if (!defined('APP_DIR')) {
        define('APP_DIR', 'domain.com');
    }
/**
 * The absolute path to the "cake" directory,
 * WITHOUT a trailing DS.
 */
    if (!defined('CAKE_CORE_INCLUDE_PATH')) {
        define('CAKE_CORE_INCLUDE_PATH', 'home' . DS . 'username');
    }
?>

I hope this helps anyone trying to find out how to deploy CakePHP 1.3 in general or at least on Dreamhost. Email me if you have any questions, I will usually give a prompt response.