昨天第一次据说ghost,一时兴起今天花了半天的时间从0开始捣鼓,node只会基本使用,js也记不清多久没碰过了,不涉及高级使用姿式,像改主题、改js文件支持某些特性等等。我的能力不够,也没有兴趣深刻研究,就是为了看看这东西怎么用。node
本身安装部署的部分就是彻底照搬官方文档,没有掺杂任何原创性的姿式,可能略有修改或说明不许确的地方彻底是我的失误。mysql
Docker部分是本身尝试用的,由于平时几乎不本身装任何服务了,能用docker的就用docker了,这里也不涉及docker安装和使用的相关说明。nginx
最后吐槽一点:彻底配置结束后我把这篇所有粘贴发布到ghost,那叫一个难看,一点都不优雅,看来美仍是须要精心调整的啊。最可恶的是居然不支持表格...,查了一下,有方法支持,可是到此为止对我我的来说已经没有必要继续了。git
为何把后记放到前面?由于看到这里若是没兴趣能够不要继续浪费时间了...github
到node官网下载安装包sql
傻瓜安装,会同时安装node和npm (macOS)docker
node版本不要过高,官方目前支持到4.xLTS数据库
下载ghost包,下载地址npm
找个目录解压,注意解压目录就是安装目录json
终端进入解压目录执行 npm install -production
,等提示,没错误就OK,有错误解决错误
安装结束后执行:npm start
就开启了ghost服务
访问localhost:2368
查看主页,后台管理地址为localhost:2368/ghost
安装forever
npm install forever -g
ghost目录运行
NODE_ENV=production forever start index.js
结束进程与查看进程
forever stop index.js forever list
安装PM2
npm install pm2 -g
运行ghost服务
NODE_ENV=production pm2 start index.js --name "Ghost"
中止、重启服务
pm2 stop Ghost pm2 restart Ghost # reload热重启 pm2 reload Ghost
远程部署,不须要,须要时再看pm2文档@deploy
初始化脚本,服务自启动
pm2 startup # 保存当前进程 pm2 save
安装supervisor并启动
# debian/ubuntu apt-get install supervisor # fedora/centos yum install supervisor # 运行 service supervisor start
建立ghost启动脚本 /etc/supervisor/conf.d/ghost.conf
cat > /etc/supervisor/conf.d/ghost.conf [program:ghost] command = node /path/to/ghost/index.js directory = /path/to/ghost user = ghost autostart = true autorestart = true stdout_logfile = /var/log/supervisor/ghost.log stderr_logfile = /var/log/supervisor/ghost_err.log environment = NODE_ENV="production" ctrl-d
启动与中止ghost
supervisorctl start ghost supervisorctl stop ghost
建立脚本
sudo curl https://raw.github.com/TryGhost/Ghost-Config/master/init.d/ghost -o /etc/init.d/ghost
打开该文件修改:GHOST_ROOT
为ghost安装目录,DAEMON
为node运行目录
建立用户并修改权限
sudo useradd -r ghost -U sudo chown -R ghost:ghost /path/to/ghost sudo chmod 755 /etc/init.d/ghost
ghost服务管理
sudo service ghost start sudo service ghost stop sudo service ghost restart sudo service ghost status
注册系统启动
sudo update-rc.d ghost defaults sudo update-rc.d ghost enable
注意当前用户权限
sudo adduser USERNAME ghost
安装ghost后,会在安装目录生成默认配置文件config.js
,内容复制自config.example.js
文件,能够修改配置域名、邮箱、数据库、资源目录等等。
名称 | 是否必须 | 描述 |
---|---|---|
url | Y | 设置blog的url |
Y | 设置邮件,用于找回密码和邀请成员 | |
database | Y | 配置数据库,默认使用sqlite3 |
server | Y | ghost服务监听地址与端口 |
compress | N | 开启gzip压缩 |
fileStorage | N | 开启本地存储 |
updateCheck | N | 禁止使用,用privacy.useUpdateCheck代替 |
privacy | N | 功能管理,如update check,rpc ping, google fonts |
forceAdminSSL | N | 强制开启后台管理SSL访问,需配置SSL |
urlSSL | N | 定义第二个url 用于SSL访问 |
paths | N | 自定义content目录 |
maintenance | N | 维护模式 |
referrerPolicy | N | 配置referrer metadata属性 |
url
配置ghost服务URL,须要保持与访问地址一致,指定完整域名,例如:http://ghost-blog.com
email
邮箱用于找回密码,邀请成员,官方说后面会增长邮件订阅等更多服务
mail: { // 自定义from字段:默认为 Blog Title <url> from: 'name@address.com', transport: 'SMTP', options: { host: 'smtp.126.com', secureConnection: false, port: 25, auth: { user: 'name@126.com', pass: 'xxxxxx' } } }
ssl 配置
第一种方式是修改url配置前缀https://
,开启全站SSL
第二种只开启管理后台SSL:forceAdminSSL: true
, http访问会自动跳转
也能够禁止http访问不跳转,直接返回403:forceAdminSSL: {redirect: false}
database
ghost默认配置使用SQLite做为数据库存储,但同时也支持MySQL和Postgres,Postgres支持有点小问题,这里不作说明,mysql配置实例:
database: { client: 'mysql', connection: { host : '0.0.0.0', user : 'ghost', password : '', database : 'ghost_testing', charset : 'utf8' }, // 能够配置pool修改链接池大小,这里是默认值 pool: { min: 2; max: 10 } },
server
配置ghost服务监听地址:
server: { host: '127.0.0.1', port: '2368' } # 也能够配置为socket监听: server: { socket: { path: 'path/to/socket.sock', permissions: '0666' } }
gzip compression
默认开启,须要关闭修改:compress: false
fileStorage
开启/关闭文件存储,有些服务器不支持持久存储的文件系统,因此禁止上传图片,关闭后:fileStorage: false
,上传图片时将只容许输入图片url
paths
ghost默认的content目录就在安装目录,能够经过paths
配置自定义路径,如:
paths: { contentPath: path.join(__dirname, 'path/to/content/dir') },
privacy
须要关闭全部可配置的功能科室直接配置useTinfoil
,开启后全部privacy配置都将失效
privacy: { useTinfoil: true } //单独配置每一项: privacy: { // 更新检查,不订阅官方邮件不会收到通知 useUpdateCheck: false, // 使用谷歌字体服务,但不影响theme中使用的Google Fonts useGoogleFonts: false, // 使用哦Gravatar检测服务,默认会检查邮箱是不是Gravatar帐户 useGravatar: false, // 使用RPC Ping服务,默认开启 useRpcPing: false, useStructureData: false }
maintenance
开启维护模式将返回503响应:maintenance: { enabled: true }
referrerPolicy
配置referer metadta属性,更多关于referrer policy参考W3C@referrer
安装nginx,不在这里扯
配置
# 建立虚拟主机配置文件 cat > /etc/nginx/sites-available/ghost.conf server { listen 80; server_name example.com; location / { proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $http_host; proxy_pass http://127.0.0.1:2368; } } ctrl-d # 软链配置文件 sudo ln -s /etc/nginx/sites-available/ghost.conf /etc/nginx/sites-enabled/ghost.conf
重启nginx生效
sudo service nginx restart
解析域名
获取证书,正式的话就花钱买吧
复制证书到nginx目录
mkdir /etc/nginx/ssl cp server.crt /etc/nginx/ssl/server.crt cp server.key /etc/nginx/ssl/server.key
修改ghost-nginx配置/etc/nginx/sites-available/ghost.conf
server { listen 80; listen 443 ssl; server_name example.com; ssl_certificate /etc/nginx/ssl/server.crt; ssl_certificate_key /etc/nginx/ssl/server.key; location / { proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_set_header X-Forwarded-Proto $scheme; proxy_pass http://127.0.0.1:2368; } }
重启nginx
强制要求admin用户使用HTTPS,修改配置文件:forceAdminSSL: true
备份
登陆管理后台,访问Labs设置页:yourblog.com/ghost/settings/labs/
,导出数据.json
文件,备份content
目录
确认服务重启命令
升级后须要重启服务,确认安装时使用的服务管理方式forever/pm2/supervisor
跨版本升级,和我没大关系
下载新版ghost
curl -LOk https://ghost.org/zip/ghost-latest.zip
解压到一个临时目录,不要直接覆盖原安装目录
unzip ghost-latest.zip -d ghost-temp
进入当前ghost安装目录,删除core目录
cd path/to/ghost rm -rf core
进入临时解压目录,复制文件
cd path/to/ghost-temp cp -R core path/to/ghost cp index.js *.json path/to/ghost cp -R content/themes/casper path/to/ghost/content/themes
回到安装目录升级ghost
cd path/to/ghost chown -R ghost:ghost * npm install --production
重启服务
# 注意使用安装时的服务管理方式 service ghost restart forever restart index.js pm2 restart ghost
这两个服务都有官方镜像源,直接下载(非必须)
docker pull ghost docker pull nginx
# 运行并挂载本地content目录到容器 docker run --name ghost \ -e NODE_ENV=production \ -v /path/to/ghost-content:/var/lib/ghost \ --restart=always \ -d ghost # 后面使用nginx代理,因此这里不作端口映射,默认暴露`:2368`端口 # content目录能够包含自定义配置文件config.js,默认会自动复制config.example.js文件到content目录config.js # 建议提早准备好本身的配置文件,默认production的配置是不完整的 # 注意NODE_ENV环境变量,不设置为production时默认为development # 每次修改完配置文件重启: docker restart ghost
# 运行并挂载配置文件 docker run --name nginx \ -v path/to/nginx.conf:/etc/nginx/nginx.conf \ -v path/to/ghost.conf:/etc/nginx/conf.d/ghost.conf \ --link ghost:GHOST_HOST \ --restart=always \ -p 80:80 \ -d nginx # 注意这里须要同时挂载nginx服务配置和ghost代理配置 # 两个配置文件虽然不是必须从容器外挂载,但仍是分离开方便维护 # 注意修改上面示例中 ghost.conf 文件:proxy_pass: http://GHOST_HOST:2368