/ Ghost

Free hosting for Ghost

This post won't be related to main blog topics. However, recently Red Hat has closed OpenShift v2 platform where my blog was hosted and I had to migrate it to the new version or find another hosting. Finally, I decided to try with OpenShift v3.

Luckily, Red Hat prepared a helpful tutorial about Ghost and the migration, but there were some parts which were missed when you wanted to use free tier, private git repository or your custom domain. Thus, I will try to give some hints here to make installation easier.

Also, if you want to start new blog and you are not interested in migration, I think that still it can be very helpful for you.

The whole tutorial can be a little bit rough, but I hope that you will find it helpful.

Pre requirements

At the beginning, I suggest to start with reading the Red Hat tutorial and get familiar with it. There is not need to follow the steps from it as they will be presented here in extended form, but reading explanations prepared by Red Hat can be helpful to understand more.

Next, download from Red Hat page the oc program and login - required command and token/password, you will find on https://console.starter-us-east-1.openshift.com/console/command-line.

oc login https://api.starter-us-east-1.openshift.com --token=<hidden>

Setup MySQL

With the free tier, you are allowed to run two machines (pods). First of them, we will use to prepare database. So create a project and MySQL DB.

oc new-project <project-name>
oc new-app mysql-persistent

Remember to save output from the command above as there will be login details to MySQL.

Code repository

Create BitBucket repository with your blog code. Eventually, if you are starting from scratch, you can obtain code from the official Ghost repo.

Next, generate SSH key pair.

ssh-keygen -t rsa -b 4096 -C -f <key name> "your_email@example.com"
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/<key-name>

If you have some troubles, check the GitHub tutorial about generating SSH keys

Also, due to security reasons, remember to do not use your basic key and just generate a new one as you will have to upload private key to Red Hat's cloud.

When your key files are ready, on BitBucket go to the settings of your repository and add public key (Repositories > your-repository-name > Settings > Access keys > Add key).

Ghost application

The second pod, we will use to run Ghost application. But before we will do it, we have to add private SSH key to the cloud and configure to use it.

oc secrets new-sshauth sshsecret --ssh-privatekey=~/.ssh/<key-name>
oc annotate secret/sshsecret 'build.openshift.io/source-secret-match-uri-1=ssh://<git-repository>' --overwrite
oc link builder sshsecret

Next, we can try to create the app:

oc new-app nodejs~<git-repository> g --name=ghost -e OPENSHIFT_APP_DNS="" -e OPENSHIFT_MYSQL_DB_HOST="mysql" -e OPENSHIFT_MYSQL_DB_PORT="3306" -e OPENSHIFT_APP_NAME="sampledb" -e OPENSHIFT_NODEJS_IP="0.0.0.0" -e OPENSHIFT_NODEJS_PORT="8080" -e OPENSHIFT_MYSQL_DB_USERNAME="<username>" -e OPENSHIFT_MYSQL_DB_PASSWORD="<password>"

It's likely that it will fail due to a problem with fetching source code from BitBucket.

So then call:

oc patch buildConfig ghost -p '{"spec":{"source":{"sourceSecret":{"name":"sshsecret"}}}}'

Also, you will have to login to the OpenShift web panel and free Ghost pod if you use free tier. When it's done, rerun build.

oc start-build ghost -n <build-name>

Routing

To make your app visible, you have to configure routing.

oc expose svc ghost
oc set env dc ghost OPENSHIFT_APP_DNS="<your-custom-domain>"

If you do not have your own domain, use the default one assigned by OpenShift. However, it's not too pretty.

oc expose svc ghost
oc get route ghost
oc set env dc ghost OPENSHIFT_APP_DNS="<from previous command below HOST/PORT>"

Custom domain

We have already started a bit domain configuration in the previous step. However, there are few things which left.

First, login to your domain provider website and configure CMAKE entry.

Then, login to your new ghost app and go to Navigation tab and put your domain in as a Home entry.

Migrating content

In the Red Hat tutorial, they suggest to create persistent storage for blog images and other content. However, with the free tier, you can use only one storage which has been already taken by MySQL. Thus, I've uploaded files directly to the Ghost app pod. In example, to upload images, you can do it with the following command:

oc rsync ./images/ <pod-name>:/opt/app-root/src/content/images

However, uploaded images remain there as long as pod will be alive. Thus, maybe it is better to use external service to host the images.

Sources