Cakephp on Site5 subdomains

I have developed different web applications using CakePHP and I’m liking the framework so far. Especially it’s ability to automatically generate (or better bake) CRUD operations for every defined model. Recently I was asked to develop two different applications that would live on two subdomains of the same site5 hosted domain.

As I had little experience with site5 hosting at the time, I went on to search the web (as well as their user forums) for guides on working with CakePHP. As strange as it may seem, different users were complaining that they were not able to install CakePHP on a site5 subdomain.

I can only assume here, but it may be related with the default cPanel way to add a subdomain which is to just add a redirect to ~/public_html/subdomain for http://subdomain.mydomain.com, which is the same thing as http://mydomain.com/subdomain. Now, this is not the way I configure my subdomains, I hate cluttering of the public_html directory, so I did not try this at all. Instead I create a webdir directory on my home and host all my add-on domains and subdomains under that directory, explicitly telling cPanel where the root location of every subdomain is.

The Fast-track

  • On Site5 cPanel:

    • create subdomain
    • create DB for cakephp (remember user/pass)
  • On Site5 shell

    • Checkout CakePHP from https://trac.cakephp.org/
    • Move app directory to webdir/cake
    • Move .htaccess file to webdir/cake
    • fix webroot/index.php so it knows where cake and your app directories are located

Step-by-step guide

First things first, log in to cPanel for your site5 account and go to Subdomains. Create a subdomain:

Subdomain: cake
Directory Root: web/cake

This will create a new directory ~/web/cake on your site5 home and configure the Apache web server to serve files from this directory for every http://cake.yourdomain.com request. Keep in mind that ~/web/cake will be the working directory for your CakePHP application.

The next step is to create a database (MySQL in this case) for CakePHP and configure a user for that database. Return to the “Home” view of your cPanel administration interface and then go to “MySQL database wizard”. Enter

New Database: cakedb

You can also take a look at my screenshot for cPanel DB name selection and then click Next. On the next screen enter

Username: cakeusr
Password: cakepass
Password (Again): cakepass

Again there is a screenshot for cPanel DB username/password selection.

Before clicking “Next Step” remember that this is a guide, you should replace the default values that I am providing with proper username and password values. Choose a strong password, or use the “Generate Password” button to generate a strong password. This is crucial to the security of your web app! After having considered this, click on “Next Step”.

On the following screen select all that apply or, if you want to give cakeusr full access to the database click on “ALL PRIVILEGES”. Here is another screenshot for cPanel DB privileges selection, where I have given all privileges to the database user. When you are done click on ‘Next Step’.

Ta-Da! We’re done with the cPanel, you will see a screen like:

cakephp_cpanel_ok.jpeg

Now log in to the site5 console via your SSH client of choice. I simply use the terminal on Unix like systems, but if you are using Windows, PuTTY seems to be the preferred choice.

Once you are logged in your account you should get a copy of CakePHP. I tend to check out a copy from their SVN repository, because it allows simple updating. If you prefer to just download CakePHP, you can get a copy from the CakePHP website.

For the subversion checkout:

mkdir -p ~/lib/php/ #  create a directory for php libraries
cd ~/lib/php/
svn co https://svn.cakephp.org/repo/trunk/cake/1.2.x.x cake

With this we are creating a a directory for php libraries where we are also storing the CakePHP framework checked out from the subversion repository. If you downloaded the zip distribution, unzip it and store it on ~/lib/php/ to follow with this guide.

A CakePHP application is served from the app directory, so we need to put that particular directory (and the .htaccess file) under the webroot of our subdomain (~/web/cake) so it can be served by Apache.

mv ~/lib/php/cake/app ~/web/cake/
mv ~/lib/php/cake/.htaccess ~/web/cake/

Now, all we need to do is tell the CakePHP application where the cake distribution is. To do that we have to edit the ~/web/cake/app/webroot/index.php file.

cd ~/web/cake/app/webroot
vim index.php

Here is my index.php file, don’t forget to change the 3rd line:

<?php
//TODO: change the following to your Site5 user
$site5user = 'YOUR_SITE5_USERNAME';

/**
 * Use the DS to separate the directories in other defines
 */
	if (!defined('DS')) {
		define('DS', DIRECTORY_SEPARATOR);
	}
/**
 * These defines should only be edited if you have cake installed in
 * a directory layout other than the way it is distributed.
 * When using custom settings be sure to use the DS and do not add a trailing DS.
 */

/**
 * The full path to the directory which holds "app", WITHOUT a trailing DS.
 */
	if (!defined('ROOT')) {
		define('ROOT', DS.'home'.DS.$site5user.DS.'web'.DS.'cake');
	}
/**
 * The actual directory name for the "app".
 */
	if (!defined('APP_DIR')) {
		define('APP_DIR', 'app');
	}
/**
 * The absolute path to the "cake" directory, WITHOUT a trailing DS.
 */
	if (!defined('CAKE_CORE_INCLUDE_PATH')) {
		define('CAKE_CORE_INCLUDE_PATH', DS.'home'.DS.$site5user.DS.'lib'.DS.'php'.DS.'cake');
	}

/**
 * Editing below this line should NOT be necessary.
 * Change at your own risk.
 */
	if (!defined('WEBROOT_DIR')) {
		define('WEBROOT_DIR', basename(dirname(__FILE__)));
	}
	if (!defined('WWW_ROOT')) {
		define('WWW_ROOT', dirname(__FILE__) . DS);
	}
	if (!defined('CORE_PATH')) {
		if (function_exists('ini_set') && ini_set('include_path', CAKE_CORE_INCLUDE_PATH . PATH_SEPARATOR . ROOT . DS . APP_DIR . DS . PATH_SEPARATOR . ini_get('include_path'))) {
			define('APP_PATH', null);
			define('CORE_PATH', null);
		} else {
			define('APP_PATH', ROOT . DS . APP_DIR . DS);
			define('CORE_PATH', CAKE_CORE_INCLUDE_PATH . DS);
		}
	}
	if (!include(CORE_PATH . 'cake' . DS . 'bootstrap.php')) {
		trigger_error("CakePHP core could not be found.  Check the value of CAKE_CORE_INCLUDE_PATH in APP/webroot/index.php.  It should point to the directory containing your " . DS . "cake core directory and your " . DS . "vendors root directory.", E_USER_ERROR);
	}
	if (isset($_GET['url']) && $_GET['url'] === 'favicon.ico') {
		return;
	} else {
		$Dispatcher = new Dispatcher();
		$Dispatcher->dispatch($url);
	}
	if (Configure::read() > 0) {
		echo "<!-- " . round(getMicrotime() - $TIME_START, 4) . "s -->";
	}
?>

We are almost done. Now all there is left to do is add your username/password for the database and of course change the security salt, but you should know how to do these if you’ve already developed with CakePHP.

No Comments

Leave a Reply

Your email is never shared.Required fields are marked *