Sean’s Obsessions

Sean Walberg’s blog

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:

1
2
3
4
5
6
7
8
  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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/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.

Comments

I’m trying something new here. Talk to me on Twitter with the button above, please.