Ghost 2.0 Using S3 custom storage module
GetNAS 网站已经从 Wordpress 迁移到 Ghost 2.x,决定将图片等资源迁移到对象存储,涉及到配置 custom storage module
。
支持对象存储的模块有很多,我最终选择了 ghost-storage-adapter-s3
模块。
进入容器
由于网站是跑在 Docker 容器里,所以需要先进入容器。
$ docker exec -it getnas /bin/bash
安装 ghost-storage-adapter-s3
在 Ghost 根目录(/var/lib/ghost
)使用 npm 安装模块:
/var/lib/ghost $ npm install ghost-storage-adapter-s3
/var/lib/ghost $ mkdir -p ./content/adapters/storage
/var/lib/ghost $ cp -r ./node_modules/ghost-storage-adapter-s3 ./content/adapters/storage/s3
特别注意:不要在
current
目录下执行 npm 安装命令。那会导致 Ghost 程序本身的包依赖被干扰,结果是无法启动 Ghost。换句话说就是单独安装存储适配器,让它拥有独立的.node_modules
目录。
编辑配置文件
编辑 Ghost 根目录下的配置文件,程序根目录下有两个配置文件 config.production.json
和 config.development.json
,分别对应生产环境和开发环境。需要根据当前 ghost 程序运行时设定的环境变量决定用哪一个。
我的网站是运行在生产环境下,所以要编辑 config.production.json
这个文件。
添加自定义存储适配器相关配置:
{
"url": "http://localhost:2368/",
"server": {
"port": 2368,
"host": "127.0.0.1"
},
"database": {
"client": "sqlite3",
"connection": {
"filename": "/Users/Herald/Code/tghost/content/data/ghost-local.db"
}
},
"mail": {
"transport": "Direct"
},
"logging": {
"transports": [
"file",
"stdout"
]
},
"process": "local",
"paths": {
"contentPath": "/Users/Herald/Code/ghost/content/"
},
"storage": {
"active": "s3",
"s3": {
"accessKeyId": "YourKeyId",
"secretAccessKey": "YourAccessKey",
"assetHost": "对象存储绑定的自定义域名,带 http:// 或 https:// 协议头",
"bucket": "BucketName",
"endpoint": "兼容 S3 的第三方平台的 endpoint",
"forcePathStyle": true
}
}
}
配置文件中的 paths
设置默认格式为为 "/Users/Herald/Code/ghost/content"
这会导致 Ghost 不从根目录的 content
文件夹查找适配器模块,而是直接去 current/content
目录下去寻找,这样不符合 Ghost 的应用逻辑。可以设置为 "/Users/Herald/Code/ghost/content/"
即末尾添加一个 /
修正此问题。
虽然配置文件中的设置与实际在 docker 容器创建时指定的数据库等设置不同,但我们仍旧无需修改,因为 ghost 会优先从环境变量中读取设置,配置文件中的设置只是作为替补。