Inside Out

DIY - Ghost (from git) on Openshift

The easiest way to get started in less than an hour with your own ghost deployment is ghost quickstart for openshift. It’s a great reference, if you’re stuck along the way.

Why cook your own? Curiousity and an attempt to try the bleeding edge bits (every minor release of Ghost :P). Let's try to setup hosted ghost linked to the [Ghost Stable git branch](https://github.com/TryGhost/Ghost/tree/stable).

*As I finished writing this post, I came cross a [nice post](http://www.mttschltz.com/installing-ghost-on-openshift-without-a-quickstart/) on the same topic by [Matthew Schultz](http://www.mttschltz.com/). Wish I had came across it while during my experimentation. I highly recommend his post to get started.*

I will leave these raw notes for my future reference :)

Setting up git remotes {#settingupgitremotes} ----------------------

Sign up for an [openshift account](https://www.openshift.com) if you've not yet! Setup your first app with the [nodejs on openshift getting started](https://developers.openshift.com/en/node-js-getting-started.html) tutorial.

At this point, we have a git remote setup for us by `rhc`. Idea is pretty simple, every push to the git repo starts a deployment of your app.

We will setup another git remote for this repository and point it to [ghost stable tree](https://github.com/TryGhost/Ghost/tree/stable). Then merge the `upstream/stable` branch to our master branch for deployment.

git remote add upstream https://github.com/TryGhost/Ghost.git git fetch upstream git merge upstream/stable

Commit away the changes :)

Build some assets {#buildsomeassets} -----------------

Instructions for these are in the [install from git](https://github.com/TryGhost/Ghost#install-from-git) page.

npm install -g grunt-cli npm install grunt init grunt prod npm start

At this point, validate your local installation of ghost is successful by navigating to

`grunt prod` command builds javascript files necessary for running ghost. While openshift automatically builds your nodejs files on a `git push origin master`, it doesn't have a way to build the client side js files. We may commit these files we built locally to our repo.

cd git add core/built git add core/client

Getting ready for npm start {#gettingreadyfornpmstart} ---------------------------

Ensure the `package.json` file follows [guidelines from openshift nodejs environment setup](https://blog.openshift.com/run-your-nodejs-projects-on-openshift-in-two-simple-steps) blog.

Apply the [503 error fix](https://github.com/openshift-quickstart/openshift-ghost-quickstart/issues/9) for `npm start` to `package.json` file. See [this](https://github.com/mttschltz/ghostblog/commit/116e31eae61bcd582613cad7d0fbdc3bfebdc2e4) commit for a diff view.

Set the production environment variables for openshift gear. This will ensure openshift doesn't install devDependencies on the git deploy.

rhc set-env NODE_ENV=PRODUCTION -a myapp rhc set-env NPM_CONFIG_PRODUCTION=true -a myapp

We can [persist](https://developers.openshift.com/en/managing-action-hooks.html#_cartridge_control_action_hooks) this environment variable as a `pre_start_nodejs` script. See an example in the openshift-quickstart [here](https://github.com/openshift-quickstart/openshift-ghost-quickstart/blob/master/.openshift/action_hooks/pre_restart_nodejs)

Persist the data {#persistthedata} ----------------

Just one last bit of caution that I learnt the hard way: openshift deployment doesn't persist the content and images :(

![Openshift Environment Variables](/content/images/2015/02/20150221-openshift-envvar.jpg)

The trick here is to use `$OPENSHIFT_DATA_DIR` as a content and image store. We can use a `deploy hook`.

cd mkdir .openshift/action_hookswget https://raw.githubusercontent.com/openshift-quickstart/openshift-ghost-quickstart/master/.openshift/action_hooks/deploy -O .openshift/action_hooks/deploychmod +x .openshift/action_hooks/deploy

We're all set. Do a final commit and `git push origin master`. Your openshift application should automatically build and deploy!

Tips and tricks {#tipsandtricks} ---------------

Several errors that I came across and some remarks:

- `git push origin master` times out during app build. It happens due to `ssh` time out. Follow the guidelines at - If nodejs application doesn't start, try logging in with ssh and use `tail_all` command to see the log. Mostly this happens if we miss a step above: a) `grunt prod` to build the client files, or b) environment variables are not set - If you see the instructions for admin setup everytime, clearly you missed the persist step above :) - Moving your old wordpress.com data is a snap. Use . I had a ton of tags in wordpress, had to consolidate it manually :( - I did another silly mistake. If you setup my localhost ghost with wordpress and then do a `ghost export` and `import` to online ghost; it is going to override the "owner" user account (I had same login). It's good to remove the user info from `ghost export` and try import. It gets messy, be careful. - If all fails: delete the data in `$OPENSHIFT_DATA_DIR` and ghost is reset.