GHOST使用教程
Ghost CMS 使用教程:从入门到进阶
Ghost 是一个开源的 Node.js 内容管理系统(CMS),专注于专业出版、博客写作和会员订阅。它设计简洁、现代,支持 Markdown 编辑、实时预览和 SEO 优化,适合个人博主、团队出版或在线杂志。相比 WordPress,Ghost 更轻量、专注写作,而非插件堆积。官方站点:ghost.org。
本教程基于官方文档和社区实践,分为安装、配置、内容管理、主题定制和高级功能。假设您使用 Ubuntu 服务器(推荐 1GB RAM 以上)。如果您是初学者,建议先用开发模式测试。
1. 环境准备
Ghost 需要 Node.js(v18+)、npm 和数据库(默认 SQLite3,生产用 MySQL)。更新系统并安装依赖:
# 更新系统
sudo apt update && sudo apt upgrade -y
# 安装 Node.js(使用 NodeSource 仓库,确保版本正确)
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt install -y nodejs
# 验证安装
node -v # 应显示 v18.x.x
npm -v # 应显示 9.x.x 或更高
# 安装其他工具
sudo apt install -y mysql-server nginx unzip # MySQL 和 Nginx 可选,但推荐
sudo npm install -g ghost-cli # Ghost CLI 工具
注意:如果使用阿里云 ECS 或 DigitalOcean Droplet,选择 Ubuntu 20.04/22.04 镜像。防火墙需开放 80/443 端口。
2. 安装 Ghost
2.1 开发模式安装(本地测试)
适合快速上手:
# 创建目录并下载 Ghost
mkdir ~/ghost-dev && cd ~/ghost-dev
wget https://ghost.org/zip/ghost-latest.zip
unzip ghost-latest.zip -d ghost
cd ghost
# 安装依赖
npm install --development
# 启动开发模式
npm start
访问 http://localhost:2368
,设置管理员账户。开发模式自动重载,适合调试。
2.2 生产模式安装(服务器部署)
使用 Ghost CLI 自动配置 Nginx 和 SSL:
# 创建用户(非 root)
sudo adduser ghostuser # 设置密码
sudo usermod -aG sudo ghostuser
su - ghostuser
# 安装 Ghost
cd ~
ghost install
# 交互式配置
- URL: http://your-domain.com(替换为您的域名)
- 数据库:SQLite3(默认)或 MySQL(输入 root 密码和 ghost 数据库名)
- Nginx: Yes(自动配置)
- SSL: Yes(使用 Let's Encrypt)
- 启动服务: Yes
安装后,访问 http://your-domain.com/ghost/
登录后台。CLI 会生成 config.production.json
文件。
MySQL 配置示例(如果选择 MySQL):
mysql -u root -p
CREATE DATABASE ghost;
CREATE USER 'ghost'@'localhost' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON ghost.* TO 'ghost'@'localhost';
FLUSH PRIVILEGES;
在 Ghost CLI 中输入数据库信息。
Docker 安装(可选,简化部署):
# 使用 Docker Compose
mkdir ghost-docker && cd ghost-docker
# 创建 docker-compose.yml
cat > docker-compose.yml << EOF
version: '3.8'
services:
ghost:
image: ghost:5-alpine
restart: always
ports:
- 2368:2368
environment:
database__client: mysql
database__connection__host: db
database__connection__user: ghost
database__connection__password: your_password
database__connection__database: ghost
url: https://your-domain.com
volumes:
- ghost_content:/var/lib/ghost/content
db:
image: mysql:8
restart: always
environment:
MYSQL_ROOT_PASSWORD: root_password
MYSQL_DATABASE: ghost
MYSQL_USER: ghost
MYSQL_PASSWORD: your_password
volumes:
- mysql_data:/var/lib/mysql
volumes:
ghost_content:
mysql_data:
EOF
docker-compose up -d
访问 http://your-ip:2368
。
常见问题:
- 端口冲突:检查 2368 端口是否占用(
sudo netstat -tlnp
)。 - 权限错误:确保以非 root 用户运行(
sudo chown -R ghostuser:ghostuser ~/ghost
)。 - 网络问题:中国用户下载慢,可用镜像源如
npm config set registry https://registry.npmmirror.com
。
3. 配置与管理
3.1 后台设置
登录 /ghost/
:
- General:设置站点标题、描述、时区、语言(支持 zh-hans 中文)。
- Design:上传 logo、favicon,选择主题。
- Users:添加作者角色(Owner、Author、Editor)。
- Labs:启用会员订阅、动态路由、代码注入。
3.2 邮件与订阅
Ghost 内置邮件新闻通讯:
- 在 Integrations > Email 配置 SMTP(推荐 SendGrid 或 Mailgun)。
- 示例配置(config.production.json):
{
"mail": {
"transport": "SMTP",
"options": {
"host": "smtp.sendgrid.net",
"port": 587,
"auth": {
"user": "apikey",
"pass": "your_api_key"
}
}
}
}
- 测试:Labs > Send a test email。
3.3 SEO 与性能
- 内置 SEO:每篇文章自动生成 meta 标签,支持 OpenGraph。
- Nginx 优化:CLI 已配置 gzip 和缓存。编辑
/etc/nginx/sites-available/ghost
添加:
location / {
proxy_cache_valid 200 60m;
proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
}
- 重启:
sudo nginx -t && sudo systemctl reload nginx
。
4. 内容发布与管理
Ghost 的编辑器简洁,支持 Markdown:
- 新建帖子:后台 > Posts > New post。
- 编辑:标题、内容(Markdown + 实时预览)、特色图像、标签、作者。
- Markdown 示例: “`markdown # 标题 这是一段 粗体 文本。
- 列表项
“`
- 列表项
- 发布:Schedule(定时发布)或 Publish。支持草稿保存。
- 页面:用于静态页如 About、Contact(Pages > New page)。
- 标签与系列:组织内容,生成动态页面(如
/tag/tech/
)。
高级编辑:
- 卡片系统:插入图像、视频、按钮、Toggle 等(点击 + 图标)。
- 导入:从 WordPress 导入(Integrations > WordPress)。
5. 主题与定制
5.1 安装主题
- 下载免费主题:ghost.org/themes 或 GitHub。
- 上传:Settings > Design > Upload theme(ZIP 文件)。
- 激活:选择并保存。
5.2 自定义主题
Ghost 使用 Handlebars 模板:
- 核心文件:
default.hbs
(布局)、post.hbs
(文章页)。 - 示例修改(添加自定义 CSS):
在assets/css/screen.css
添加:
body { font-family: 'Arial', sans-serif; }
- 代码注入:Settings > Code injection 添加头部/脚部脚本(e.g., Google Analytics)。
推荐主题:Lyra(现代博客)、Edition(杂志风格)。
6. 高级功能
- 会员订阅:Settings > Membership 设置付费墙、免费/付费内容。集成 Stripe(Integrations > Stripe)。
- API 使用:Ghost 是无头 CMS,支持 REST/JSON API。示例(获取帖子):
curl https://your-domain.com/ghost/api/v3/content/posts/?key=your_content_api_key
API 密钥:Settings > Integrations > Add custom integration。
- 多语言:Labs > Translations 支持本地化(需主题支持)。
- 备份:
ghost backup
(CLI)或手动备份/var/lib/ghost/content
。
7. 更新与维护
- CLI 更新:
ghost update # 检查并更新到最新版
- 手动更新:
- 下载新 ZIP,备份旧版。
cd /var/lib/ghost && sudo -u ghost -H npm rebuild
。ghost restart
。
- 监控:查看日志
ghost ls
或journalctl -u ghost
。生产环境用 PM2:npm install pm2 -g && pm2 start ghost
。
常见问题:
- SSL 证书过期:
ghost ssl renew
。 - 内存不足:增加 swap 或升级服务器。
- 中文支持:后台不支持中文,但前端主题可添加(e.g., zh-hans 语言包)。
8. 学习资源
- 官方文档:docs.ghost.org(安装、API)。
- 中文社区:ghostchina.com、知乎专栏。
- 教程合集:jsnoteclub.com/ghost-tutorial。
- GitHub:github.com/TryGhost/Ghost(源码、issue)。
- 论坛:Reddit r/Ghost、Ghost 社区论坛。
Ghost 强调“专注写作”,上手后您会爱上它的简洁。建议先在本地搭建测试。如果遇到具体错误(如 Docker 报错),提供日志我可以进一步帮助!
“`