Arul's Blog

Blogging with Ghost and Heroku

Sun 06 July 2014 By Arulmurugan Rajaraman comments

All of us like to write blogs to share our experience. Most of us are not doing because of the work involved in setting up a blog and formatting the posts. What if we can do it in a much faster and simpler way in few minutes? That will be awesome, right?

Ghost

I never thought that blogging can be this much easier before I tried Ghost. Ghost is a blogging engine built using node.js and is open-source. You can sign up for a premium account in ghost website and they will setup your blog in few seconds. If you want it to be free, you can install ghost in any hosting providers of your choice and this will cost nothing but your time. Here lets see how we can get a ghost blog up and running on Heroku for free.

Versions Used

  • Ghost - 0.5.x, 0.6.x
  • Nodejs - v0.10.25
  • Ubuntu - 14.04

Step 1: Setup Heroku account

Go to Heroku site and create an account using your email address. This is free. No credit card or any other payment information required.

Step 2: Setup Heroku toolbelt

Install Heroku toolbelt in your system. The tool is available for all the most popular platforms. Login to your heroku account from the terminal using the command

heroku login

This will setup your heroku account in your system and will upload SSH key from your system to your heroku account.

Step 3: Setup Git

If you are not using git, download and install git from Git SCM. You can learn more about git from the presentation on Slideshare.

Step 4: Create and clone heroku app

Method 1: Login to your heroku account and create an application by giving a name of your choice. The URL of your heroku application will be yourappname.herokuapp.com. After creating the application, clone it to your system using the command

git clone git@heroku.com:yourappname.git

Method 2: You can also create heroku application using the command

heroku create yourappname

You can use any of these methods to create heroku application. If you don't specify your app name in command line, heroku will choose one for you.

Then open the cloned heroku app directory in terminal.

Step 5: Create Heroku database

Create a database in heroku using the following commands which will be used for our blog

heroku addons:add heroku-postgresql:dev

This command will output a message similar to Attached as HEROKU_POSTGRESQL_BRONZE_URL. Here note down the name of name of the database URL. Use the following command to set the database URL

heroku pg:promote HEROKU_POSTGRESQL_BRONZE

Step 6: Install node.js

Download latest version of node.js from its download page and install it. If you are installing it from the source code, you can use the follwing commands.

tar -zxvf node-latest.tar.gz
cd [version of node that was downloaded]
./configure
make
sudo make install

Step 7: Install Ghost

Download latest version of ghost from its download page. Extract the downloaded file and cd into the extracted directory. Then install it using the following command

sudo npm install --production

Step 8: Test ghost configuration

Run ghost locally using the command given below and make sure that everything is installed successfully

sudo npm start

Step 9: Specify database backend

Ghost uses SQLite by default. We need to specify a setting to use postgres database from heroku. To do this add the following line in the dependencies section of package.json in the root directory.

"pg": "latest"

Do not include a comma at the end of last value, as this will lead to an error (I faced it in heroku).

Step 10: Declare Procfile

Procfile will be used to decalre what command should be executed to start a web dyno. Create a text file in the root directory of your application with the following content

web: node index.js --production

This will be used to start your application during each deployment.

Step 11: Production configuration

The settings of your application needs to be modified to match your heroku app settings. These settings need to be modified in the production section of config.js file found in your app root directory.

Step 11.1: URL configuration

Modify the URL to match the URL of your blog application to be hosted.

Step 11.2: Mail configuration

Configure your mail settings to receive email notifications from you ghost blog. If you forget your admin password, email is the only way reset your password. I'm here giving settings specific to Mandrill email account. If you already don't have a mandrill account, create one at Mandrill website and get the API key which will be your mail password. Use the following commands to set username and password in your heroku environment.

heroku config:set MANDRILL_USERNAME=your_mandrill_username
heroku config:set MANDRILL_APIKEY=your_api_key

Then replace the mail settings with the below given settings

mail: {
    transport: 'SMTP',
    host: 'smtp.mandrillapp.com',
    options: {
        service: 'Mandrill',
        auth: {
            user: process.env.MANDRILL_USERNAME,
            pass: process.env.MANDRILL_APIKEY
        }
    }
},
Step 11.3: Database configuration

Login to your heroku account and go to databases tab found in the top. Click on the database url you configured in Step 5. Setup the database details in your heroku account by using the commands given below

heroku config:set POSTGRES_HOST=host
heroku config:set POSTGRES_USER=user
heroku config:set POSTGRES_PASSWORD=password
heroku config:set POSTGRES_DATABASE=database

Then replace the database settings in config.js with the settings given below

database: {
    client: 'pg',
    connection: {
        host: process.env.POSTGRES_HOST,
        user: process.env.POSTGRES_USER,
        password: process.env.POSTGRES_PASSWORD,
        database: process.env.POSTGRES_DATABASE,
        port: '5432'
    },
    debug: false
},
Step 11.4: Port Configuration

Then replace server settings in config.js as given below

server: {
        host: '0.0.0.0',
        port: process.env.PORT
}

Also use the following command to make sure that your app's production setting is being used.

heroku config:set NODE_ENV=production

Without this command, the app didn't work.

Step 12: Deploy to heroku

We are ready to push the application to heroku now. You can do that using the command

git push heroku master

Sometimes you may have to use the command

git push origin master

Step 13: Scale web dyno

Web dynos are processes that will run your application. You need to increase your app's web dyno to 1. Heroku offers only one web dyno for free. If you need more than one web dyno, you need to pay heroku. You can scale the web dyno using the following command

heroku ps:scale web=1

Step 14: Adding custom domain (optional)

If you like to use custom domain for your heroku application, you can do so by adding the domain to your herokuapp and configuring CNAME in your domain DNS editor. Add the domain to your heroku app using the following command.

heroku domains:add www.example.com

Configure CNAME in your domain DNS editor with host as www and Points to as yourappname.herokuapp.com.

Your ghost blog should be live on heroku now. If you are facing any issues, you can check heroku logs using the command

heroku logs

Now we have successfully hosted a ghost blog on heroku for free. You can access your blog on your-domain and your blog's admin on your-domain/ghost/. Feel free to post your feedback and questions as comments.

comments powered by Disqus