Magento on Heroku - Part 1

Heroku is an application platform that's widely used in the Ruby on Rails community. It's not as well known in the PHP world, but you can deploy and run PHP5 web applications on Heroku. It does take a bit more setup than Ruby/Python etc, but it's well worth the effort.

I've been using it recently to do some Magento development - here's the steps to get up and running with Magento on Heroku.

Firstly, you'll need to log into the Magento website, and download the latest (1.7.0 at time of writing) CE release. Extract this somewhere sensible, and create a git repository for it:

  tar xvf magento
  cd magento
  git init
  git add .
  git commit -m "Initial commit of Magento"

We then want to create a heroku application based on this directory. However, rather than do the standard heroku create, we need to use a PHP buildpack. This bundles Apache and PHP along with some config files - these should be useful later if we need to tweak our Magento install.

And finally, push our code to the Heroku git repository - this will deploy the app.

  heroku create --buildpack https://github.com/heroku/heroku-buildpack-php --region eu
  git push heroku master

Run heroku open, and you should see the Magento install page in your browser!

Magento Installer running on Heroku

Continue through the installer as normal. However, when you get the database settings, pause. Where is our database?

Normally, I'd recommend using Heroku's Postgres addons for database support. However, Magento doesn't support Postgres, so that's out. We'll need to use one of the MySQL addons available for Heroku. Your choices are between Amazon RDS and ClearDB. Currently, the ClearDB free tier has a limit of 5MB - this won't be big enough for Magento at present, so I'm using Amazon RDS.

You'll need to sign up to AWS, and provision a RDS instance. This takes a bit of time, but there's ample help along the way. Keep a note of the following information while setting up:

  • Availablilty Zone (e.g. eu-west-1a)
  • Endpoint (this should be a url ending in something like rds.amazonaws.com:3306)
  • Database name
  • Master username and password

Once this is done, we'll need to authorise Heroku to connect to our RDS instance. The quickest way to do this is to use the Amazon RDS Command Line Toolkit, which you can download from amazon here.

Once downloaded, unzip the tools into a sensible location (I'm using ~/libs/rdscli, change the instructions below as appropriate). Once done, we need to add some environment variables, and set up our credentials file.

In your .bashrc (or .zshrc, or similar), add the following ENV vars:

export JAVA_HOME=/usr/lib/jvm/java-6-sun
export EC2_REGION=eu-west-1
export AWS_RDS_HOME=~/libs/rdscli
export PATH=$PATH:$AWS_RDS_HOME/bin

You should also have a file named credential-file-path.template in your RDSCli directory. Edit this, and fill in your details, then rename it to creds.key You can find your API Key and Secret Key in the Security Credentials section of your AWS account.

You can now grant heroku access to your RDS instance, with the following command:

rds-authorize-db-security-group-ingress default \
--ec2-security-group-name default \
--ec2-security-group-owner-id 098166147350 \
--aws-credential-file ~/libs/rdscli/creds.key

You should now be able to fill in the database configuration section of the Magento install with your RDS endpoint, username, database name and password. As part of this step, there should be an option to "Skip Base URL Validations Before the Next Step". Make sure to check this option, as Magento doesn't validate heroku URLs for some reason.

You should be able to create an admin account, and complete the installation, and voila! A heroku-hosted instance of Magento awaits.

I'll go into more detail in later posts, once I've had a bit more time to develop on this platform. I'm not super convinced that it's a production-ready setup, especially in regards to scaling Magento horizontally, but for developers who want a decent development setup, and all the goodies that Heroku provides (excellent logging, and brilliant metrics with a free New Relic account), it's well worth a try.

Published 9 Nov 2022