August 19, 2013

Beginner

How To Install CakePHP On An Ubuntu 12.04 VPS

About CakePHP

CakePHP is a powerful and robust PHP framework built around the Model-View-Controller (MVC) programming paradigm. In addition to the flexible way you can use it to build your application, it provides a basic structure for organising files, classes and database table names - keeping everything consistent and logical.

The minimum requirements for CakePHP are a webserver and PHP 5.2.8 or greater. Although it’s most commonly run on Apache, CakePHP also works with other servers such as Lighttpd or Microsoft IIS. Additionally, since databases are part of most web applications, CakePHP supports a number of drivers such as MySQL, PostgreSQL, Microsoft SQL Server or SQLite (all with their respective PDO extensions installed).

In this tutorial you will learn how to install and get started with CakePHP. For this it assumes you are already running your own cloud server instance and a web server. This tutorial will use Ubuntu for the operating system and Apache for the webserver (+ PHP and MySQL).

Installing CakePHP

There are a few ways you can install CakePHP on your server. You can download and unpack the archive from the CakePHP website, you can use Git to clone a release from Github or you can even use PEAR. We will use the first one as it is the most straightforward.

So what we need to do is download the latest stable release (link to which we can find on the CakePHP website home page) and unzip it into a folder. So let’s navigate to our web server’s root folder and download the archive:
cd /var/www
wget https://github.com/cakephp/cakephp/zipball/2.3.9

This will download a .zip file with the release version as the name. Now we need to unzip this file. If you don’t have unzip installed, just run the following command to quickly install it:
sudo apt-get install unzip

Now you’ll be able to unzip the archive (just make sure you replace the name of the .zip file with the one you have just downloaded):
unzip 2.3.9

You should get a new folder that contains all the CakePHP files. You can go ahead and rename this folder to something more useful, let’s say project:
mv cakephp-cakephp-4b9e390 project

Make sure again that you replace the name of the folder with the one you got after unziping the archive. And this command will rename it to project. So you currently have CakePHP found in the /var/www/project folder. You can find more information about the files and folder structure you’ll see in there.

Next up, let’s change the permissions of the app/tmp folder of your application as CakePHP will need to use it quite a bit so it needs to be writable by the webserver. For Apache, run the following commands from within the /var/www/project folder:
cd project
chown -R root:www-data app/tmp
chmod -R 775 app/tmp

This will change the ownership of the folder and everything inside it to the root user and www-data group (which contains the www-data user that Apache uses to run its processes). The second command then sets the folder permissions in a way in which the www-data group can write in it.

URL rewriting

Let’s make sure that our webserver allows url rewriting as CakePHP works very nicely with clean URLs. This means that the Apache module mod_rewrite needs to be enabled and that the virtual host allows the .htaccess files to do their overriding. So first check if mod_rewrite is enabled on your system with this command:
apache2ctl -M

If you see rewrite_module in the list, you are fine. If not, use the following command to enable the module:
a2enmod rewrite

Next, if this is not the case for you, edit the Apache default virtual host file and make sure that Allow Overrides is set to All for the /var/www directory. Edit the file with the following command:
nano /etc/apache2/sites-available/default

And where you see this block, make the changes to correspond to the following:
		Options Indexes FollowSymLinks MultiViews
		AllowOverride All
		Order allow,deny
		allow from all

This will make sure that the .htaccess files can override the default Apache instructions. After any of these steps, make sure you restart Apache for the changes to take effect:
sudo service apache2 restart

Additional configuration

Now that we have URL rewriting functional, let’s take care of some security related configuration CakePHP offers, namely the "salt" and the "seed". The first one is a custom string used by CakePHP in security hashes whereas the second one is a custom number used for encryption. Let’s set them now so we don’t have to worry about them later. Plus, the CakePHP application home page will show you 2 big red notices if we don't.

Edit the core.php file located in the app/Config folder and find the following block:
/**
 * A random string used in security hashing methods.
 */
	Configure::write('Security.salt', 'DYhG93b0qyJfIxfs2guVoUubWwvniR2G0FgaC9mi');

/**
 * A random numeric string (digits only) used to encrypt/decrypt strings.
 */
	Configure::write('Security.cipherSeed', '76859309657453542496749683645');

The actual strings and numbers may differ in your case, but this is where you’ll have to change the values into something impossible to guess. Just make sure you only include numbers for the cipherSeed.

Save the file and exit.

Database connection

Although a database is not necessary for CakePHP, most web applications need one to store information. So let’s set up a connection to a MySQL database that we will now create. We’re not going into the details of how to create the database itself so please consult this tutorial for more information about how to work with MySQL. Or you can check out this tutorial that will guide you to migrating to MariaDB if you'd like.

So for the purpose of the next tutorial in which we will create a small example application, let’s create already now a database with some content and make CakePHP aware of it. We’ll need a database with one table in it that has 5 columns (id, title, body, created, modified). The first should be an auto incrementing integer as the primary key, the second a VARCHAR(500), the third a TEXT and the last 2 DATETIME. You can also go ahead and create a couple of rows in this table.

For the purpose of the tutorial, we will name the database "cake" and the table "posts". The naming of the table is actually quite important in CakePHP because adhering to its convention will bring you great benefits. More about this when we look at creating Models and Controllers.

So with a couple of commands we can do all this.

Log into MySQL or MariaDB.

From the console you can create the database:
create database cake;

Then you can create inside this database the table with the columns I mentioned. But first, connect to the "cake" database:
use cake;

And now you can create the table:
CREATE TABLE posts (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    title VARCHAR(50),
    body TEXT,
    created DATETIME DEFAULT NULL,
    modified DATETIME DEFAULT NULL
);

And then insert some content in it too very easily:
INSERT INTO posts (title,body,created)
    VALUES ('Title 1', 'Some body text.', NOW());

This will add a new row to the "posts" table.

Next, let’s go ahead and configure CakePHP to use this database. First thing you need to do is make a copy of the database.php.default file located in the /app/Config/ folder and name it database.php. Then open the file and locate the following block of code (change cakeBlog and c4k3-rUl3Z to your mysql login and password) :
public $default = array(
    'datasource' => 'Database/Mysql',
    'persistent' => false,
    'host' => 'localhost',
    'port' => '',
    'login' => 'cakeBlog',
    'password' => 'c4k3-rUl3Z',
    'database' => 'cake',
    'schema' => '',
    'prefix' => '',
    'encoding' => 'utf8'
);

All you have to do now is provide the database connection information specific to your case and you are good to go. Save the file and exit. Now you can navigate in your browser and point to the project folder (or however you chose to name the folder in which you placed CakePHP) in your webserver’s document root:
http://www.example.com/project

You should see the home page of your new CakePHP application and hopefully after the configuration you’ve made earlier, all the notices are in green (apart from the one about DebugKit which is an external and optional library).

If however you are getting a timezone related error from PHP, go ahead and uncomment the following line in the same core.php file located in the app/Config folder:
/**
 * Uncomment this line and correct your server timezone to fix
 * any date & time related errors.
 */
    date_default_timezone_set('UTC');

Conclusion

We’ve seen in this tutorial how to install and set up CakePHP on your cloud server. In the following article we will play a bit with CakePHP to create a simple application that will interact with the database we configured earlier.
Article Submitted by: Danny

Share this Tutorial

Vote on Hacker News

Try this tutorial on an SSD cloud server.

Includes 512MB RAM, 20GB SSD Disk, and 1TB Transfer for $5/mo! Learn more

Create an account or login:

5 Comments

Write Tutorial
  • Gravatar Arcadio Quintero 8 months

    Excelent

  • Gravatar kensully 7 months

    Great tutorial!! Many thanks for it, you explained it very well. I managed to run it on Ubuntu 13.04. Do you know tatoeba.org? It's an online multilingual sentence aligner server. They also share language database. I want to make an offline version of it but I don't know how to do it. I'm just a beginner. I found two sources on the net but they are not very well explained or I don't have enough knowledge to master it. Here the sources: Tatoeba shared files http://tatoeba.org/app/webroot/files/downloads/ A tutorial about how to setup offline server http://lilyx.net/2011/07/21/extracting-multilingual-parallel-senteces-from-tatoeba-com/ Another CakePHP tatoeba installation guide http://en.wiki.tatoeba.org/articles/show/install-tatoeba-php But CakePHP they used is old, so it gives me some errors. "cat" commands in the link are not completely visible but I found these: cat links.csv | while read -r id1 id2; do echo "insert into sentences_translations (sentence_id, translation_id) values ($id1, $id2);"; done | mysql -u root -p 'tatoeba' cat sentences.csv | while read -r id lang text; do echo "insert into sentences (id, lang, text) values ('$id', '$lang', '${text//\'/\'}');"; done | mysql -u root -p 'tatoeba' The former one worked ok but latter gave me an error in the end. Would you please help me about how to setup offline version of tatoeba.org in my computer?

  • Gravatar crlsmtzprds 5 months

    Nice work man, this post help me so much!

  • Gravatar nasir.prz 3 months

    Ecellent Work!

  • Gravatar ankurg2020 30 days

    Great explanation...Thanks a lot..

Leave a Comment

Create an account or login:
Ajax-loader