Setting up Kohana 3.2 on Mac OS X

by greg on November 21, 2011

I’ve been doing more coding than blogging lately. My tool of choice is Kohana, a hierarchical model-view-controller PHP framework – it forces me to follow some rules, and when you’re a relative beginner like me, you need to follow some rules.

While Kohana is powerful, I’ve found it has a bit of a learning curve. The documentation isn’t as strong as it could be, and the extensive changes between 2.X, 3.0, and 3.1 make it difficult to pick up and work with existing tutorials written for older versions. The only long-form book written for Kohana, Packt’s Kohana 3.0 Beginners Guide, was published in August and (while informative) is already a couple of versions behind.

For the beginner, setting up Kohana can be frustrating. This post covers some of the ‘gotchas’ I’ve run into on Mac OS X.

After downloading the Kohana 3.2 framework, unzip it and place the contents of the unzipped folder in the directory where you plan on doing your development. I keep my projects in a ~/Development folder, each in their own directory named after the project. If I were create a project called ‘projectname’, I’d copy the contents of the unzipped kohana-3.2-master-1 folder to ~/Development/projectname.

First, enable Web Sharing from the Sharing pane of your System Preferences.app. That will activate the stock Apache web server included with Mac OS X. (Yes, you could install Apache and PHP independently from the stock versions, but unless there’s a compelling reason I’d rather work with what I’ve got.)

Next, you’ll need to tell Apache to work with PHP5 by modifying Apache’s config file. Open /private/etc/apache2/http.conf with your favorite editor and look for this line:

#LoadModule php5_module libexec/apache2/libphp5.so

Remove the # in front of it to load Apache’s PHP5 module:

LoadModule php5_module libexec/apache2/libphp5.so

While you’re editing Apache’s http.conf, you’ll want to make sure http.conf can be overridden by an .htaccess file in your Kohana directory, which will contain configuration specific to your Kohana application.

Search for the section starting with . In that section, look for the AllowOverride directive. Change this to read AllowOverride All. Now you can save and close http.conf.

You’ll need to restart Apache for these changes to take effect, which you can do with the command sudo /usr/sbin/apachectl restart.

At this point, I encountered a rather cryptic error:

/usr/sbin/apachectl: line 73: ulimit: open files: cannot modify limit: Invalid argument

Happily, someone more knowledgable than me had already solved the problem. Should you have this error, edit the /usr/sbin/apachectl script, and replace ULIMIT_MAX_FILES="ulimit -S -n `ulimit -H -n`" with ULIMIT_MAX_FILES="". You then will successfully be able to restart Apache.

Next, we need to get Mac OS X’s built in webserver to serve the contents of ~/Development/projectname. The default webserver directory on Mac OS X is /Library/WebServer/Documents. We could do this by editing Apache’s http.conf to change this default, but I prefer to create a symbolic link:

sudo ln -s ~/Development/projectname /Library/WebServer/Documents/projectname

This will make your Kohana application accessible at the http://localhost/projectname URL. However, Kohana out of the box expects your application is at webroot – that is, at the http://localhost URL.

Let’s fix this. In your projectname directory, in the file application/bootstrap.php, find this code:

Kohana::init(array(
'base_url' => '/',
));

and change it to this:

Kohana::init(array(
'base_url' => '/projectname/',
));

You’ll also want to set up your local .htaccess file. The rules in .htaccess force all URLs to be redirected to index.php/URL, allowing Kohana to do its magic. Open the example.htaccess file in your projectname directory and change the RewriteBase / line to RewriteBase /projectname/. Then save the file as .htaccess, not example.htaccess.

From this point on, you can follow the usual Kohana installation procedure – opening http://localhost/projectname, looking at the tests, and modifying the application/cache and application/logs directories to be writable with the following commands:

chmod 777 ~/Development/project-name/application/cache
chmod 777 ~/Development/project-name/application/logs

And that’s it!

I apologize in advance for all errors – or missed ‘gotchas’ – which are inevitably present. If there’s something that needs correcting, leave a comment.

{ 0 comments… add one now }

Leave a Comment

Previous post:

Next post: