The Right Code

Home of Greg Bergé. Let's speak about JavaScript.

Deploy Ghost to heroku for free

Deploying Ghost to heroku is a true challenge, the Ghost team says that it is not supported. In fact, it's possible to deploy Ghost to heroku, but you must make concession. Actually Ghost doesn't support other storage system than file system, so if you deploy Ghost on heroku, you should not use the very nice upload feature.

Signup on heroku

The first step is signing up to heroku. If you are familary with heroku just go to the next step, else you can follow this quick start guide.

Download Ghost

The first thing to do is to download the last version of Ghost, as i am writing this article, the last version is 0.4.1, you can find it on the download page. Since Ghost offers blog hosting, the download page is not very accessible, but you can access it directly on https://ghost.org/download/.

Download Ghost

After downloading Ghost, we can extract the zip into your blog directory. Let's call it my-blog.

$ unzip ghost-0.4.1.zip -d my-blog

Initialize the heroku application

Heroku deployment is based on git, so we must create a git repository to host Ghost source code.

Create a git repository is very simple, just go into your fresh created directory my-blog and run these commands.

$ git init
Initialized empty Git repository in ~/my-blog/.git/  
$ git add -A
$ git commit -m 'Initialize repository.'
[master (root-commit) f6984f1] Initialize repository.
 194 files changed, 77679 insertions(+)
 create mode 100644 Gruntfile.js
 ...

Ghost release doesn't come with a .gitignore. If we install the application in development, we will need to install dependencies. The node_modules directory will not be ignored and the world will collapse. To avoid commiting modules, we must create a .gitignore with node_modules.

$ echo "node_modules/" > .gitignore
$ git add .gitignore
$ git commit -m 'Ignore "node_modules" directory.'
[master b35d205] Ignore "node_modules" directory.
 1 file changed, 1 insertion(+)
 create mode 100644 .gitignore

Then we can initialize the heroku application.

$ heroku create my-blog --region eu
reating my-blog... done, region is eu  
http://my-blog.herokuapp.com/ | git@heroku.com:my-blog.git  
Git remote heroku added  

Install PostgreSQL

The default database used by heroku is SQLite. Using SQLite on heroku is not possible because at each deployment, heroku will erase all local files.

So i advice you to use PostgreSQL, it's a good database, better than mySQL on many points I think notroll.

$ heroku addons:add heroku-postgresql:dev
Adding heroku-postgresql:dev on my-blog... done, v3 (free)  
Attached as HEROKU_POSTGRESQL_CHARCOAL_URL  
Database has been created and is available  

Heroku advice us to promote the base before using it, then you can use the environment variable DATABASE_URL cleanest than the custom variable HEROKU_POSTGRESQL_CHARCOAL_URL.

heroku pg:promote HEROKU_POSTGRESQL_CHARCOAL_URL  
Promoting HEROKU_POSTGRESQL_CHARCOAL_URL to DATABASE_URL... done  

Using PostgreSQL in Ghost requires node-postgres module. The module have to be added in application dependencies. To do that, the easiest method is to use npm.

$ npm install pg --save
npm http GET https://registry.npmjs.org/pg  
...
pg@2.11.1 node_modules/pg  
├── buffer-writer@1.0.0
├── generic-pool@2.0.3
├── nan@0.6.0
└── pgpass@0.0.1 (split@0.2.10)

The package.json has been updated, changes need to be commited.

$ git add package.json
$ git commit -m 'Add pg as dependency.`
[master 2d18cfd] Add pg as dependency.
 1 file changed, 88 insertions(+), 87 deletions(-)
 rewrite package.json (99%)

Ok PostgreSQL is installed, as we are in plugins installation, we will install the mail plugin to enable mailing in Ghost application.

Install Mandrill

They are several free mailing add-ons avalaible on heroku. Personnaly, my prefered service is Mandrill because I love monkeys.

$ heroku addons:add mandrill
Adding mandrill on my-blog... done, v5 (free)  
Use `heroku addons:docs mandrill` to view documentation.  

Configure application

We have services, now we must configure the application.

The first thing to do is to copy the example config file.

cp config.example.js config.js  

Now we must set up database and email in production environment. The production section must be replaced by these configuration:

  // ### Production
  production: {
    url: 'http://my-blog.com',
    mail: {
      transport: 'SMTP',
      host: 'smtp.mandrillapp.com',
      options: {
        service: 'Mandrill',
        auth: {
          user: process.env.MANDRILL_USERNAME,
          pass: process.env.MANDRILL_APIKEY
        }
      }
    },
    database: {
      client: 'postgres',
      connection: process.env.DATABASE_URL,
      debug: false
    },
    server: {
      host: '0.0.0.0',
      port: process.env.PORT
    }
  }

We are on a git repository, so we must commit changes.

$ git add config.js
$ git commit -m 'Configure application.'

Deploy on heroku

The last step before enjoying your Ghost blog is to deploy. To do that, it's very simple, you just have to push on heroku.

$ git push heroku master

There is one more thing to do, the Ghost blog is actually in development mode, so it will doesn't work, let's run it in production.

$ heroku config:set NODE_ENV=production
Setting config vars and restarting my-blog... done, v8  

Your blog is now avalaible: http://my-blog.herokuapp.com/

Set up your blog

To set-up your blog, go to admin, http://my-blog.herokuapp.com/ghost/.

You will be asked to signup the first user of your blog.

Signup first Ghost user

Just click on signup and enjoy your Ghost blog !

Welcome to Ghost

How to deal with images ?

Amazon S3 or other services are not avalaible in version 0.4.x. So waiting the version 0.5.0, we must use other services like Dropbox or imgur.

Using Dropbox

With Dropbox, it's very simple, just upload images into Public directory or a subdirectory of Public. Then you can copy the link of the image and use it directly in your articles.

Copy Public Link

comments powered by Disqus