Ghost is another awesome blogging platform. Its simple, really fast and powered by NodeJs. As NodeJs is not supported by most of the sharing host server like GoDaddy so thought to try Heroku. Let me share the experience how to Install Ghost on Heroku.
First of all NodeJs need to be installed in machine. Download NodeJs and install if its already not there.
Now Download Ghost from https://ghost.org. Extract the zip. Open the code in any editor. My recent favorite is Webstorm. You can use terminal of your OS or Webstorm. Lets try in Webstorm. Tools -> Open terminal.
In the IDE, load the project. Open pacakege.json. You can change information about the packages here. There are lots of dependencies. Lets install the dependencies first. In the terminal type #npm install –production
You can see all the packages are downloaded in node_modules folder. Goto config.js and change settings if you want. e.g. I have set production url: ‘http://shuvablog.herokuapp.com’. You can change mail setting, database setting etc here.
Its almost ready for local run. Now fire the command #npm start.
Your blog is ready. Open the http://127.0.0.1:2368 link in browser. You can see your blog is up and running. Goto http://127.0.0.1:2368/ghost/ and signup yourself. Click new post and add some entry and publish.
Now its the time to add the code in GIT repository, Upload code in github and publish it in Heroku. GIT need to be installed in your machine.
Now your git is pointed to master branch. Lets Create one branch for github and another for heroku.
Install heroku toolbelt from https://toolbelt.heroku.com/ to execute heroku related commands. To login to heroku account from command prompt use below command and enter credential.
Now create a heroku app using command heroku create <your-app-name>
#heroku create shuvablog
To run ghost in heroku, we need to make some tweak.
By default ghost use sqlite database but as heroku doesnot support local file storage, it doesn’t accept sqlite setting. We need to configure postgreSql database for it.
At first it need to add below dependencies in package.json to get the postgres package.
Run npm install to download postgres.
Now it need to activate postgres in heroku account.
#heroku addons:add heroku-postgresql:dev
Adding heroku-postgresql:dev on shuvablog… done, v6 (free)
Attached as HEROKU_POSTGRESQL_COPPER_URL
Database has been created and is available
See the value of Attached as and use in below command.
#heroku pg:promote HEROKU_POSTGRESQL_COPPER_URL
You can get the postgress cretential in heroku dashboard. Goto your app -> Resources -> In Add-Ons click on Heroku Postgress -> You will get the connecton settings there
It is better not to hard-code the database credential in config.js. Use the environment variable and pass value of those via command line.
#heroku config:set POSTGRES_HOST=host
#heroku config:set POSTGRES_USER=user
#heroku config:set POSTGRES_PASSWORD=password
#heroku config:set POSTGRES_DATABASE=database
Now edit config.js to configure postgres settings. In the production area delete sqlite3 setting and add the setting for postgres. Change the host and port settings also like host: ‘0.0.0.0’, port: process.env.PORT
Create a Procfile in root folder. Enter text into it like
web: NODE_ENV=production node index.js
Now create branch for heroku. It will create branch name ‘heroku’ and point the heroku repository.
#heroku git:remote -a shuvablog
Also create branch for Github. It will create branch name ‘origin’ and point the github repository.
#git remote add origin ‘firstname.lastname@example.org:sonu041/ghostblog.git’
You can see the branches by below command
#git remote -v
heroku email@example.com:shuvablog.git (fetch)
heroku firstname.lastname@example.org:shuvablog.git (push)
origin email@example.com:sonu041/ghostblog.git (fetch)
origin firstname.lastname@example.org:sonu041/ghostblog.git (push)
After all connections are made. Upload the files in git. Just fire the command #git add .
Commit the changes in git -> #git commit -m ‘Initial Commit’
To upload code in github. push the master branch to origin (which is pointed to github).
#git push origin master
Once the deployment is done check out the heroku app link (e.g. http://shuvablog.herokuapp.com/) if its working fine. If you get a nice website then congratulation. You have deployed a working blog in heroku powerd by NodeJs.
goto <url>/ghost (e.g. http://shuvablog.herokuapp.com/ghost) for admin panel. Register yourself and start blogging.
Difficulties I faced.
1. I was getting error like
- ‘Unable to connect to Heroku API’,
- ‘ssh: github.com: no address associated with name fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists.’
- ‘fatal: unable to access Could not r esolve host: github.com’
- fatal: Full write to remote helper failed: Invalid argument
I searched a lot. Tried several solution from stackoverflow but no success. Lets not be hopeless. Finally I got the issue. My antivirus was blocking the connection. 😛
2. fatal: could not read Username for ‘https://github.com’: No such file or directory
I added https link (git remote add origin https://github.com/sonu041/ghostblog.git) to origin. Not sure why but it was not working. Deleted the https link and added the ssh link and then it worked fine.
#git remote rm origin
#git remote add origin ‘email@example.com:sonu041/ghostblog.git’
4. Application was not started. All the time I deployed in heroku the log comes as below (you can check the log by command $heroku logs). All the time process exited with status 0.
2014-04-06T15:14:25.444688+00:00 heroku[web.1]: Starting process with command `node index.js –production`
2014-04-06T15:14:28.306861+00:00 heroku[web.1]: Process exited with status 0
2014-04-06T15:14:28.318669+00:00 heroku[web.1]: State changed from starting to crashed
After wasting 2 hrs (parallel watching India Srilanka T20 Final online) got the solution. I have written ‘web: node index.js –production’ in Procfile. After changing it to ‘web: NODE_ENV=production node index.js’ It works fine. See the change log.
2014-04-06T15:14:28.319079+00:00 heroku[web.1]: State changed from crashed to starting
2014-04-06T15:14:32.258072+00:00 heroku[web.1]: Starting process with command `NODE_ENV=production node index.js`
2014-04-06T15:14:35.612501+00:00 app[web.1]: ?[32mGhost is running…?[39m
2014-04-06T15:14:35.612501+00:00 app[web.1]: Your blog is now available on http://shuvablog.herokuapp.com ?[90m
2014-04-06T15:14:35.612501+00:00 app[web.1]: Ctrl+C to shut down?[39m
2014-04-06T15:14:35.983647+00:00 heroku[web.1]: State changed from starting to up
You can face other issue like git setup, public-private key sharing etc… Google has all(most) the solutions 🙂
You can see the sourcecode of my blog in https://github.com/sonu041/ghostblog. Enjoy 🙂