將部落格從 WordPress 遷移到 Ghost

將部落格從 WordPress 遷移到 Ghost

寫習慣 Markdown 之後,WordPress 對於現在的我來說已經過於臃腫了,即便有外掛可以支援 Markdown,非原生的體驗還是有差。資料的備份與網站的管理也造成了我不小的負擔,隨著工作佔用的時間越來越多,部落格還是維持簡單就好。因此下定決心把用了很久的 Dreamhost WordPress 搬到自己的 VPS 上面維護。過程有點繁瑣,順手記錄下來,希望可以幫到有同樣需求的人。

安裝 Ghost

Ubuntu 安裝步驟如下:

curl -L https://ghost.org/zip/ghost-latest.zip -o ghost.zip
wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.31.1/install.sh | bash
nvm install v4
cd /var/www/ghost && npm install --production
vi config.js
npm start --production 2>&1 >> /var/log/ghost.log &
# open http://your-ghost.com or http://11.22.33.44:2368

將 Ghost 執行於背景

將 Ghost 執行於背景有以下三種方法:

  • 寫 ghost 命令 /etc/init/ghost.conf
  • 寫啟動腳本 /etc/init.d/ghost
  • 透過 pm2 讓 Ghost 常駐

下面介紹第三種用法:

cd /path/to/ghost/folder
sudo npm install -g pm2
echo "export NODE_ENV=production" >> ~/.profile
source ~/.profile
pm2 kill
pm2 start index.js --name ghost
pm2 dump
sudo pm2 startup <centos|ubuntu|amazon>

文章與圖片備份

  1. 安裝 Ghost Plugin
  2. 到管理後台的工具匯出得到 Ghost 的 JSON 檔案
  3. 將匯出的檔案中的圖片路徑從 wp-content/uploads 改為 content/images,執行命令:sed -i .bak -e 's|wp-content|content\\/images|g' wp2ghost_export_1462376963.json
  4. 備份 wp-content/uploads 圖片目錄
  5. 安裝 Ghost
  6. 匯入前面修改過的 Ghost JSON 檔案
  7. 將備份的 wp-content/uploads 資料夾上傳替換為 /var/www/ghost/content/images

留言備份

安裝 Disqus

  1. 註冊 Disqus,創建新的 Site
  2. 取得 Universal Code 或者直接參考複製這裡的程式碼片段將 disqus_shortname 改為你在 Disqus 註冊的站名
  3. vi /content/themes/THEME_NAME/post.hbs
  4. 將上面複製的程式碼貼到 {{/post}} 與 之間
  5. service ghost restart

https://wordpress.org/plugins/disqus-comment-system/

遷移留言到 Disqus

  1. 安裝 Disqus Comment System
  2. 啟用並且到設定頁登入 Disqus
  3. 迴響 -> Disqus -> Export
  4. 當 Disqus import 完成後會發信件通知下載(不下載也沒關係)
  5. 域名更改後原有的留言就會完成遷移

安裝 uno-zen 主題

uno-zen 是我個人蠻喜歡的 Ghost 主題,uno-zen 主題支援語法高亮,可惜的是根據文件上的說明,目前只有支援 markup, css, c, javascript 三種。

首先,下載主題:

cd /var/www/content/theme
curl -sSL http://git.io/vcIHr | sh

貼到 Code injection -> Blog Footer 中:

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/zepto/1.1.6/zepto.min.js"></script>
<script>jQuery = Zepto</script>

接著做個人化設定:

  1. 重啟 Ghost 後到設定頁更換主題
  2. 替換掉 Logo 與 Cover(這步沒做的話頁面顯示會不正常)
  3. 換上自己的社群網站連結 vi /var/www/ghost/content/themes/uno-zen/partials/social.hbs

大功告成!

Reference