Sean’s Obsessions

  • Archives

05 Nov

Pushing a CakePHP app from dev to prod

After more work on a CakePHP app I wanted to host it somewhere. Because this is a fairly low volume app I went with a shared hosting provider.

One problem was that I needed two different database connections, one for my development site, and one when I push it to the server. It was fairly easy to set up two different connections in app/config/database.php:

var $default = array('driver' => 'mysql',
'connect' => 'mysql_connect',
'host' => 'localhost',
....
var $production = array('driver' => 'mysql',
'connect' => 'mysql_connect',
'host' => 'PRODUCTION_HOST',
.....

Then within cake/app_model.php I did a check within the constructor:


class AppModel extends Model{
function __construct($id = false, $table = null, $ds = null) {
if ($this->isProd()) {
$this->useDbConfig = 'production';
} else {
$this->useDbConfig = 'default';
}
parent::__construct();
}
function isProd() {
$server = $_SERVER["HTTP_HOST"];
if ($server == "DEVELOPMENT_SERVER") {
return false;
} else {
return true;
}
}
}

The next order of business was to script pushing code.


#!/bin/sh
#
# FTP information
FTPHOST=xxxx.com
FTPUSER="xxxx"
FTPPASS="xxxx"
# do we cd to a dir after logging in?
FTPDIR=.
# path to base of svn
SVNDIR="http://svnserver/svn/project"
TMPDIR=/tmp/$$
#
if [ "x$1" == "x" -o "$1" == "trunk" ]; then
SVNURL="${SVNDIR}/trunk/"
else
SVNURL="${SVNDIR}/tags/$1/"
fi
#
echo $SVNURL
mkdir -p $TMPDIR
#
(cd $TMPDIR && svn export $SVNURL code )
#
(cat - < cd $FTPDIR
lcd ${TMPDIR}/code
put .htaccess
mput -R *
chmod 777 app/tmp
mkdir app/tmp/cache
chmod 777 app/tmp/cache
mkdir app/tmp/logs
chmod 777 app/tmp/logs
mkdir app/tmp/sessions
chmod 777 app/tmp/sessions
SCRIPT
) | ncftp -u $FTPUSER -p $FTPPASS $FTPHOST
#
rm -rf $TMPDIR

This pulls the current code out of subversion (either trunk or a tag) and uploads it to the server. ncftp is pretty good at only uploading changed files.

In the end I used 1 and 1 (aff) and their $4/mo beginner plan that came with a free domain name. There was one problem with 500 errors with 1and1 that was fixed by changing the .htaccess files (no problems on the dev side either)

The only thing I need to change is to move the isProd() function somewhere that is accessible in the configuration class so that errorlevels are changed depending on the environment. This could also be taken care of in the svn branch.

5 Responses to “Pushing a CakePHP app from dev to prod”

  1. 1
    Steven Says:

    Hi Sean,

    Thanks for your link to this article, I have placed it in the FAQ and will make a notice in our weblog.

    Regards,

    Steven

  2. 2
    CakePHP Tips » Sean’s Obsessions on pushing your CakePHP app to production Says:

    […] out the script and his article on his […]

  3. 3
    Matt Curry Says:

    I have a similar script to move code to production, using rsync instead of ncftp.

    Instead of setting debug at runtime you can put a line in the build script that always forces debug to 0:
    perl -pi -e “s/debug’, 2/debug’, 0/g” ${LOCAL_TEMP_DIR}/${TAG}/app/config/core.php

    Your path would obviously be different.

  4. 4
    CakePHP Tutorials :: PseudoCoder.com Says:

    […] ertw.com - Pushing a CakePHP app from dev to prod […]

  5. 5
    Adam Royle Says:

    Instead of adding mess to your AppModel, just change your database class:

    class DATABASE_CONFIG {

    var $development = array(
    ‘driver’ => ‘mysql’,
    ‘persistent’ => false,
    ‘host’ => ”,
    ‘login’ => ”,
    ‘password’ => ”,
    ‘database’ => ”,
    ‘prefix’ => ”
    );

    var $production = array(
    ‘driver’ => ‘mysql’,
    ‘persistent’ => false,
    ‘host’ => ”,
    ‘login’ => ”,
    ‘password’ => ”,
    ‘database’ => ”,
    ‘prefix’ => ”
    );

    var $test = array(
    ‘driver’ => ‘mysql’,
    ‘persistent’ => false,
    ‘host’ => ”,
    ‘login’ => ”,
    ‘password’ => ”,
    ‘database’ => ”,
    ‘prefix’ => ”
    );

    var $default = array();

    function __construct()
    {
    $this->default = (env(’SERVER_ADDR’) == ‘127.0.0.1′ || !env(’SERVER_ADDR’)) ? $this->development : $this->production;
    }

    function DATABASE_CONFIG()
    {
    $this->__construct();
    }
    }

Leave a Reply

Show some love for the magnitudinous, extraordinary, voluminous, and intense Wordpress Hash-cash?

© 2008 Sean’s Obsessions | Entries (RSS) and Comments (RSS)

Powered by Wordpress, design by Web4Sudoku, based on Pinkline byGPS Gazette