Heroku is a polyglot platform that can host applications built in many different languages and frameworks. In this recipe, we will learn how to deploy Ghost, a popular open source blogging platform built on Node.js.
We'll build on what we learned in the previous recipe, Deploying a Rails application to Heroku. Here, we'll see that there are a lot of similarities between deploying the two different applications. The process to deploy any application to Heroku is very similar to the previous recipe, irrespective of the language or framework in which it is written.
We'll be setting up and deploying Ghost from the command line. Let's open up a terminal to begin with by performing the following steps:
First, we'll need to download the Ghost source code from GitHub. We'll clone an existing Ghost Git repository that's been set up to run on Heroku:
$ git clone https://github.com/mscoutermarsh/ghost_heroku.git Cloning into 'ghost_heroku'... remote: Counting objects: 16411, done. remote: Compressing objects: 100% (7480/7480), done. remote: Total 16411 (delta 8481), reused 16381 (delta 8455) Receiving objects: 100% (16411/16411), 8.55 MiB | 1.75 MiB/s, done. Resolving deltas: 100% (8481/8481), done. Checking connectivity... done
Let's navigate to the new
ghost_heroku
directory and create a new Heroku application:$ cd ghost_heroku $ heroku apps:create Creating fast-coast-3773... done, region is us http://fast-coast-3773.herokuapp.com/ | [email protected]:fast-coast-3773.git Git remote heroku added
The configuration for our application is in the
config.js
file. Let's open the file now and update the default production URL to reflect our new Heroku application's URL (given to us from Heroku in the previous step):production: { url: 'http://my-ghost-blog.com', mail: {},
Commit the changes to Git:
$ git commit -am 'Updating production URL config' [master cd0ec0e] Updating production URL config 1 file changed, 1 insertion(+), 1 deletion(-)
We'll use Postgres as our database for Ghost. We can create a new Postgres database now:
$ heroku addons:add heroku-postgresql:dev Adding heroku-postgresql:dev on fast-coast-3773... done, v3 (free) Attached as HEROKU_POSTGRESQL_SILVER_URL Database has been created and is available ! This database is empty. If upgrading, you can transfer ! data from another database with pgbackups:restore. Use `heroku addons:docs heroku-postgresql:dev` to view documentation.
We'll need to set up our new database as the primary one for our application by promoting it. In the previous command, Heroku gave us a unique database name. It follows the format of
HEROKU_POSTGRESQL_COLOR_URL
. We'll use that name as the argument for the next command:$ heroku pg:promote HEROKU_POSTGRESQL_SILVER_URL Promoting HEROKU_POSTGRESQL_SILVER_URL to DATABASE_URL... done
Next, we'll need to set a configuration variable to let Node know which environment it's running on. Let's set it to
production
:$ heroku config:set NODE_ENV=production
Our application now has everything it needs to be deployed. Let's push our repository to Heroku to deploy our code:
$ git push heroku master
Once the build process is complete, our blog will be up and running. We can now launch a browser from the command line to see the following screen:
$ heroku open
To access Ghost's admin panel, add
/ghost
at the end of the URL. We can then create an account and start playing with our new blog.
Heroku uses a unique term for its web servers; it calls them dynos. A dyno starts out as a plain Ubuntu Linux web server. It's during the initial push and slug-compilation process that Heroku auto detects the type of application we are trying to deploy and installs the software necessary for it to run.
Heroku uses an ephemeral filesystem. This means that any files written to disk after the creation of the slug will not be persisted beyond the life of the dyno. All Heroku dynos are cycled every 24 hours. There is a good reason for this restriction: it allows our application to scale. If we were to allow file storage on Heroku dynos, we'd have to replicate the file across every dyno.
When writing blog posts with Ghost, we'll see that we are able to upload images. The problem with this feature is that Ghost currently stores these images on the web server. This won't work for us on Heroku; we'll have to use a file store outside Heroku, such as Amazon S3 or Dropbox.
See ghost.org to learn more about Ghost
Check out the Ghost project on GitHub at https://github.com/tryghost/Ghost