Saturday, January 23, 2010

CakePHP: Initial Problems

I just started with CakePHP, a PHP framework, and have been having some problems going through the "15 minutes" blog tutorial. It took me about 2 days to dig my way out through openSUSE, Apache, Linux and CakePHP itself.

First issue I had was enabling mod_rewrite on openSUSE. To do that, edit the file: "/etc/sysconfig/apache2" (Assuming Apache2.2)
  • Go to the line that starts with "APACHE_MODULES" and add "rewrite" at the end just before the closing quote.
  • Save the file then run "sudo SuSEconfig" to update Apache's configuration files.
I'm not using virtual hosts. I have downloaded the latest stable CakePHP and extracted it in my "/srv/www/htdocs" directory and named it "blog"

mj@linux-mlvm:~$ ls -l /srv/www/htdocs
drwxr-xr-x  5 mj   users 4096 2010-01-23 02:28 blog
So I access the blog through: http://localhost/blog/

After configuring the database and the connection settings for CakePHP as instructed in the tutorial, I had to set permissions for the tmp directory: "/srv/www/htdocs/blog/app/tmp"

sudo chown wwwrun /srv/www/htdocs/blog/app/tmp
This will set ownership of the directory to the user running Apache. In my case, it's wwwrun. On a shared web host, it's often nobody. You can find out which username Apache uses by making a small PHP script:
<?php echo `whoami`; ?>
Visit the script in a web browser and voila!

Second issue: when reaching the step to create the posts view, I created the directory:
mkdir app/views/posts
But I got an error message in the browser when visiting the link: http://localhost/blog/posts:

Missing View
Error: The view for PostsController::index() was not found.
Error: Confirm you have created the file: /srv/www/htdocs/blog/app/views/posts/index.ctp
Notice: If you want to customize this error message, create app/views/errors/missing_view.ctp

After verifying that everything is properly spelled and whatnot, turns out it was a dumb mistake of not checking directory permissions:
mj@linux-mlvm:/srv/www/htdocs/blog/app$ ls -la
drwxr-xr-x 12 mj     users 4096 2010-01-23 02:03 .
drwxr-xr-x  5 mj     users 4096 2010-01-23 02:28 ..
drwxr-xr-x  3 mj     users 4096 2010-01-12 05:58 config
drwxr-xr-x  3 mj     users 4096 2010-01-21 22:32 controllers
-rw-------  1 mj     users   67 2010-01-23 02:03 .directory
-rw-r--r--  1 mj     users  169 2010-01-23 02:08 .htaccess
-rw-r--r--  1 mj     users  859 2010-01-12 01:47 index.php
drwxr-xr-x  3 mj     users 4096 2010-01-12 01:47 locale
drwxr-xr-x  4 mj     users 4096 2010-01-19 06:54 models
drwxr-xr-x  2 mj     users 4096 2010-01-12 01:47 plugins
drwxr-xr-x  5 mj     users 4096 2010-01-12 01:47 tests
drwxr-xr-x  6 wwwrun www   4096 2010-01-12 01:47 tmp
drwxr-xr-x  3 mj     users 4096 2010-01-12 01:47 vendors
drwxr--r--  8 mj     users 4096 2010-01-19 07:00 views
drwxr-xr-x  5 mj     users 4096 2010-01-19 07:36 webroot

Never mind the fact that I'm using my own username for the directories. It's just for quick editing.
Focus on the bold line which has "views" -- since I didn't assign the user wwwrun for that directory, the permissions prevented Apache from accessing the directory resulting in the above Missing error message.

You can either change directory ownership as done for tmp, or change permissions:
chmod 755 views

Note: If a directory is not executable, you can't change to it.

4 comments:

Anonymous said...

Typically, it's more effective to set the group and ownership to your apache account. This prevents some of the issues you experienced. Personally I prefer wordpress for it's ease of installation.

MBH said...

The thing is, most of my projects involved home-cooked scripts; i.e., it's my first time using a framework ever since I started using PHP, and even though I knew that setting permissions & ownership is in fact the best choice, I didn't do it. On purpose.

My goal of setting up the blog tutorial isn't using it for a real blog. It was just my first step into the cooking class of CakePHP :)

I've started working on a certain project and quickly realized how complex and cumbersome it will get because the Model, View & Controller all mixed together. It was ugly!

vinnie said...

So you'll keep using it or change to a different framework?

MBH said...

I'm sticking to it as everything went smooth after the permission issue.

Modifying templates is a breeze. The same goes for the data & logic.