Book Image

Heroku Cookbook

By : Mike Coutermarsh
Book Image

Heroku Cookbook

By: Mike Coutermarsh

Overview of this book

Heroku is a Platform as a Service that enables developers to rapidly deploy and scale their web applications. Heroku is designed for developer happiness, freeing developers from doing system administrative tasks such as configuring servers and setting up load balancers. Developers are able to focus on what they do best, building web applications, while leaving the details of deployment and scaling to the experts at Heroku. This practical guide is packed with step-by-step solutions to problems faced by every production-level web application hosted on Heroku. You'll quickly get comfortable with managing your Heroku applications from the command line and then learn everything you need to know to deploy and administer production-level web applications.
Table of Contents (17 chapters)
Heroku Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Deploying a Node.js application to Heroku


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.

How to do it…

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:

  1. 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
    

    Note

    This specific Ghost repository was set up to be easy to deploy to Heroku. The difference between this repository and the core Ghost source code is that it has a Procfile added and the configuration has been set up to use a Postgres connection defined by an environment variable.

  2. 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
    
  3. 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: {},
    
  4. 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(-)
    
  5. 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.
    
  6. 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
    

    Note

    Our Ghost installation is set up to parse Heroku's DATABASE_URL to connect to the database. To see how this works, look at the production database section of config.js.

  7. 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
    

    Note

    The terms environment variable and configuration variable are interchangeable.

  8. 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
    

    Note

    Heroku only deploys code in the master branch of our Git repository. If we want to deploy a code from a different branch, we can use the $ git push heroku other_branch_name:master command.

  9. 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
    
  10. 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.

How it works…

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.

The ephemeral filesystem

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 also