Make Ghost running on Heroku

Before the version 0.4 of Ghost, Heroku wasn't a good choice for a production environment for Ghost. The reason was that the images would be overwritten periodically. But now, Ghost allows us to configure it to go through this problem.

Why Heroku ?

I choose Heroku for two reasons:

  • First, I had -- when I chose to open this blog -- many other websites already hosted on Heroku, and I wanted to have all my websites on the same place and because I know how Heroku works.
  • And simply because Heroku just works perfectly.

Let's install Ghost now

First, configure heroku

If you don't have an Heroku account yet, start by going on this page to create one: https://id.heroku.com/signup

Next, you have to install the Heroku Toolbelt: https://toolbelt.heroku.com/

As Heroku suggests on its documentation:

It’s generally a good idea to login and add your public key immediately after installing the heroku toolbelt so that you can use git to push or clone Heroku app repositories:

$ heroku login
Enter your Heroku credentials.  
Email: joe@example.com  
Password:  
Uploading ssh public key /Users/joe/.ssh/id_rsa.pub  

If you don't know how to setup your ssh key, just follow this: Heroku instructions for Managing Your SSH Keys

Then install Ghost

If you skipped the previous step, just type in your command line:

$ heroku login

Next, download the latest version of ghost:

$ wget https://ghost.org/zip/ghost-latest.zip

To test your new blog, you will need to install node.js on your local machine:

$ wget http://nodejs.org/dist/node-latest.tar.gz
$ tar -xvzf node-latest.tar.gz
$ cd node-v0.10.24/
$ ./configure
$ make
$ sudo make install

Then unzip and install Ghost:

$ cd ghost-latest/
$ npm install --production

The next step will be to make sure that everything got installed successfully:

$ npm start

And launch your favorite browser and on 127.0.0.1:2368.

Fantastic ! Everything is running !

Push it on Heroku

First, tell to heroku to start Ghost. The way to this is to use a Procfile. Create a text file named Procfile and add it:

web: node index.js --production

Then create a git repository for the Ghost directory:

$ git init
$ git add .
$ git commit -m "First commit"

Now create the Heroku app:

$ heroku create <app-name>

Replace <app-name> by your application name.

If you want to know exactly what heroku create do, just go on this page: Creating Apps from the CLI

Because you cannot use SQLite on Heroku (explanations), we will configure Ghost to use PostgreSQL.

First, we have to add a PostgreSQL database to our heroku app:

$ heroku addons:add heroku-postgresql:dev
Adding heroku-postgresql:dev to sushi... done, v69 (free)
Attached as HEROKU_POSTGRESQL_RED_URL
Database has been created and is available

Then make this database as your primary database:

$ heroku pg:promote HEROKU_POSTGRESQL_RED_URL
Promoting HEROKU_POSTGRESQL_RED_URL to DATABASE_URL... done

Replace HEROKU_POSTGRESQL_RED_URL by the correct one returned by the previous command.

Heroku example application

Click on your newly created database (for me I have to click on Heroku Postgres :: Teal).

Then you have this page:

Heroku example database configuration

In your command line type:

$ heroku config:set POSTGRES_HOST=<value> \
    POSTGRES_DATABASE=<value> \
    POSTGRES_USER=<value> \
    POSTGRES_PASSWORD=<value>

Just replace the <value> by the corresponding values for your database.

With your favorite text editor open the file config.js in the root folder of Ghost. We will modify the database configuration for the production environment:

// ...

config = {
    // ...
    production: {
        // ...
        database: {
            client: 'postgres',
            connection: {
                host: process.env.POSTGRES_HOST,
                database: process.env.POSTGRES_DATABASE,
                user: process.env.POSTGRES_USER,
                password: process.env.POSTGRES_PASSWORD,
                port: '5432'
            }
        },
        // ...
    },
    // ...
};

// ...

Also, you should choose an URL for your blog:

$ heroku config:set BLOG_URL=<your-url>

Then edit again config.js, you will change 3 variables this time:

config = {
    // ...
    production: {
        url: process.env.BLOG_URL,
        // ...
        server: {
            host: '0.0.0.0',
            port: process.env.PORT
        }
    },
    // ...
};

For the environment of Node to production:

$ heroku config:set NODE_ENV=production

Your blog is now ready to go on production environment on Heroku.

$ git add .
$ git commit -m "Blog ready"
$ git push heroku master

The last line will deploy Ghost on Heroku, this process may take one or two minutes.

Once this process is done, it will output an URL you can use to see you blog live !

Enjoy !

Sending email

To send emails we will use the Sendgrid addon from Heroku:

$ heroku addons:add sendgrid:starter

Now you have to add the sendgrid creadentials to your config.js, first in your command line type:

$ heroku config
=== your-blog Config Vars
DATABASE_URL:              postgres://something...
HEROKU_POSTGRESQL_RED_URL: postgres://something...
NODE_ENV:                  production
SENDGRID_PASSWORD:         <your-password>
SENDGRID_USERNAME:         <your-username>

You can see two new variables begginning by SENDGRID.

Now edit your config.js:

config: {
    production: {
        // ...
        mail: {
            transport: 'sendgrid',
            host: 'smtp.sendgrid.net',
            options: {
                service: 'Sendgrid',
                auth: {
                    user: process.env.SENDGRID_USERNAME,
                    pass: process.env.SENDGRID_PASSWORD
                }
            }
        },
        //...
    },
};

Your email system is now working.

Troubleshooting

Sometimes, you may have this kind of page:

Error page from Heroku

The first think to do is to read the logs. To do this, just type:

$ heroku logs

The case of PostgreSQL

In my case, while I followed all those instructions I had the famous error page.

In the logs I found:

Error: Cannot find module 'pg'

The reason why you encounter this message is because you need to add the pg module to ghost. Open the file package.json and add this line:

{
    //...
    "dependencies": {
        // ...
        "pg": "~2.6.0"    // <- this line
    },
    // ...
}

Then execute

$ npm install

And commit your changes:

$ git add .
$ git commit -m "Added pg module"
$ git push heroku master

You blog should be running fine now !

If you encounter other issues while deploying Ghost on Heroku feel free to contact me or to post a comment !

View Comments