Andy Regan Ramblings of an Irish Sysadmin

16Feb/100

Setting Up A New CakePHP Environment in Ubuntu

CakePHP Logo

CakePHP is a useful MVC framework for PHP applications. However, there are a few common gotchas when setting up a new development environment. In this post, I will go through how to get around these issues when setting up a new environment on a LAMP (Linux Apache MySQL PHP) server, in this case server running an Ubuntu OS. Please leave a comment if you have any feedback or can recommend any improvements on the suggestions here.

Grab a Copy and Get Started

The first thing you'll need to do is get a local LAMP server up and running. Then, grab a fresh copy of CakePHP from cakephp.org. This site also has some useful resources if you'd like to learn more about the framework.

$wget http://download.github.com/cakephp-cakephp1x-ef18ab2.tar.gz

Once you have downloaded the tarball, extract it and then rename the new directory to something more useful. This is the root directory of the application.

$tar -xvvzf cakephp-cakephp1x-ef18ab2.tar.gz
$mv cakephp-cakephp1x-ef18ab2/ cake_project/

Make Tmp/ Writable

Next, you need to make sure that the tmp/ directory is writeable. To avoid using insecure 777 permissions, I recommend installing suPHP. SuPHP allows PHP scripts to be run as their owner rather than as the default Apache user (www-data). As a result, you can use more secure file and directory permissions while still allowing the tmp/ directory to be writeable by the application. Generally, 755 permissions are sufficiently secure for directories and 644 for files.

Install the correct packages, enable suphp and disable the php5 module.
$sudo apt-get install libapache2-mod-suphp suphp-common
$sudo a2enmod suphp;sudo a2dismod php5;sudo /etc/init.d/apache2 restart

Add A Dash of Salt

With that out of the way, now you need to change the default salt value for the application. CakePHP needs a random string ("salt") which is used to generate secure hashes. I find the pwgen utility in Linux very useful for quickly generating a randomĀ  string.

Open up app/config/core.php. Then, replace the random_string value below with the output of the pwgen command .

Configure::write('Security.salt', 'random_string');

$pwgen -y 100

Point Your Domain at the Cake Directory

You can use Apache's userdir module to test your application. This will allow you to go to http://yourdomain.com/~username in your browser where /home/username/public_html is the document root. However, this often conflicts with the Apache rewrite module which is necessary for "friendly" URLs.

To get around this conflict on a local development environment, I recommend pointing your domain at the application's root directory. In this way, when you go to the domain in your browser it will show you the newly installed CakePHP application.

First, add a rule to your computer's /etc/hosts file so that the domain points to localhost.
#add a similar rule to /etc/hosts
127.0.0.1 caketest.com www.caketest.com

Then, add and enable a vhost for the domain you are working on.

$sudo vim /etc/apache2/sites-available/caketest.com

<VirtualHost *:80 >
#Basic setup
ServerAdmin webmaster@mydomain.com
ServerName caketest.com
ServerAlias www.caketest.com
DocumentRoot /home/andrew/public_html/cake

ErrorLog /home/andrew/public_html/cake/error_log/error.log

<Directory /home/andrew/public_html/cake>
Order Deny,Allow
Allow from all
# Don't show indexes for directories
Options -Indexes
</Directory>
</VirtualHost>

$sudo a2ensite caketest.com;/etc/init.d/apache2 restart

Why is Everything Black and White?

If the new Cake installation displays without any CSS markup, make sure that the Apache rewrite module is enabled. After that, your done.

$sudo a2enmod rewrite;/etc/init.d/apache2 restart

At this point, you might want to check out the CakePHP documentation which will give you more information on what to do from here. You should also check out this article on using Netbeans as your developemnt IDE for CakePHP projects.

Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


No trackbacks yet.