This article covers a version of Ubuntu that is no longer supported. If you are currently operate a server running Ubuntu 12.04, we highly recommend upgrading or migrating to a supported version of Ubuntu:
Reason: Ubuntu 12.04 reached end of life (EOL) on April 28, 2017 and no longer receives security patches or updates. This guide is no longer maintained.
This guide might still be useful as a reference, but may not work on other Ubuntu releases. If available, we strongly recommend using a guide written for the version of Ubuntu you are using. You can use the search functionality at the top of the page to find a more recent version.
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 organizing 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).
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/
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):
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:
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.
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:
If you see rewrite_module in the list, you are fine. If not, use the following command to enable the module:
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:
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
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', ' DYhG93b0qyJfIxfs2guVoUubWwvniR 2G0FgaC9mi'); /** * 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.
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:
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:
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('
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.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
Click here to Sign up and get $200 of credit to try our products over 60 days!