- Heroku Cookbook
- Mike Coutermarsh
- 845字
- 2021-08-05 17:14:27
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:
- 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.
- 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/ | git@heroku.com: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
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 ofconfig.js
. - 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.
- 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. - 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.
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
- See about Ghost
- Check out the Ghost project on GitHub at https://github.com/tryghost/Ghost
- SPSS數據挖掘與案例分析應用實踐
- 少兒人工智能趣味入門:Scratch 3.0動畫與游戲編程
- SOA實踐
- PostgreSQL技術內幕:事務處理深度探索
- SQL語言從入門到精通
- Java Web程序設計
- 從零開始學C語言
- Visual Basic程序設計
- NetBeans IDE 8 Cookbook
- Fast Data Processing with Spark(Second Edition)
- Mastering Adobe Captivate 7
- Learning Bootstrap 4(Second Edition)
- Python應用開發技術
- 絕密原型檔案:看看專業產品經理的原型是什么樣
- 軟件開發中的決策:權衡與取舍