Migrating a Legacy PHP Application To Heroku
Wow, that was easy! -Me
Last time I looked at doing PHP on Heroku it was pretty scary and involved creating custom buildpacks.
Migrating from subversion to git. git2svn makes this really easy. Creating an app on Heroku and pushing to it is the usual easy
heroku create and
git push heroku master, and hooray, I was done! Not quite…
My old code used PHP’s old built-in support for MySQL (
mysql_connect) which has been replaced with the shiny new MySQL extension (
mysqli_connect). Getting rid of the first errors in the log that prevented the app from building meant switching to
mysqli_* functions everywhere, and passing around the
$link to objects/methods that needed it. Had my app been ‘modern’ PHP code, this wouldn’t have been needed.
For actually hosting the database, Heroku has an addon for MySQL: ClearDB. It’s free for 5MB of data which is plenty for this app. This was as simple as
heroku addons:add cleardb. I added a little bit of logic to pull in the CLEARDB_DATABASE_URL:
$url = parse_url(getenv("CLEARDB_DATABASE_URL")); $server = $url["host"]; $user = $url["user"]; $pass = $url["pass"]; $db = substr($url["path"], 1); $link = mysqli_connect($server, $user, $pass, $db) or die(mysqli_error());
And was in business. Getting the data loaded in was as easy as grabbing a snapshot from my old production server and running something along the lines of
cat dump.sql | mysql -u$USER -p $HOST $DB and everything worked great. I did have an old ‘loader’ script that relied on MySQL’s “LOAD INFILE”, but this doesn’t work with ClearDB.
The Good And The Bad
The good news is that I don’t need this app co-habitating space with my (now static) homepage. It’s one less thing to worry about. The bad is that Heroku will idle out my process when it’s not hit in a while so the first time someone visits will be awfully slow sometimes. I’m the only target user, so I think I’ll be alright. For playing with PHP apps in the future, I’ll definitely consider using Heroku and so should you.