Easily build a great vocabulary without studying! Now on the App Store!
Click here for more info about the app

Deploying Wordpress on Heroku


This post is out of date - Heroku officially supports PHP now w/ buildpacks

Heroku runs

Apache version 2.2.22
PHP version 5.3.10

Deploying Wordpress to Heroku requires that you throw the Wordpress source into a Git repository. So download the latest stable build and commit it.

wget http://wordpress.org/latest.zip
unzip latest.zip
cd wordpress
git init
git add .
git commit -m'init'

If you already have Heroku's command line tool installed, great! If not you can download their toolset from here https://toolbelt.heroku.com/

Once installed, initailize an application on the Heroku platform.

heroku create --stack cedar

Wordpress (as far as I know) doesn't support Postgres, which is the default database on Heroku. However, MySQL is available as a free addon (5MB of space).

The plugin is called cleardb http://www.cleardb.com/. Cleardb is a MySQL-as-a-service company. You'll have to tell Heroku that you want to use it.

heroku addons:add cleardb

**You may have to have a verified account to do this, which means you need to enter your credit card number under your account settings. I can't remember if this addon requires it. But so long as you stay on the cleardb free plan you won't be charged.

Wordpress has a great self install tool, but we're going to skip that. We need the config checked into Git so we don't want Wordpress to do it for us on the deployed site. We can do it locally and check it in. Rename the wp-config-sample.php to wp-config.php.

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

Replace the placeholder database configuration credentials with the connection details Heroku and Cleardb provide. To get this information run

heroku config

This will return the cleardb database connection url, which looks something like

CLEARDB_DATABASE_URL => mysql://[username]:[password]@[host]/[database name]?reconnect=true

Extract those credentials and plug them into wp-config.php where it says

/** The name of the database for WordPress */
define('DB_NAME', 'database_name_here');

/** MySQL database username */
define('DB_USER', 'username_here');

/** MySQL database password */
define('DB_PASSWORD', 'password_here');

/** MySQL hostname */
define('DB_HOST', 'localhost');

Commit the changes and deploy!

git add . 
git commit -m'database details'
git push heroku master

When you visit the site, Wordpress will walk you through titling and adding an admin username and password. To get the URL of the site run

heroku info

By default cleardb let's you connect from anywhere. So if you're running your local instance of Wordpress out of a web enabled directory (Mamp, Wamp, ~/Sites etc.) your content will be production data.

Now you can edit your theme or edit plugins and deploy by pushing the branch to Heroku!

git push heroku master

You can also push other branches (not yet merged with master)

git push heroku other_branch:master

For more information about Heroku visit their dev center https://devcenter.heroku.com/

A Few Other Things To Think About

Heroku doesn't have an MTA (Mail Transfer Agent) so your blog won't be sending any email. There are a number of plugins for Wordpress that let you configure SMTP, such as http://wordpress.org/extend/plugins/wp-mail-smtp/, if you need it.

To get nice URLS/Permalinks you should work on them in development and check in the auto generated .htaccess file. Because Heroku deploys to Amazon's EC2, you're not guaranteed the same instance (filesystem) when you redeploy or restart the application.

heroku restart

And for the same reason above (you shouldn't write to the filesystem) you will have to host assets not bundled with the source code elsewhere, for instance on Amazon S3. There are quite a few plugins available, like http://wordpress.org/extend/plugins/tantan-s3/. Theme assets are fine, just assets that you would upload to attach to a blog post.

Tagged w/ #heroku #wordpress #wp #php #deploy #configuration #.htaccess #apache