There are a few tutorials on hosting a Ghost blog on Heroku, but I could not find any for the latest Ghost 2.0 update. I was able to get it working (including updating my old 0.11.x version in place while preserving all my existing posts – I do not detail the upgrade process*).
It wasn't obvious, but the way to go is to follow the steps in the article on Using Ghost as an NPM module. In order to get the "content" folder that they show in the file tree, I downloaded the zip version of the latest 2.0.3 version.
My package.json
was simple:
{
"name": "ghost-blog",
"version": "0.0.1",
"description": "UX Files blog",
"author": "Nick Burwell",
"homepage": "http://blog.nickburwell.com",
"main": "./index.js",
"scripts": {
"start": "node index"
},
"dependencies": {
"ghost": "^2.0.3"
}
}
My config.production.json
file ended up looking like:
{
"server": {
"host": "0.0.0.0",
"port": 2368
},
"database": {
"client": "mysql",
"connection": {
"host": "xxxxxxxxxxx.cleardb.net",
"port": 3306,
"user": "yyyy",
"password": "zzzz",
"database": "heroku_xxxxxxxx"
},
"pool": {
"min": 2,
"max": 2
}
},
"paths": {
"contentPath": "content"
}
}
I created a free ClearDB MySQL add on in heroku, and put in the host, user, password and database values.
One tricky part was getting the correct port for heroku. Because the config files are now JSON and not a JS file, you can't simply put process.env.PORT
as a value in the server/port object. Instead, I solved it in the main index.js
file by programmatically setting the config before starting the ghost server:
const ghost = require('ghost');
ghost()
.then(function (ghostServer) {
var serverConfig = ghostServer.config.get('server');
ghostServer.config.set('server', {
host: serverConfig.host,
port: process.env.PORT || serverConfig.port
});
return ghostServer.start();
})
At this point I was able to push my files to heroku and deploy. I went to my blog URL and created the initial admin user via the UI wizard, then went to Settings > Labs and uploaded my previous JSON export of blog posts/settings. Success!
\* In summary, to upgrade from before 1.0 all the way to 2.x: download an export from your running blog (in the admin settings), then run version 1.x locally (any set up there is fine, using the ghost-cli for a local server works). Import the file and then re-download a new export, then import that 1.x version on your new 2.x blog.