Debian 12 安装配置 Ghost 5

环境检查

环境依赖

  • Systemd (Debian12 自带)
  • 使用 SQLite3 作为数据库(无须安装 MySQL)
  • 至少 1 GB 物理内存(可设置 1GB 以上的 Swap 代替)
  • 一个非 root 且拥有 sudo 权限的用户来安装 Ghost(用户名不能为 ghost
  • nginx(如需使用 HTTPS 则需要 nginx >= 1.9.5)
  • Node.js( 支持的 Node.js 版本 ,推荐 node@20)

检查系统内存

系统内存信息:

free
​ total used free shared buff/cache available
Mem: 1016168 100360 293520 356 622288 746024
Swap: 1048572 0 1048572

检查当前用户

安装 Ghost 需要使用一个非 root ,用户名不为 ghost(Ghost 默认会创建一个名为 ghost 的专用系统用户,目的是以最小权限运行 Ghost 进程)且拥有 sudo 权限的用户。如果没有,需要新建一个用户进行后续操作。

检查 nginx

检查 nginx 版本:

sudo nginx -v
nginx version: nginx/1.22.1

检查 Node.js

检查 Node.js 版本:

node -v
v20.19.1

安装 Ghost

配置 nginx

新建网站配置文件:

sudo vim /etc/nginx/sites-available/blog.avinzheng.com.conf

写入配置:

# blog.avinzheng.com

map $status $header_content_type_options {
  204 "";
  default "nosniff";
}

server {
  listen 80;
  server_name blog.avinzheng.com;
  client_max_body_size 50m;

  location / {
    proxy_pass http://127.0.0.1:2368;
    proxy_set_header Host $http_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    add_header X-Content-Type-Options $header_content_type_options;
  }
}

server_name 字段为自己解析到主机的域名,多个域名空格隔开。

建立符号链接到 /etc/nginx/sites-enabled/ 目录下:

sudo ln -s /etc/nginx/sites-available/blog.avinzheng.com.conf /etc/nginx/sites-enabled/blog.avinzheng.com.conf

验证 nginx 配置是否正确:

sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

重启 nginx 服务:

sudo systemctl restart nginx

配置 AppArmor

如果配置了 nginx 的 AppArmor 规则,需要修改规则文件:

sudo vim /etc/apparmor.d/usr.sbin.nginx

需要添加相应文件路径:

#include <tunables/global>

/usr/sbin/nginx {
  ...
  /var/lib/nginx/body/** rw,
  /var/lib/nginx/proxy/** rw,
}

加载 AppArmor 配置文件:

sudo apparmor_parser -r /etc/apparmor.d/usr.sbin.nginx

安装 Ghost-CLI

使用 npm 全局安装 Ghost-CLI :

sudo npm install -g ghost-cli@latest

Ghost 在使用 Ghost-CLI 启动后会使用降权用户 ghost 来运行 Ghost 进程,如果使用 pnpm 或者 Yarn 安装需要确保 global binnode_modules 均在系统级路径( user/, user/local/ 等路径下),不能为默认的用户级路径(~/.local/ 等),不然启动 Ghost 会因为权限问题出错:

Can't connect to the bootstrap socket (localhost 8000) ECONNREFUSED

查看安装的 Ghost-CLI 版本:

ghost -v
Ghost-CLI version: 1.27.0

新建安装目录

新建安装目录并设置权限:

sudo mkdir -p <dir>
sudo chown <user>:<user> <dir>
sudo chmod 775 <dir>
  • <dir> 为Ghost 安装目录
  • <user> 为当前用户(运行 Ghost 的用户)

安装 Ghost

进入安装目录:

cd <dir>

使用 Ghost-CLI 在当前目录安装 Ghost :

ghost install --db=sqlite3 --no-setup-nginx --no-setup-ssl

安装时会自动创建 Ghost 服务并设置开机启动。

安装完成后,通过浏览器访问管理后台地址并注册管理员账号:

http://blog.avinzheng.com/ghost

配置 Ghost

生产环境下 Ghost 的配置位于config.production.json 文件中:

vim config.production.json

隐私配置

添加隐私配置:

"privacy": {
  "useUpdateCheck": false,
  "useGravatar": false,
  "useRpcPing": true,
  "useStructuredData": false
}
  • useUpdateCheck:自动版本更新检查和匿名统计数据收集,默认开启。
  • useGravatar:自动下载用户邮箱对应的 Gravatar 头像,默认开启。
  • useRpcPing:自动在发布文章后通知搜索引擎,默认开启。
  • useStructuredData:自动生成适用于某些社交网站的结构化数据,默认开启。

安全配置

添加安全配置:

"security": {
  "staffDeviceVerification": false
}
  • staffDeviceVerification:在新设备登录 Ghost 管理后台时发送邮件验证码,默认开启。

搜索配置

Ghost 使用 JsDeliver CDN 来加载用于本地搜索的 sodo-search 资源文件。

如果主题没有集成本地搜索功能,可以去掉加载的 sodo-search 的 JS 文件:

"sodoSearch": {
  "url": false
}

如果集成了,可以将文件放到主题的 assets 目录里面,添加以下配置:

"sodoSearch": {
  "url": "/assets/sodo-search/sodo-xxx.js",
  "styles": "/assets/sodo-search/sodo-xxx.css"
}

Ghost 升级后该配置需要重新添加到到配置文件。

管理 Ghost

服务管理

查看配置的 Ghost 服务:

ghost ls

启动 Ghost 服务:

ghost start <service>

停止 Ghost 服务:

ghost stop <service>

重启 Ghost 服务:

ghost restart <service>

查看 Ghost 服务状态:

sudo systemctl status <service>

设置 Ghost 服务开机启动:

sudo systemctl enable <service>

停止 Ghost 服务开机启动:

sudo systemctl disable <service>

参考文献