如何設置 Headless Ghost CMS 與 Vercel 整合
在這篇全面詳細的教程中,我將帶您逐步了解如何設置 Headless Ghost CMS 與 Vercel 整合,創建一個高效的 JAMstack 部落格網站。這種架構組合利用了 Ghost 強大的內容管理功能和 Vercel 卓越的前端託管服務,讓您獲得兩者的最佳優勢。
什麼是 Headless CMS?
Headless CMS 是指只使用內容管理系統的後端來管理資料,而不使用其自帶的前端展示層。在這種架構中,Ghost 只負責內容管理,而前端則使用 Next.js 等框架通過 API 獲取內容並展示。
第一部分:設置 Ghost CMS
Ghost CMS 需要一個伺服器來運行。您有幾個選擇:
選項 1:使用 DigitalOcean 托管 Ghost
-
創建 DigitalOcean Droplet
- 註冊 DigitalOcean 帳戶
- 創建一個新的 Droplet (推薦最低配置:2GB RAM)
- 選擇 Ubuntu 作為操作系統
-
安裝 Ghost
- 通過 SSH 連接到您的 Droplet
- 執行以下命令更新伺服器:
sudo apt-get update sudo apt-get upgrade -y
- 創建一個非 root 用戶(不要使用 ‘ghost’ 作為用戶名,會與 Ghost CLI 衝突):
sudo adduser ghostuser sudo usermod -aG sudo ghostuser su - ghostuser
- 安裝必要的軟件:
sudo apt-get install nginx mysql-server -y
- 配置 MySQL:
sudo mysql ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'YOUR_PASSWORD'; exit;
- 安裝 Node.js(Ghost 支持的版本):
curl -sL https://deb.nodesource.com/setup_18.x | sudo -E bash sudo apt-get install -y nodejs
- 安裝 Ghost CLI:
sudo npm install ghost-cli@latest -g
- 創建 Ghost 安裝目錄:
sudo mkdir -p /var/www/yoursitename sudo chown ghostuser:ghostuser /var/www/yoursitename sudo chmod 775 /var/www/yoursitename cd /var/www/yoursitename
- 安裝 Ghost:
在安裝過程中,您需要回答一些問題(網站 URL、是否設置 SSL 等)。ghost install
選項 2:使用 Render 托管 Ghost
- 在 Render 上設置 Ghost
完成安裝後,您需要獲取 Ghost Content API 密鑰:
- 登錄到 Ghost Admin 面板 (通常是
https://您的網域/ghost
) - 導航到 Settings > Integrations
- 點擊 “Add custom integration”
- 為您的集成命名(例如 “Next.js Frontend”)
- 創建後,您將獲得 Content API Key 和 API URL,請保存這些信息。
第二部分:設置 Next.js 項目
-
創建 Next.js 應用
npx create-next-app ghost-nextjs-blog cd ghost-nextjs-blog
-
安裝 Ghost Content API 包
npm install @tryghost/content-api
-
設置環境變量
- 在項目根目錄創建
.env.local
文件:
GHOST_API_URL=https://您的ghost網站地址 GHOST_API_KEY=您的Content API Key
- 在項目根目錄創建
-
創建 Ghost API 客戶端
- 在項目中創建
ghost.js
文件:
const GhostContentAPI = require('@tryghost/content-api'); const api = new GhostContentAPI({ url: process.env.GHOST_API_URL, key: process.env.GHOST_API_KEY, version: 'v4' }); export default api;
- 在項目中創建
-
創建顯示文章的頁面
- 在
pages
文件夾中創建posts.js
:
import api from '../ghost'; export async function getStaticProps() { const posts = await api.posts.browse(); return { props: { posts }, revalidate: 60 }; } function Posts({ posts }) { return ( <div> {posts.map(post => ( <div key={post.id}> <h2>{post.title}</h2> <div dangerouslySetInnerHTML={{ __html: post.html }} /> </div> ))} </div> ); } export default Posts;
- 在
-
創建單篇文章頁面
- 在
pages
文件夾中創建post/[slug].js
來處理單篇文章:
import api from '../../ghost'; export async function getStaticPaths() { const posts = await api.posts.browse(); return { paths: posts.map(post => ({ params: { slug: post.slug } })), fallback: 'blocking' }; } export async function getStaticProps({ params }) { const post = await api.posts.read({ slug: params.slug }); return { props: { post }, revalidate: 60 }; } function Post({ post }) { return ( <div> <h1>{post.title}</h1> <div dangerouslySetInnerHTML={{ __html: post.html }} /> </div> ); } export default Post;
- 在
第三部分:將 Next.js 應用部署到 Vercel
-
將代碼推送到 GitHub
git init git add . git commit -m "Initial commit" git remote add origin https://github.com/您的用戶名/您的倉庫名.git git push -u origin main
-
部署到 Vercel
- 註冊或登錄 Vercel 帳戶
- 連接您的 GitHub 帳戶
- 點擊 “New Project”
- 選擇您的 GitHub 倉庫
- Vercel 會自動檢測它是一個 Next.js 項目並配置構建設置
- 在設置中添加環境變量(與您的
.env.local
中相同):GHOST_API_URL
GHOST_API_KEY
- 點擊 “Deploy”
第四部分:設置 Webhook 以自動重新部署
當您在 Ghost 中更新內容時,您希望 Vercel 能夠自動重建您的網站:
-
在 Vercel 中獲取 Webhook URL
- 在 Vercel 控制面板中,轉到您的項目
- 轉到 Settings > Git
- 滾動到 “Deploy Hooks” 部分
- 創建一個新的 hook 並複製 URL
-
在 Ghost 中設置 Webhook
- 登錄到 Ghost Admin
- 轉到 Settings > Integrations
- 選擇您之前創建的集成
- 滾動到 “Webhooks” 部分
- 點擊 “Add webhook”
- 選擇 “Site” 作為事件
- 粘貼您的 Vercel webhook URL
- 保存 webhook
最佳實踐與注意事項
-
Ghost 定價考慮
- 如果只需要 Headless CMS 功能,可以考慮自託管 Ghost,因為 Ghost Pro 的基本計劃 ($9/月) 不支持自定義集成。
- 自託管 Ghost 可以使用像 DigitalOcean 這樣的 VPS,月費約 $5。
-
性能優化
- 使用 Next.js 的 Incremental Static Regeneration 功能來平衡構建時間和內容更新。
- 考慮使用 CDN 來優化全球分發。
-
開發流程
- 在本地開發時,您可以通過
ghost install local
命令在本地安裝 Ghost 進行測試。 - 使用
next dev
命令在本地運行 Next.js 應用。
- 在本地開發時,您可以通過
結論
通過這個設置,您擁有了一個功能強大的 JAMstack 部落格,其中 Ghost CMS 負責內容管理,Next.js 和 Vercel 負責前端展示和部署。這種架構提供了卓越的性能、安全性和擴展性。
最適合的方案取決於您的具體需求和預算。如果您是開發人員或技術愛好者,自託管 Ghost 是一個經濟實惠的選擇。如果您偏好更無憂的解決方案,Ghost Pro 加上 Vercel 可能更適合,儘管成本會更高。
無論您選擇哪種方案,結合 Ghost 的內容管理能力和 Next.js 的前端靈活性,您都能建立出一個現代化、高性能的部落格或內容網站。
需要專業的數位轉型方案嗎?Tenten.co 擁有豐富的 Headless CMS 和現代網站架構經驗,能為您的企業建立高效能、易於管理的數位體驗。我們的專家團隊提供從 Ghost CMS 設置到 Vercel 部署的全方位服務,幫助您的網站獲得最佳性能和使用者體驗。立即預約免費諮詢會議,探討如何將您的網站提升到新的高度!