Running Wordpress on Heroku

May 26 2012

Heroku is a great hosting platform. It takes most of the pains of deployment, sysadmining, and scaling out of a basic website or service. Wordpress is a publishing engine that powers a large precentage of the web. It's community is great, with lot of development going into the plugins. Now how do we use these together?

Disclaimer Now I havent been running Wordpress for my site for a while as my requirements have changed. I'm now running Jekyll. However I still help some friends with their Wordpress installs.

First lets grab the latest stable Wordpress. Either download the tarball or there is a maintained git repo that is update every half hour on github that we can clone.

git clone https://github.com/WordPress/WordPress.git

checkout the latest version tag, as of this it's 3.4

git checkout 3.4

Just like normal with a wordpress install we'll copy the sample wp-config-sample.php to wp-config.php

mv wp-config-sample.php wp-config.php

We need some database settings so we finish our config, so lets start setting up heroku. We should create a new app on heroku.

heroku create --stack=cedar

We need a database. Wordpress depends on mysql, although there is a plugin to add postgresql support I fear that many plugins would be broken by using postgresql. ClearDB is a hosted mysql addon for heroku. For a small site you can even run in their free service level.

heroku addons:add cleardb:ignite

This will add a DB connection string to your environment variables named: CLEARDB_DATABASE_URL In order to put this to use we'll change some of the code in wp-config.php. First we need to parse the connection string.

$clearDB_url = parse_url($_ENV["CLEARDB_DATABASE_URL"]);

After we parse the connection use that to set the DB vars that wordpress expects

define('DB_NAME', ltrim($clearDB_url["path"], "/"));
define('DB_USER',  $clearDB_url["user"]);
define('DB_PASSWORD',  $clearDB_url["pass"]);
define('DB_HOST',  $clearDB_url["host"]);

Commit those changes to the wp-config.php and now lets push to Heroku.

git add .
git commit -m "read connection string from ENV"
git push heroku master

Once deployed you should be able to open your heroku site.

heroku open

Go to /wp-admin and you should be able to finish the setup of the database.

Now you have wordpress setup on Heroku!