Ghost - 专业开源发布平台
项目概述
Ghost 是一个现代化的开源发布平台,专为专业内容创作者和出版商设计。作为最受欢迎的无头 Node.js CMS 之一,Ghost 提供了强大的内容管理功能,同时保持简洁易用的特性。无论是个人博客、企业网站还是大型媒体平台,Ghost 都能提供优秀的发布体验。
核心亮点
- 现代化编辑器 - 直观的所见即所得编辑体验,支持 Markdown 和富文本编辑
- 无头架构 - 完全的 API 驱动,可与任何前端技术栈集成
- 会员订阅系统 - 内置付费订阅功能,支持 Stripe 集成
- SEO 优化 - 内置 SEO 最佳实践,自动生成结构化数据
- 主题系统 - 丰富的主题生态,支持完全自定义
- 性能优异 - 基于 Node.js 构建,响应速度快,资源占用低
应用场景
Ghost 适用于各种内容发布场景:个人博客、企业官网、新闻媒体、在线杂志、技术文档站点、会员制内容平台等。其灵活的架构设计使其既可以作为传统 CMS 使用,也可以作为无头 CMS 为移动应用或静态网站提供内容服务。
技术架构
技术栈
- Node.js - 后端运行环境,提供高性能的服务器端支持
- JavaScript/TypeScript - 主要开发语言,支持现代 ES6+ 特性
- MySQL/SQLite - 数据库支持,可根据需求选择合适的数据库
- Handlebars - 模板引擎,用于主题开发
- Ember.js - 管理后台前端框架
- Docker - 容器化部署支持
- Redis - 缓存和会话存储
系统架构
Ghost 采用现代化的分层架构设计:
- 前端层 - 基于 Ember.js 的管理界面和基于 Handlebars 的主题系统
- API 层 - RESTful API 和 GraphQL 支持,提供完整的内容管理接口
- 业务逻辑层 - 核心业务逻辑处理,包括内容管理、用户认证、订阅系统等
- 数据访问层 - 数据库抽象层,支持多种数据库类型
- 基础设施层 - 缓存、日志、邮件服务等基础功能
安装指南
方式一:Ghost CLI 安装(推荐)
使用官方 CLI 工具是最简单的安装方式:
# 全局安装 Ghost CLI
npm install ghost-cli -g
# 本地开发环境安装
ghost install local
# 生产环境安装(包含 SSL 配置)
ghost install
方式二:Docker 安装
使用 Docker 可以快速部署 Ghost 实例:
# 拉取 Ghost 镜像
docker pull ghost:latest
# 运行 Ghost 容器
docker run -d \
--name ghost-blog \
-p 2368:2368 \
-v ghost-content:/var/lib/ghost/content \
ghost:latest
方式三:Docker Compose 安装
使用 Docker Compose 可以同时部署 Ghost 和数据库:
# 创建 docker-compose.yml 文件
version: '3.8'
services:
ghost:
image: ghost:latest
ports:
- "2368:2368"
environment:
database__client: mysql
database__connection__host: mysql
database__connection__user: ghost
database__connection__password: ghostpass
database__connection__database: ghost
volumes:
- ghost-content:/var/lib/ghost/content
depends_on:
- mysql
mysql:
image: mysql:8.0
environment:
MYSQL_ROOT_PASSWORD: rootpass
MYSQL_DATABASE: ghost
MYSQL_USER: ghost
MYSQL_PASSWORD: ghostpass
volumes:
- mysql-data:/var/lib/mysql
volumes:
ghost-content:
mysql-data:
# 启动服务
docker-compose up -d
方式四:源码编译
从源码编译适合开发者和高级用户:
# 克隆源码
git clone https://github.com/TryGhost/Ghost.git
cd Ghost
# 安装依赖
yarn install
# 设置开发环境
yarn setup
# 启动开发服务器
yarn dev
使用示例
基本配置
Ghost 的配置文件位于 config.production.json,以下是基本配置示例:
{
"url": "https://your-domain.com",
"server": {
"port": 2368,
"host": "0.0.0.0"
},
"database": {
"client": "mysql",
"connection": {
"host": "localhost",
"user": "ghost",
"password": "your-password",
"database": "ghost_production"
}
},
"mail": {
"transport": "SMTP",
"options": {
"service": "Gmail",
"auth": {
"user": "[email protected]",
"pass": "your-password"
}
}
}
}
主题开发
Ghost 使用 Handlebars 模板引擎,以下是一个简单的主题结构:
my-theme/
├── index.hbs # 首页模板
├── post.hbs # 文章页模板
├── page.hbs # 页面模板
├── author.hbs # 作者页模板
├── tag.hbs # 标签页模板
├── default.hbs # 默认布局
├── partials/ # 部分模板
│ ├── header.hbs
│ └── footer.hbs
├── assets/ # 静态资源
│ ├── css/
│ ├── js/
│ └── images/
└── package.json # 主题配置
基本的 Handlebars 模板示例:
{{!-- index.hbs --}}
{{!< default}}
{{#foreach posts}}
{{title}}
{{excerpt}}
{{/foreach}}
API 调用示例
Ghost 提供了强大的 Content API 和 Admin API:
// 获取所有文章
const posts = await fetch('https://your-site.com/ghost/api/v3/content/posts/?key=your-content-api-key')
.then(res => res.json());
// 获取特定文章
const post = await fetch('https://your-site.com/ghost/api/v3/content/posts/slug/my-post/?key=your-content-api-key')
.then(res => res.json());
// 使用 JavaScript SDK
import GhostContentAPI from '@tryghost/content-api';
const api = new GhostContentAPI({
url: 'https://your-site.com',
key: 'your-content-api-key',
version: 'v3'
});
// 获取文章列表
const posts = await api.posts.browse({
limit: 10,
include: 'tags,authors'
});
API 文档
Content API - 获取文章列表
请求方式:GET
请求地址:/ghost/api/v3/content/posts/
请求参数
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| key | string | 是 | Content API 密钥 |
| limit | number | 否 | 返回文章数量,默认15,最大50 |
| page | number | 否 | 页码,从1开始 |
| include | string | 否 | 包含关联数据:tags,authors,count.posts |
| filter | string | 否 | 过滤条件,如:tag:news |
| order | string | 否 | 排序方式,如:published_at desc |
响应示例
{
"posts": [
{
"id": "5b7ada404f87d200b5b1f9c8",
"title": "Welcome to Ghost",
"slug": "welcome",
"html": "Welcome to Ghost...
",
"excerpt": "Welcome to Ghost, it's great to have you :)",
"published_at": "2018-08-20T15:12:00.000Z",
"updated_at": "2018-08-20T15:12:00.000Z",
"url": "https://demo.ghost.io/welcome/",
"tags": [...],
"authors": [...]
}
],
"meta": {
"pagination": {
"page": 1,
"limit": 15,
"pages": 1,
"total": 1,
"next": null,
"prev": null
}
}
}
社区支持
获取帮助
- GitHub仓库:TryGhost/Ghost
- 官方网站:Ghost.org
- 官方文档:Ghost 文档中心
- 社区论坛:Ghost 论坛
- 问题反馈:GitHub Issues
- 官方 Twitter:@ghost
Ghost(Pro) 托管服务
Ghost 官方提供专业的托管服务 Ghost(Pro),包含以下特性:
- 全球 CDN 加速
- 自动备份和恢复
- SSL 证书自动配置
- 24/7 技术支持
- 自动更新和安全维护
- 高可用性保障
Ghost(Pro) 的收入 100% 用于支持 Ghost 开源项目的持续开发。
参与贡献
Ghost 是一个活跃的开源项目,欢迎各种形式的贡献:
- 代码贡献 - 修复 Bug、添加新功能、改进性能
- 文档改进 - 完善文档、翻译内容、编写教程
- 主题开发 - 创建和分享 Ghost 主题
- 问题报告 - 报告 Bug、提出功能建议
- 社区支持 - 在论坛帮助其他用户
贡献流程
- Fork Ghost 项目到你的 GitHub 账户
- 创建功能分支进行开发
- 遵循代码规范和提交信息格式
- 编写测试用例确保代码质量
- 提交 Pull Request 并等待审核
项目统计
- GitHub Stars:47,000+
- 下载量:100M+
- 贡献者:800+
- 版本:5.128.0(持续更新)
- 开源协议:MIT License
- 主要语言:JavaScript/TypeScript
总结
Ghost 作为现代化的开源发布平台,凭借其简洁的设计理念、强大的功能特性和活跃的社区支持,已经成为内容创作者和开发者的首选工具。无论你是个人博主、企业用户还是开发者,Ghost 都能为你提供专业级的内容发布解决方案。
通过本文的介绍,你已经了解了 Ghost 的核心特性、技术架构、安装方法和使用示例。现在就开始你的 Ghost 之旅,体验这个优秀的开源发布平台带来的便利吧!
立即开始: