搬瓦工部署 Ghost 博客平台教程
Ghost 是一款基于 Node.js 的现代开源博客平台,以优雅的写作体验和出色的 SEO 优化著称。Ghost 内置了会员订阅、Newsletter 邮件、内容变现等功能,非常适合专业博主和内容创作者。本文将在搬瓦工 VPS 上从零部署 Ghost。
一、方案推荐
- 最低配置:1 核 1GB 内存(Ghost 官方最低要求)。
- 推荐配置:2 核 2GB 内存,运行更加流畅。
Ghost 基于 Node.js,相比 PHP 博客系统内存占用稍高。具体方案请参考 搬瓦工全部在售方案。
二、系统准备
以 Ubuntu 22.04 为例,先更新系统并安装基础工具:
apt update && apt upgrade -y
apt install curl wget build-essential -y
三、安装 Node.js
Ghost 5.x 需要 Node.js 18.x 或 20.x。使用 NodeSource 安装:
curl -fsSL https://deb.nodesource.com/setup_20.x | bash -
apt install nodejs -y
验证安装:
node -v
npm -v
四、安装 MySQL
Ghost 生产环境需要 MySQL 8.0:
apt install mysql-server -y
systemctl enable mysql
systemctl start mysql
mysql_secure_installation
创建 Ghost 专用数据库和用户:
mysql -u root -p
CREATE DATABASE ghost_production CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'ghostuser'@'localhost' IDENTIFIED BY '你的强密码';
GRANT ALL PRIVILEGES ON ghost_production.* TO 'ghostuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;
五、安装 Nginx
apt install nginx -y
systemctl enable nginx
Ghost CLI 会自动配置 Nginx,这里先安装好即可。
六、创建系统用户
Ghost 官方建议不要使用 root 用户运行。创建专用用户:
adduser ghostuser
usermod -aG sudo ghostuser
su - ghostuser
七、安装 Ghost CLI
Ghost CLI 是 Ghost 的官方命令行工具,用于安装、配置和管理 Ghost:
sudo npm install ghost-cli@latest -g
八、安装 Ghost
创建站点目录并安装 Ghost:
sudo mkdir -p /var/www/ghost
sudo chown ghostuser:ghostuser /var/www/ghost
sudo chmod 775 /var/www/ghost
cd /var/www/ghost
ghost install
安装过程中,Ghost CLI 会依次询问以下信息:
- Blog URL:输入你的域名,如
https://your-domain.com。 - MySQL hostname:
localhost。 - MySQL username:
ghostuser(之前创建的数据库用户)。 - MySQL password:输入数据库密码。
- Ghost database name:
ghost_production。 - Set up Nginx:选择 Yes,Ghost CLI 会自动创建 Nginx 配置。
- Set up SSL:选择 Yes,自动申请 Let's Encrypt 证书。
- Set up systemd:选择 Yes,自动创建系统服务实现开机自启。
- Start Ghost:选择 Yes,安装完成后立即启动。
九、完成初始设置
安装完成后,访问 https://your-domain.com/ghost 进入管理界面。首次访问需要创建管理员账号:
- 输入站点名称。
- 填写管理员姓名、邮箱和密码。
- 可选邀请团队成员(可跳过)。
设置完成后即可进入 Ghost 管理后台,开始写作和配置站点。
十、Ghost 常用管理命令
# 查看 Ghost 状态
cd /var/www/ghost
ghost status
# 停止 Ghost
ghost stop
# 启动 Ghost
ghost start
# 重启 Ghost
ghost restart
# 查看 Ghost 日志
ghost log
# 更新 Ghost
ghost update
# 查看 Ghost 配置
ghost config
十一、配置邮件发送
Ghost 的会员订阅和 Newsletter 功能需要邮件服务。编辑 Ghost 配置文件:
nano /var/www/ghost/config.production.json
在配置中添加邮件设置(以 Mailgun 为例):
"mail": {
"transport": "SMTP",
"options": {
"service": "Mailgun",
"host": "smtp.mailgun.org",
"port": 587,
"secure": true,
"auth": {
"user": "postmaster@your-domain.com",
"pass": "your-mailgun-password"
}
}
}
保存后重启 Ghost:
ghost restart
十二、主题管理
Ghost 内置了简洁的 Casper 主题。你可以在 Ghost Marketplace 中找到更多免费和付费主题。安装方法:
- 下载主题 zip 文件。
- 在 Ghost 管理后台进入"Settings" > "Design" > "Change theme"。
- 点击"Upload theme"上传 zip 文件。
- 激活主题。
你也可以通过命令行安装:
cd /var/www/ghost/content/themes/
unzip your-theme.zip
ghost restart
十三、性能优化
13.1 启用 Nginx 缓存
在 Nginx 配置中添加静态资源缓存:
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2)$ {
expires 30d;
add_header Cache-Control "public, immutable";
}
13.2 配置 CDN
Ghost 对 Cloudflare CDN 支持良好,配置后可以加速全球访问并减轻服务器负载。
13.3 数据库优化
如果内存有限,调整 MySQL 配置减少内存占用:
nano /etc/mysql/mysql.conf.d/mysqld.cnf
添加:
innodb_buffer_pool_size = 128M
innodb_log_file_size = 32M
max_connections = 50
十四、备份与恢复
Ghost 后台提供了内容导出功能(Settings > Labs > Export),可以导出文章和设置为 JSON 文件。但完整备份还需要包含图片和主题文件:
#!/bin/bash
DATE=$(date +%Y%m%d)
BACKUP_DIR="/opt/backups/ghost"
mkdir -p $BACKUP_DIR
# 备份数据库
mysqldump -u ghostuser -p你的密码 ghost_production > $BACKUP_DIR/ghost_db_$DATE.sql
# 备份内容目录(包含图片和主题)
tar czf $BACKUP_DIR/ghost_content_$DATE.tar.gz -C /var/www/ghost content
# 删除旧备份
find $BACKUP_DIR -mtime +30 -delete
十五、常见问题
Ghost 安装失败
常见原因:Node.js 版本不兼容、内存不足或权限问题。检查 Node 版本是否为 18.x 或 20.x,确保有足够内存(至少 1GB),使用专用用户而非 root 安装。
SSL 证书申请失败
确保域名已正确解析到服务器 IP,且 80 端口和 443 端口均已开放。可以先跳过 SSL 设置,稍后手动配置:
ghost setup ssl
升级后站点异常
查看日志排查问题:
ghost log
如果是主题不兼容新版本导致的问题,可以临时切换回默认的 Casper 主题。
Ghost 与其他博客平台对比
与 WordPress 相比,Ghost 更专注于写作和内容发布,内置的会员订阅和 Newsletter 功能让内容变现更简单。与 Typecho 相比,Ghost 功能更丰富但资源占用也更多。
总结
Ghost 是一个专业的现代博客平台,Ghost CLI 让部署过程非常便捷。在搬瓦工 VPS 上运行 Ghost 可以获得完全的控制权和更好的性价比。购买 VPS 时使用优惠码 NODESEEK2026 享受折扣,更多教程请参考 新手教程。