How to Install and Setup Kohana, a PHP Web-Application Development Framework
Amongst many PHP based frameworks, Kohana sets itself apart from the rest with its ideology of following common conventions and rules to develop fully object oriented web applications. This BSD licensed framework does not come with commercial strings attached and it has a build-by-the-community, for-the-community type of philosophy.
In this three-part DigitalOcean series, we will be covering all the essentials a developer should be familiar with in order to start building web applications using the PHP5 Kohana framework. We will begin with going over some of the basics, continuing through the key concepts and modules after installing the framework.
Note: This is the first article in our Kohana series - and it mainly consists of the basics and its installation. To continue with learning the framework to create web-applications, check out its sequel Building Web Applications with HMVC PHP5 Framework Kohana.
1. Web Application Development Frameworks
1. What Are Frameworks? 2. What Makes a Framework "Light"?
2. Kohana Framework
1. Kohana's Features 2. Kohana's Standard (Out-of-The-Box) Modules
3. Model - View - Controller Pattern
1. Routing Structure 2. Model 3. View 4. Controller 5. Template Files
4. Programming with Kohana and Preparations
5. Downloading and Installing the Kohana Framework
6. Getting Started with Kohana Installation
1. Bootstrapping The Setup 2. Setting Application Directory Permissions 3. Finalizing Everything and Testing
Web Application Development Frameworks
Kohana is a web application development framework. Given PHP’s nature as a language and the way the code written is executed on computers, there is no strict requirement to exploit frameworks to quickly develop applications when working with it. However, for any serious application that aims to have a long(-ish) lifecycle (making use of code already written once and with more than a single developer working on it), using a framework means making a ton of things much simpler.
What Are Frameworks?
Much like the dictionary definition of the word framework, web application frameworks provide both an essential structure to begin developing [applications] and a glue layer to hold everything together in a sound and logical way that makes sense for those who are familiar with the framework itself.
These bases come with many of the necessary common tools that are almost always needed to develop web applications such as processing incoming requests, generating and populating templates, returning responses, handling security and authentication, managing cookies (and sessions) and more.
What Makes a Framework “Light”?
Depending on the amount of tools a framework is shipped with out of the box, it is either referred to as a lightweight or an all-in-one (full stack, batteries included, etc.) solution. Kohana, albeit being extremely powerful and functionally rich, can still be considered light because of the freedom it gives to developers working with it, and the way it has been designed and set to operate.
Kohana HMVC (Hierarchical Model View Controller) framework offers - probably - all the tools necessary to build a modern web application that can be developed rapidly and deployed/maintained easily using the PHP  language.
Compared to other similar solutions, Kohana sets itself apart not with its features but with the way it presents these features and how it performs them.
Kohana comes with many of the commonly required additional tools (modules)such as encryption, validation, database access etc.
It offers the possibility to simply expand the defaults.
Allows building commercial applications with its BSD licensing.
Getting started and setting up is extremely fast and easy compared to heavy and complicated frameworks.
All the modules and the way things function are designed and built using classes and object. The framework sustains the “Don’t Repeat Yourself” principle.
Offers profiling and debugging tools.
Its code is very well documented and it comes with a relatively good documentation with examples and good explanations.
Prefers following conventions over [endless and frustrating] configurations.
Kohana’s Standard (Out-of-The-Box) Modules
Below are some of the out-of-the-box modules of Kohana.
Auth: User authentication and authorization.
Cache: Common interface for caching engines.
Codebench: Code benchmarking tool.
Database: Database agnostic querying and result management.
Image: Image manipulation module.
ORM (Object Relational Mapper): A modeling library for object relational mapping.
Unittest: Unit testing module.
Model - View - Controller Pattern
The MVC (Model - View - Controller) application pattern is used to divide code and logical structures into groups depending on their role and what they are set out to perform. Each of these parts process information within themselves and then share the necessary output between each other to complete jobs collectively, forming the final presentation (i.e. results) to the end user (i.e. the result of a URL visited).
Following the MVC pattern, a request goes through a process - similar to the example below - before a result gets returned.
(1) (2) (3) Request ---> Parsing ---> Matching [Data] ..  >> ..  >    .. .. .>. .. . ........ (4) (5) (6) Routing ---> Controller ---> Response ----- .. >> .. >> .. ../\ .. /\  >>  >> [Data] || . || \/ . \/ Model View
In model, definition of object classes and handling the data operations exist. In this layer, there is no direct interaction with other parts of the application (e.g. views). When a new event takes place, model let’s its parent (i.e. the controller) know.
View layer consists of files where the views (e.g. data representations) are generated. The controller object, using the view, presents the final result to the user.
In controller, the parsed data from the request gets processed using the model and the view, generating the file response through actions. Controllers act like a glue, connecting all pieces to work together.
Template files form a base which are generally used to facilitate maintenance of the representation of certain data presented by the application to the end user. In terms of PHP applications, PHP language equally acts like a templating language hence providing the templating syntax.
Programming with Kohana and Preparations
Kohana, as a light framework, consists of a bunch of files scattered across carefully structured directories which, in the end, is transferred to the production server and used to run the web application. Therefore, each Kohana package can be considered a [new] web application.
Note: In our examples, we will be working on a droplet, running the latest version of Ubuntu. To build web applications with Kohana, you can work on your home computer until the production step and later push your code for publication.
Note: We are going to use a default LAMP (Linux - Apache - MySQL - PHP) set up in our droplet to work with Kohana. To quickly set up a LAMP stack on a Ubuntu droplet, you can use:
sudo apt-get install tasksel sudo tasksel install lamp-server
Downloading and Installing the Kohana Framework
The latest available version of Kohana is
3.3.1. In order to download it to our VPS, we will use
wget (i.e. the GNU Wget command line tool).
After the download, we need to expand the zipped package. For this we will be using the unzip command and set “"my_app as the extraction folder.
# You might need to install *unzip* before extracting the files aptitude install -y unzip # Unzip and extract the files unzip kohana-v3.3.1.zip -d my_app # Remove the zip package rm -v kohana-v3.3.1.zip
Once we are ready with the framework package, we can move it to a more permanent location to get it to work with Apache. The default location for our LAMP installation is /var/www/
# Remove the *index.html* inside /var/www rm -v /var/www/index.html # Move the application directory inside mv my_app /var/www/ # Enter the directory cd /var/www/my_app
From now on, your installation will be accessible from the WWW.
# Visit: http://[your droplet's IP adde.]/my_app/ http://184.108.40.206/my_app/
Note: Kohana is not yet ready to work. Its configuration needs to be set first (i.e. bootstrapped).
Getting Started with Kohana Installation
Bootstrapping The Set Up
Before we start going over the steps to learn about developing an application, let’s bootstrap and finish off its installation procedure.
Run the following to edit the bootstrapping file using the nano text editor:
Edit your timezone:
# Find date_default_timezone_set and set your timezone date_default_timezone_set('Europe/London');
Set your locale:
# Find setlocale and set your locale setlocale(LC_ALL, 'en_UK.utf-8');
Set the base application directory location:
# Find base_url and set the base application directory # Relative to the base Apache directory (i.e. /var/www/) Kohana::init(array( 'base_url' => '/my_app/', ));
# Find Kohana::modules and uncomment them Kohana::modules(array( 'auth' => MODPATH.'auth', // Basic authentication 'cache' => MODPATH.'cache', // Caching with multiple backends 'codebench' => MODPATH.'codebench', // Benchmarking tool 'database' => MODPATH.'database', // Database access 'image' => MODPATH.'image', // Image manipulation 'orm' => MODPATH.'orm', // Object Relationship Mapping 'oauth' => MODPATH.'oauth', // OAuth authentication 'pagination' => MODPATH.'pagination', // Paging of results 'unittest' => MODPATH.'unittest', // Unit testing 'userguide' => MODPATH.'userguide', // User guide and API documentation ));
Save and exit by pressing CTRL+X and confirm with Y.
Setting Application Directory Permissions
In order to run Kohana, we need to mark two of its folders writable.
sudo chmod -R a+rwx application/cache sudo chmod -R a+rwx application/logs
Finalizing Everything and Testing
Once we are done with bootstrapping the set up and configuring folder permissions, we can test it all again by visiting the application using a web browser
# Visit: http://[your droplet's IP adde.]/my_app/ http://220.127.116.11/my_app/
When you confirm that everything is set correctly and working fine, you can remove the
Run the following to remove the install file:
rm -v install.php
If you re-visit the URL from the previous step, you will be welcomed with a hello, world! message. This means that our requests are now routed through the HMVC process following the pattern correctly.