Baking a Cake – Installation & Configuration

Published on and tagged with baking a cake  cakephp  configuration

Last week we defined the requirements and the domain model. Now, before we can start with the realization of our application we have to install and configure CakePHP. So the goal for today is to get the Cake welcome screen.

I will describe an advanced setup (i.e the core of CakePHP will be used by multiple applications) as I think it is a common setup for developers working on different projects. Such a setup involves more steps than a default setup, so if you want to do a default setup you have to skip some steps.

Get CakePHP: You can download a release of CakePHP from CakeForge, or you can get it directly via Subversion (https://svn.cakephp.org/repo/trunk resp. https://svn.cakephp.org/repo/branches/1.2.x.x for the development branch). For the application I will use the version from the development branch.

Extract archive file: For the advanced setup we only need the “cake” and “vendors” folder plus the two files “.htaccess” and “index.php”. Extract them from the archive file, it doesn’t matter where you place them. In my case, I have placed them in /home/dho/projects/cake_1.2.x.x

Bake the application skeleton: CakePHP comes with a command line tool called bake (it is available in cake/scripts) which allows you to create the application skeleton very easily. The screenshot should be self-explanatory:

Baking the application skeleton

Create the database: As database I will use MySQL (you could also use PostgreSQL, MS SQL Server, and some others, even though I don’t know how mature those drivers are). To create the database I use the command line tool which comes with MySQL as it is in my opinion the fastest way to create a database. As I will name the application “n” (short for notify), the database is also named “n”.

dho@tumulux:~$ mysql -u root
mysql> create database n;
mysql> quit
dho@tumulux:~$

Define database settings: First we have to rename app/config/database.php.default to database.php:

dho@tumulux:~/projects/n/trunk/app/config$ mv database.php.default database.php

And then we can define the database settings (you can remove the $test connection as it is not used):

class DATABASE_CONFIG
{
    var $default = array('driver' => 'mysql',
                                 'connect' => 'mysql_connect',
                                 'host' => 'localhost',
                                 'login' => 'root',
                                 'password' => '',
                                 'database' => 'n',
                                 'prefix' => '');
}

Edit app/webroot/index.php: In this step we have to specify the location of the Cake core. For this purpose we have to change the value of the constant CAKE_CORE_INCLUDE_PATH in app/webroot/index.php. In my case the statement is now:

define('CAKE_CORE_INCLUDE_PATH', '/home/dho/projects/cake_1.2.x.x');

Add hosts entry: For each project I have an entry in the form project.localhost in the file /etc/hosts. That allows me to use virtual hosts in Apache (see next step). For this application it looks like:

127.0.0.1    n.localhost    localhost

Add virtual host: The previous step is done so that I can use a virtual host for the application on my local machine. So I have to add the following to /etc/apache2/sites-available/default:

NameVirtualHost *
#
# other virtual hosts
#
<VirtualHost *>
    ServerName n.localhost:80
    DocumentRoot /home/dho/projects/n/trunk/app/webroot

    ErrorLog /var/log/apache2/n_error.log
    CustomLog /var/log/apache2/n_access.log combined
</VirtualHost>

Please note that the DocumentRoot points to app/webroot, i.e. only the webroot folder (and its subfolders) will be directly accessible with the browser, all other folders are not accessible.

After restarting Apache with

apache2 -k restart

we should now get the welcome screen when we go to http://n.localhost. And voilĂ , here it is:
Application baked

Change value of CAKE_SESSION_STRING: Since revision 4565 you will get the following notice when you go to http://n.localhost:

Notice: Please change the value of CAKE_SESSION_STRING in app/config/core.php to a salt value specific to your 
application in /home/dho/projects/cake_1.2.x.x/cake/bootstrap.php on line 54

So we have to do what is stated in the notice, we have to change the value of the constant CAKE_SESSION_STRING in app/config/core.php. After a reload the notice will disappear and we will get the screen as shown above.

So, mission accomplished for today. The next mission already waits…

Update (2007-03-04): Added last step “Change value of CAKE_SESSION_STRING”

10 comments baked

  • kabturek

    i use virtual hosts also- only i choose a domain like ‘project’ and not ‘project.localhost’ :) It’s pretty convenient

    You’re on Kubuntu right ? i didn’t check if the apache2 configuration files layout is the same on other distros….
    I’ve got a little /bin/sh script that automates the creation of the project – copying skeleton , setting paths in index.php , database etc. Got to change some things in it and publish it somewhere :) but it will be after all my exams ;)

    back to the series – they’re great. It’s good to have these guides – especially for newbies :)

    greets,

  • Anonymous

    I personally just use top-level virtualhost hostnames (i.e. ‘n’ instead of ‘n.localhost’) to each his own.

  • Anonymous

    Whoops, just saw kabturek’s post!

    Sorry about the ‘me too’.

  • KesheR

    The truth is I feel more comfortable copying and pasting than using the bake script.

  • cakebaker

    @All: Thanks for your comments.

    @kabturek, Anonymous: Yeah, I think it is just a matter of taste whether you use “project” or “project.localhost” as host name.

    @kabturek: No, I don’t use Kubuntu, I use a plain Debian installation. I would be interested in this script, as up to now I was too lazy to write one for myself ;-) Oh, and good luck with your exams!

    @KesheR: Yes, that’s another way to create the application skeleton. The advantage of using the bake script is that it automatically sets the necessary permissions for the tmp folder.

  • hepper

    What’s is happening. When is the next part coming? :)

  • cakebaker

    @hepper: The next part probably comes this weekend. Stay tuned ;-)

  • Some handy CakePHP links. « Digital Spaghetti

    […] Baking a Cake: A handy tutorial on how to set up your CakePHP application on your server. […]

  • Web App Procrastination by jpsykes

    […] to taking an idea from concept to reality, I love the fact that I can map out my DB and using bake have a local CRUD system in place for the data that my application will manipulate in a matter of […]

  • Cake installation | Hailium

    […] Baking a Cake – Installation & Configuration – cakebakerFeb 15, 2007 … So the goal for today is to get the Cake welcome screen. I will describe an advanced setup (i.e the core of CakePHP will be used by multiple … […]

Bake a comment




(for code please use <code>...</code> [no escaping necessary])

© daniel hofstetter. Licensed under a Creative Commons License