Friday, August 22, 2008

Cakephp 1.2.x tutorial for Ubuntu Firebird and Nginx

You're probably checking out this tutorial because you want to use firebird  with cakephp.


We will create acreation of a simple blog application. We'll be getting and installing Cake, creating and configuring a database, and creating enough application logic to list, add, edit, and delete blog posts.
Here's what you'll need:
  1. A running web server. We're going to assume you're using Nginx , that is the recommended for performance and for it's simple elegance
  2. A database server. We're going to be using Firebird in this tutorial. You'll need to know enough about SQL in order to create a database but is good if you know the Relational way.
  3. Basic PHP knowledge. The more object-oriented programming you've done, the better
  4. Finally, you'll need a basic knowledge of the MVC programming pattern. A quick overview can be found in Chapter "Beginning With CakePHP", Section : Understanding Model-View-Controller
Installing nginx can be done by using this php script in ubuntu/debian

$ sudo apt-get install git-core php5-cli
$ git clone  git://github.com/mariuz/firebird_scripts.git
$ cd firebird_scripts
$ sudo php nginx-build.php

Follow this guide to install firebird and php5-interbase
https://help.ubuntu.com/community/Firebird2.5

add firebird repository

sudo pico /etc/apt/sources.list.d/firebird.list

and add
deb http://ppa.launchpad.net/mapopa/ubuntu natty main
deb-src http://ppa.launchpad.net/mapopa/ubuntu natty main

$apt-get update
$sudo apt-get -y --force-yes install firebird2.5-super php5-interbase
$sudo dpkg-reconfigure firebird2.5-super

For installing firebird you can run this script from the firebird-scripts
it will do all the above steps

$sudo php firebird-install.php


and optionally flamerobin if you are on desktop side
$sudo apt-get install flamerobin

Getting Cake


First, let's get a copy of fresh Cake code.
To get a fresh download, visit the CakePHP project at github:
https://github.com/cakephp/cakephp/tags
and download the stable release. For this tutorial you need 1.2.x.x

$ wget https://github.com/cakephp/cakephp/tarball/1.2.11 -O cakephp-1.2.11.tar.gz
$tar -zxvf cakephp-1.2.11.tar.gz
$ chown -R www-data.www-data cakephp-cakephp-*
$ sudo mv cakephp-cakephp-* /opt/nginx/html/firetube
or run this script from the firebird-scripts
$sudo php cake-install.php


Regardless of how you downloaded it, place the code inside of your DocumentRoot. Once finished, your directory setup should look something like the following:
/opt/nginx/html/firetube
/app
/cake
/docs
/vendors
.htaccess
index.php


Now might be a good time to learn a bit about how Cake's directory
structure works: check out Chapter "Basic Principles of CakePHP", Section :
CakePHP File Structure.

load localhost and you should see something like this




Creating the Blog Database

Next, lets set up the underlying database for our blog. Right now, we'll just create a single table to store our posts. We'll also throw in a few posts right now to use for testing purposes. Execute the following SQL statements into your database
I have created the database with flamerobin

Database -> Create new database
Display name:firetube
Database:path:/var/lib/firebird/2.5/data/firetube.fdb
Username:sysdba
password:masterkey

/****************** GENERATORS ********************/
CREATE GENERATOR GEN_POSTS_ID;
/******************** TABLES **********************/
CREATE TABLE POSTS
(
ID Integer NOT NULL,
TITLE Varchar(50),
BODY Varchar(2000),
CREATED Timestamp,
MODIFIED Timestamp,
PRIMARY KEY (ID)
);
/******************** TRIGGERS ********************/
SET TERM ^ ;
CREATE TRIGGER POSTS_BI FOR POSTS ACTIVE
BEFORE INSERT POSITION 0
AS
BEGIN
IF (NEW.ID IS NULL) THEN
NEW.ID = GEN_ID(GEN_POSTS_ID, 1);
END^
SET TERM ; ^
GRANT DELETE, INSERT, REFERENCES, SELECT, UPDATE
ON POSTS TO SYSDBA WITH GRANT OPTION;
INSERT INTO "POSTS" ("TITLE","BODY","MODIFIED","CREATED") VALUES ('Foo bazz bar','test blog ','now','now');

INSERT INTO "POSTS" ("TITLE","BODY","MODIFIED","CREATED") VALUES (' bazz bar Foo','test blog2 ','now','now');





The choices on table and column names are not arbitrary. If you follow Cake's database naming conventions, and Cake's class naming conventions (both outlined in "CakePHP Conventions"), you'll be able to take advantage of a lot of free functionality and avoid configuration. Cake is flexible enough to accomodate even the worst legacy database schema, but adhering to convention will save you time.
Check out "CakePHP Conventions" for more information, but suffice it to say that naming our table 'posts' automatically hooks it to our Post model, and having fields called 'modified' and 'created' will be automagically managed by Cake.

Cake Database Configuration


A copy of CakePHP's database configuration file is found in /app/config/database.php.default. Make a copy of this file in the same directory, but name it database.php.
The config file should be pretty straightforward: just replace the values in the $default array with those that apply to your setup. A sample completed configuration array might look something like the following:
var $default = array(
'driver' => 'firebird',
'persistent' => false,
'host' => 'localhost',
'port' => '',
'login' => 'sysdba',
'password' => 'masterkey',
'database' => '/var/lib/firebird/2.5/data/firetube.fdb',
'schema' => '',
'prefix' => '',
'encoding' => ''
);

Once you've saved your new database.php file, you should be able to open your browser and see the Cake welcome page. It should also tell you that your database connection file was found, and that Cake can successfully connect to the database.


Optional Configuration


There are two other items that can be configured. Most developers complete these laundry-list items, but they're not required for this tutorial. One is defining a custom string (or "salt") for use in security hashes. The second item is allowing CakePHP write access to its tmp folder.
The security salt is used for generating hashes. Change the default salt value by editing /app/config/core.php line 153. It doesn't much matter what the new value is, as long as its not easily guessed.
/**
* A random string used in security hashing methods.
*/
Configure::write('Security.salt', 'pl345e-P45s_7h3*S@l7!');
The final task is to make the app/tmp directory web-writable. The best way to do this is to find out what user your webserver runs as () and change the ownership of the app/tmp directory to that user. The final command you run (in *nix) might look something like this.
$ chown -R www-data app/tmp
If for some reason CakePHP can't write to that directory, you'll be informed by a warning while not in production mode.

So all it should be all green in configuration check




Note on rewrite on nginx


Occasionally a new user will run in to rewrite issues, so I'll mention them marginally here. If the CakePHP welcome page looks a little funny (no images or css styles), it probably means rewrite isn't functioning on your system. Here are some tips to help get you up and running on nginx

location / {
root html/firetube/app/webroot;
index index.php;
# If the file exists as a static file serve it
# directly without running all
# the other rewite tests on it
if (-f $request_filename) {
break;
}
if (!-f $request_filename) {
rewrite ^/(.+)$ /index.php?url=$1 last;
break;
}
}

You can follow now the part 2 from this tutorial

4 comments:

Ian in Japan said...

Correction:

In the "Getting Cake" section between the mv and chown commands you are missing a "tar" before the "jxvf" options!

Popa Adrian Marius said...

thanks this spotting now is ok

I tested only from script

Unknown said...

Hey... Looks like you just copied your article from the cake website:

http://manual.cakephp.org/view/326/The-Cake-Blog-Tutorial

Do you think that's fair?

Popa Adrian Marius said...

yes it is modified tutorial for nginx and firebird only but it will be an video aplication later

http://github.com/mariuz/firetube/tree/master