heroku

Deploying WordPress in Heroku

Download latest version of wordpress.

$ wget http://wordpress.org/latest.zip
$ unzip latest.zip
$ mv wordpress mywebsite # optional if you want to rename the folder.

Create “Procfile” and put this following line.

web: vendor/bin/heroku-php-apache2

Create “composer.json and put this following line.

{"require": {"php": "5.6.27"}}

Configure the wordpress by creating “wp-config.php”. WordPress already had the template, so you just need to rename it.

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

Edit “wp-config.php” and replace following line.

// ** MySQL settings - You can get this info from your web host ** //
$url = parse_url(getenv('DATABASE_URL') ? getenv('DATABASE_URL') : getenv('CLEARDB_DATABASE_URL'));

/** The name of the database for WordPress */
define('DB_NAME', trim($url['path'], '/'));

/** MySQL database username */
define('DB_USER', $url['user']);

/** MySQL database password */
define('DB_PASSWORD', $url['pass']);

/** MySQL hostname */
define('DB_HOST', $url['host']);

/** Database Charset to use in creating database tables. */
define('DB_CHARSET', 'utf8');

/** The Database Collate type. Don't change this if in doubt. */
define('DB_COLLATE', '');

define('AUTH_KEY',         getenv('AUTH_KEY'));
define('SECURE_AUTH_KEY',  getenv('SECURE_AUTH_KEY'));
define('LOGGED_IN_KEY',    getenv('LOGGED_IN_KEY'));
define('NONCE_KEY',        getenv('NONCE_KEY'));
define('AUTH_SALT',        getenv('AUTH_SALT'));
define('SECURE_AUTH_SALT', getenv('SECURE_AUTH_SALT'));
define('LOGGED_IN_SALT',   getenv('LOGGED_IN_SALT'));
define('NONCE_SALT',       getenv('NONCE_SALT'));

Now is the deployment process, ensure you have installed heroku toolbelt and already sign in on it.

$ git init
$ git add -A
$ git commit -m "basic setup wordpress heroku"
$ heroku create
$ heroku addons:add cleardb

After that, put following environment variables with new values. You can generate those in here.

heroku config:set AUTH_KEY=''
heroku config:set SECURE_AUTH_KEY=''
heroku config:set LOGGED_IN_KEY=''
heroku config:set NONCE_KEY=''
heroku config:set AUTH_SALT=''
heroku config:set SECURE_AUTH_SALT=''
heroku config:set LOGGED_IN_SALT=''
heroku config:set NONCE_SALT=''

Now we need download all the dependencies, ensure you have composer installed. If you are not, you can install following this.

$ curl -sS https://getcomposer.org/installer | php
$ php composer.phar install

Last step is deployment.

$ git add composer.lock
$ git commit -m "final step"
$ git push heroku master

Reference: https://ksylvest.com/posts/2014-05-02/deploying-wordpress-to-heroku

Dump PostgreSQL on Heroku and Download to Local

Here is short snippet how to create dump postgresql database on heroku and download it to your local computer.
Within your root project directory, run these heroku commands.

$ heroku pg:backups capture          # Dump remote database.
$ heroku pg:backups public-url       # Get download url for your dumped database.
$ curl -o latest.dump <download_url> # Download your database.
# Restore database to local database.
$ pg_restore --verbose --clean --no-acl --no-owner -h localhost -U myuser -d mydb latest.dump

Reference: https://devcenter.heroku.com/articles/heroku-postgres-import-export

Import MySQL Local Database to ClearDB

I’m using mysql for some of wordpress project but the project need to deploy on heroku. As you know heroku has no addons for mysql, for the alternative heroku using ClearDB (MySQL cloud service).

Here is how to import your local database to cleardb.

mysql --host=<cleardb_host> --user=<cleardb_user> --password=<cleardb_password> --reconnect <cleardb_database_name> < your_local.sql

If you don’t know how to get cleardb host, open your heroku dashboard then select your apps settings. Then open “Reveal Config Vars”.
You will get CLEARDB_DATABASE_URL following below.
mysql://abcde:fghij@eu-cdbr-west-01.cleardb.com/databasename?reconnect=true

The bold text is your database host url. 😀