🌟 在 Heroku 上搭建 Ghost 博客 2.x 版本 | Host Ghost 2.x on Heroku

之前的 VPS 要到期了,不想续费(~~毕竟穷~~),然后突然想起了 Heroku。想着能不能把 Ghost 2.X 版本部署到 Heroku,谷歌了一下,找到个方法。

0、注册 Heroku 账号,并新建一个 APP

有条件的绑定个信用卡以提升 Free Dyno Hours。

进入新建的 App - Resources - Add-ons 搜索 mysql 新建一个免费的 ClearDB MySQL 数据库。

1、本地安装 Heroku Cli 并登录

下载 Heroku Cli (x64x32)、Git-scm(Win) 并安装,打开 CMD 或者 PowerShell 登录 Heroku:

heroku login

2、创建文件

在本地新建一个文件夹,创建下面几个文件:

package.json:

{
  "name": "ghost-blog",
  "version": "0.0.1",
  "description": "Ghost Blog",
  "author": "YJK",
  "homepage": "https://github.com/ygbhf/ghost-heroku",
  "main": "./index.js",
  "scripts": {
    "start": "node index"
  },
  "dependencies": {
    "ghost": "^2.9.1"
  }
}

其中 "ghost": "^2.9.1" 版本号可以修改为最新的 Releases 版本号

index.js:

const ghost = require('ghost');

ghost()
    .then(function (ghostServer) {
        var serverConfig = ghostServer.config.get('server');
        ghostServer.config.set('server', { 
            host: serverConfig.host, 
            port: process.env.PORT || serverConfig.port
        });
        return ghostServer.start();
    })

config.production.json:

{
  "url": "http://APP_NAME.herokuapp.com",
  "server": {
      "host": "0.0.0.0",
      "port": 2368
  },
  "database": {
    "client": "mysql",
    "connection": {
      "host": "us-cdbr-iron-east-01.cleardb.net",
      "port": 3306,
      "user": "",
      "password": "",
      "database": "heroku_"
    },
    "pool": {
      "min": 2,
      "max": 2
    }
  },
  "paths": {
    "contentPath": "content"
  },
  "logging": {
    "level": "info",
      "rotation": {
        "enabled": true
       },
    "transports": ["file", "stdout"]
  }
}

修改其中的 url 为你自己的域名,或者不想绑定域名的话使用 Heroku 的默认域名。

修改 databaseMysql 数据库连接信息,可以在 Settings - Config Vars 中查看:

其中形如

mysql://*:@us-cdbr-iron-east-01.cleardb.net/heroku_***?reconnect=true
// 依次为 "user":"password"@"host"/"database"

下载 Ghost 源代码将其中的 content 文件夹整体复制到本地文件夹中,然后整个文件夹目录应该为以下的样子:

- Ghost
  - content
  - config.production.json
  - index.js
  - package.json

3、部署至 Heroku

在本地文件夹右击 - Git Bash Here

执行:

git init
git add -A
// 执行 git commit 的时候如果出现错误,则执行以下两段
// git config --global user.name "YOUR NAME"
// git config --global user.email "YOUR EMAIL"
git commit -m "First Commit"
heroku git:remote -a YOUR_APP_NAME
// 更改 YOUR_APP_NAME 为你的 Heroku 应用名称
heroku push heroku master

然后访问 http://APP_NAME.herokuapp.com 不出意外的话即搭建成功,访问 /ghost 创建账号。


!!!在 Heroku 上部署的(免费)应用会在 10 分钟没访问的情况下自动休眠,休眠后的下次访问会增加响应时间。

!!!不能上传图片至 Heroku 搭建的 Ghost,会自动删除。

本文参考了 1

More from Youth
All posts