Running Ghost 1.0 on Windows with IISNode

Introduction

在 Ghost 1.0 之後,作者更新了安裝過程,使用者可以使用 ghost-cli 在 Linux 上輕鬆設定、安裝。

但 CLI 目前不支援在 Windows 中自動設定,本篇文章將示範如何在 IIS 下運作 Ghost(使用 IISNode),

Ghost

Ghost 是用 JavaScript 編寫的部落格平台,基於 MIT 許可證開放原始碼。Ghost 的設計主旨是簡化個人網站發布以及網上出版的過程。

Wikipedia ── Ghost_(blogging_platform)

  • Ghost 為個人部落格系統
  • 使用 Node.js 語言和 MySQL 資料庫
  • 可以使用 Markdown 和 HTML 混合編輯文章

Preparation

Installation

  1. 安裝 node.js LTS,接著安裝 ghost-cliknex-migrator

    1
    npm i -g ghost-cli knex-migrator
  2. 設定環境變數並進入網站根目錄:

    1
    2
    set NODE_ENV=production
    cd "C:/interpub/wwwroot/blog.holey.cc/"
  3. 安裝 Ghost 並建立預設組態:

    1
    2
    ghost install --db sqlite3 --no-prompt --no-stack --no-setup
    ghost config --ip 127.0.0.1 --port 2368 --no-prompt --db sqlite3 --url https://blog.holey.cc
  4. 進行資料庫遷移:

    1
    knex-migrator-migrate --init --mgpath "C:/interpub/wwwroot/blog.holey.cc/current"
  5. 調整 C:\interpub\wwwroot\blog.holey.cc\config.production.json,將 server port 移除:

    1
    2
    3
    4
    5
    6
    7
    {
    "url": "https://blog.holey.cc",
    "server": {
    "host": "127.0.0.1"
    },
    // [...]
    }

    若想要指定 Ghost Content 資料夾路徑(假設為同一目錄下的 content),可設定為:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    {
    "url": "https://blog.holey.cc",
    "server": {
    "host": "127.0.0.1"
    },
    "paths": {
    "contentPath": "content"
    // [...]
    }
  6. C:\interpub\wwwroot\blog.holey.cc\ 上建立一檔案 index.js,其內容如下:

    1
    2
    process.env.server__port = process.env.PORT;
    require('./current/index.js');
  7. C:\interpub\wwwroot\blog.holey.cc\ 上建立一檔案 web.config,其內容如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    <configuration>
    <system.webServer>
    <handlers>
    <clear />
    <add name="iisnode" path="index.js"
    verb="*" type="" modules="iisnode"
    scriptProcessor="" resourceType="Unspecified"
    requireAccess="Script" allowPathInfo="false"
    preCondition="" responseBufferLimit="4194304" />
    </handlers>

    <iisnode node_env="production"
    nodeProcessCountPerApplication="1"
    enableXFF="true" />

    <rewrite>
    <rules>
    <rule name="ghost">
    <match url="/*" />
    <action type="Rewrite" url="index.js" />
    </rule>
    </rules>
    </rewrite>
    </system.webServer>
    </configuration>

    其他更多 web.config 設定項目可參考 Github 範例

Issues

目前最新版本 1.18.2 在登入 ghost 時會發生停在轉圈畫面,透過 Chrome DevTools 發現是 1.18.2/core/built/assets/ghost.min-xxx.js1.18.2/core/built/assets/vendor.min-xxx.js 出現問題。

但改正錯誤後登入 ghost 仍會出現錯誤,並且也是錯在同個地方。目前暫時使用 1.13.0 版本的檔案來修正錯誤,方法如下:

  1. 複製 1.13.0/core/built/assets/ghost.min-xxx.js1.13.0/core/built/assets/vendor.min-xxx.js 檔案至 1.18.2/core/built/assets/ 目錄下
  2. 開啟 1.18.2/core/server/admin/views/default-prod.html 將原本的引入的檔案改為 1.13.0 的檔案
  3. 重啟 IIS(iisreset

References