01
Jan
2013
4 Comments

Setup a CakePHP app on Heroku

The goal is to get you from zero to this: cakephp-example.herokuapp.com

Step 1 create the app:

(source: https://devcenter.heroku.com/articles/cedar)
  • in the directory containing the CakePHP app run heroku create
    output: http://quiet-sky-6888.herokuapp.com/ | git@heroku.com:quiet-sky-6888.git
  • Add the repository as a remote git remote add heroku git@heroku.com:quiet-sky-6888.git
    *Make sure you use the repo in your output, not mine.

Step 2 add the database:

(source: https://devcenter.heroku.com/articles/heroku-postgresql)
  • Run heroku addons:add heroku-postgresql:dev
    output: Attached as HEROKU_POSTGRESQL_RED_URL
  • Run heroku pg:promote HEROKU_POSTGRESQL_RED_URL
    * Make sure you use the name from your output, not mine.
  • Get your new DB credentials heroku pg:credentials DATABASE
    output: dbname=dee932clc3mg8h host=ec2-123-73-145-214.compute-1.amazonaws.com port=6212 user=user3121 password=98kd8a9 sslmode=require

Step 3 setup environment vars:

Using the credentials from when you ran the last step, run:
  • heroku config:add DB_NAME=dee932clc3mg8h
  • heroku config:add DB_HOST=ec2-123-73-145-214.compute-1.amazonaws.com
  • heroku config:add DB_USER=user3121
  • heroku config:add DB_PASS=98kd8a9

While we are at it, setup security salt and cipher vars also:
  • heroku config:add SECURITY_SALT=ffdsdsgdfgdfgfdghgfdhfghfgdh Use a random string
  • heroku config:add CIPHER_SEED=32432847328472389473 Use random digits

Step 4 write some code:

Open up the file app/Config/core.php and edit the security salt and cypher string to:
Configure::write('Security.salt', getenv('SECURITY_SALT'));
Configure::write('Security.cipherSeed', getenv('CIPHER_SEED'));
Rename the file app/Config/database.php.default to app/Config/database.php
Open up the new file app/Config/database.php and edit the database config to:
class DATABASE_CONFIG {

  public $default;

  function __construct() {
    $this->default = array(
      'datasource' => 'Database/Postgres',
      'persistent' => false,
      'host'       => getenv('DB_HOST'),
      'login'      => getenv('DB_USER'),
      'password'   => getenv('DB_PASS'),
      'database'   => getenv('DB_NAME'),
      'prefix'     => '',
      'encoding'   => 'utf8',
    );
  }
}
You have to set the $default array in the __construct function instead of the usual way in order to be able to use the getenv methods.

You will need to force add app/Config/database.php to the repo since it is ignored:
  • Run git add app/Config/database.php -f
It is set to ignore to prevent your credentials from showing up in the repo for everyone to see, but we aren't storing them here, we are storing them in ENV vars, so it is ok to add this to the repo (and you have to so you can push it up to heroku)

Once you have all these changes done and commited push them to heroku
  • Run git push heroku master
  • Now visit your site: heroku open

You should be greeted with a vanilla CakePHP app with all green statuses telling you that Cake is able to connect to the database. Your page should look like the example in the top of this article. You are all setup for Heroku, now all you need to do is build your app.

If you have problems with step 4 you can fork my repo with that part already setup for you.

4 Comments

1277415d415a13a891f8c171893b2635?s=40
Sun May 19 02:35:41 2013
Are there any issues with CakePHP's default logging and caching mechanism writing to local files? I found this to be an issue with AppFog.
F31c6e48dc5f0f2756e748bf4ec94cc2?s=40
Tue Jan 15 08:02:07 2013
You are the awesomest!!! This article helped me out so much. You get the props!

Sincerely,
Karl Oscar Weber
1de5108f00252c3c443fdb98305e02e9?s=40
Fri Jan 18 11:05:36 2013
This is alll fine to create Heroku app but one can even make use of Heroku Postgres if preferring PostgreSQL into a database.
Cdbc6dd406157767336b9ba9d6b1d864?s=40
TryFailRepeat
Wed Feb 27 00:14:29 2013
Thanks for this great Tutorial!
I guess you need to tell git to add the whole config to your reposity not just the 
database.php to make it work.
use git add app/Config -f

Leave A Comment