2014-07-07

Baking with CakePHP on Ubuntu

OK so there are several blog posts out there on getting started with CakePHP and Ubuntu and installing it from scratch. Ubuntu does provide CakePHP in its repositories, and I've been trying to get to grips with going through the tutorial for the Blog application in the tutorial on http://book.cakephp.org/.

The example code blocks include the path for a user "ian" creating the "blog" application from the tutorial. This post only details the steps needed to get the application working correctly.

Preparation

As with everything else, Ubuntu doesn't have the latest & greatest in their repositories. 12.04 has 1.3.7 whereas 14.04 has 1.3.14. The one advantage of this is that this post will apply to a wide range if installations! :-)

To start you obviously need CakePHP installed. This is as simple as running
  sudo apt-get install cakephp cakephp-scripts  

This should install all dependencies too. My standard install has Apache, PHP and MySQL installed anyway.
My usual development route is to create in my userdir then release once I'm happy with it. This means that you need to enable the userdir nad rewrite mods in apache and then enable the php engine in the userdirs.
sudo a2enmod userdir
sudo a2enmod rewrite
vi /etc/apache2/mods-enabled/userdir.conf
  Change
    AllowOverride All
vi /etc/apache2/mods-enabled/php5.conf
  Comment out 
#            php_admin_value engine Off

Now we're ready to start baking!

Baking

So, create a public_html directory in your home directory and cd into it then run
  cake bake blog  

You'll then be presented with something like
Welcome to CakePHP v1.3.14 Console
---------------------------------------------------------------
App : public_html
Path: /home/ian/public_html
---------------------------------------------------------------
Bake Project
Skel Directory: /usr/share/php/cake/console/templates/skel
Will be copied to: /home/ian/public_html/blog
---------------------------------------------------------------
Look okay? (y/n/q)

Follow the prompts to set up the database configuration. NOTE:- Leave the database connection name as default.

Edit .htaccess

You need to add the following line to the .htaccess file in the application root directory and the webroot directory. Edit it to match the URL you enter to get the the application root in your web browser.

RewriteBase /~ian/blog/

Edit the application configuration

Given the path in the Baking section above, you need to edit /home/ian/public_html/blog/webroot/index.php to specify the full path to the directory:-

**
 * The full path to the directory which holds "app", WITHOUT a trailing DS.
 *
 */
        if (!defined('ROOT')) {
                define('ROOT', DS.'home'.DS.'ian'.DS.'public_html');
        }
/**
 * The actual directory name for the "app".
 *
 */
 if (!defined('APP_DIR')) {
  define('APP_DIR', 'blog');
 }

You should now have a working application that you can then enhance as per the Blog tutorial.

No comments:

Post a Comment