Debian 9.x / Ubuntu 16.04.x 搭建 Ghost 1.x 教程

Debian 9.x / Ubuntu 16.04.x 搭建 Ghost 1.x 教程

之前写过《Debian 8.x / Ubuntu 16.04.x 搭建 Ghost 教程》,而当时 Ghost 还是 0.10.x,并没有自己的 cli 工具,就在今天,Ghost 宣布发布 v1.0.2 作为最新稳定版,安装方法和以前的完全不同,所以我们单独写几篇文章指导大家如何安装 Ghost 1.0.x

1、更新系统并安装必要软件

这个就不需要我多讲了,首先用 root 用户登陆系统,如果不是的话先切换到 root

Debian 9.x 下

apt-get update && apt-get upgrade
apt-get -t stretch-backports update && apt-get -t stretch-backports upgrade

Ubuntu 16.04 下

apt-get update && apt-get upgrade

然后安装一些必要的软件

apt-get install apt-transport-https lsb-release ca-certificates unzip wget curl sudo sqlite3

2、安装 Node.js 6.x LTS

我们仍然采用 NodeSource 编译的 Node.js 源

Debian 9.x 下

curl -sL https://deb.nodesource.com/setup_6.x | bash -  
apt-get install nodejs

Ubuntu 16.04 下

curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -  
apt-get install nodejs  

国内机器安装他们的源可能会很慢,可以修改 /etc/apt/sources.list.d/nodesource.listhttps://deb.nodesource.com/node_6.x 替换成 https://mirrors.tuna.tsinghua.edu.cn/nodesource/deb_6.x/ 然后执行 apt-get update 在安装 Node.js 即可

3、添加 ghost 运行用户

首先使用 adduser 而不是 useradd 命令添加用户,后者只能添加一个简单的无密码无执行权限的用户,前者是后者的加强版命令

adduser ghost

然后系统会提示你输入两次密码,其他的一律回车即可

接着给予 ghost 用户 sudo 权限,这样这货就可以执行好多操作了

usermod -aG sudo ghost

4、安装 Ghost-CLI 和 Ghost 1.0.x

Ghost 从 1.0 开始,已经不需要其他第三方的软件来保持后台运行、更新、安装等操作,因为他们出了个命令行软件 Ghost-CLI 有了这货,我们再也不需要安装 pm2 来保持后台运行,也不需要用 ghost-upgrade 来升级,因为他基本已经全部带了以前的功能

首先我们需要切换到 ghost 这个用户

su - ghost

输入密码回车

然后直接用 npm 安装 Ghost-CLI

sudo npm i -g ghost-cli

假设你的博客要放在 /var/www/ghost 目录,那么我们就创建一个并赋予权限

sudo mkdir /var/www/ghost
sudo chown ghost:ghost /var/www/ghost

进入这个目录就可以开始安装 Ghost 了

cd /var/www/ghost
ghost install

因为官方的文档只对 Ubuntu 16.04 进行了测试,所以如果直接这么做的话,Debian 9.x 下会提示

System checks failed with message: 'Linux version is not Ubuntu 16'
Some features of Ghost-CLI may not work without additional configuration.
For local installs we recommend using `ghost install local` instead.

其实脑子想想也知道,Ubuntu 下可以用的东西,Debian 下绝 B 没鸟问题,然后再看一下这个文档,并且由于本站博客比较小,懒得配置个 MySQL 了,所以我最后决定使用 SQLite3 作为数据库,然后配置所有的环境和依赖(其实用起来一毛一样)

所以我们直接执行这个命令即可

ghost install local --db=sqlite3

不出意外的话,就会提示安装完毕,出意外的话,慢慢检查以上步骤,如果是国内机器,那么大多数情况是出口网络太渣导致下载失败,没别的办法,挂一层 socks5 代理配合 proxychains 才是最好的选择,具体方法请 Google

然后修改 /var/www/ghost/config.development.json 文件,修改自己的域名即可,其他配置请参考官方的 Config 文档

这时候可以用 ghost stop && ghost restart 命令强行关闭再启动(注意不是重启),看是否正确,正确的输出信息是类似这样的

ghost@sb-blog:/var/www/ghost$ ghost start
✔ Validating config
✔ Starting Ghost
You can access your blog at https://sb.sb/

Ghost uses direct mail by default
To set up an alternative email method read our docs at https://docs.ghost.org/docs/mail-config

没问题以后,就可以把这个文件改成正式环境的文件了

mv config.development.json config.production.json

接着可以重启试试

ghost restart

没问题即可

5、安装 Nginx

这个步骤可以参考《Debian 9.x “stretch” 使用源安装最新版本 LEMP / LNMP》和《Ubuntu Server 16.04.x (Xenial Xerus) 安装 LEMP / LNMP 教程》

Debian 9.x 推荐使用 Ondřej Surý 打包的源

sudo wget -O /etc/apt/trusted.gpg.d/nginx-mainline.gpg https://packages.sury.org/nginx-mainline/apt.gpg
sudo sh -c 'echo "deb https://packages.sury.org/nginx-mainline/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/nginx-mainline.list'
sudo apt-get update

Ubuntu 16.04 推荐使用 Ondřej Surý 制作的 PPA

sudo apt-get install software-properties-common
sudo add-apt-repository ppa:ondrej/nginx-mainline
sudo apt-get update

然后直接安装

sudo apt-get install nginx

编辑一个例如 /etc/nginx/sites-enabled/example.com.conf 配置文件

sudo cat >> /etc/nginx/conf.d/example.conf << EOF
server {
        listen 80;
        listen [::]:80;

        server_name example.com;		

        location / {
            proxy_pass http://127.0.0.1:2368;
            proxy_redirect default;
            proxy_set_header Host $http_host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forward-IP       $remote_addr;
            proxy_set_header X-Forwarded-Proto $scheme;
			
            client_max_body_size    10m;
            client_body_buffer_size  512k;
            proxy_connect_timeout    5;
            proxy_read_timeout       60;
            proxy_send_timeout       5;
            proxy_buffer_size        16k;
            proxy_buffers            4 64k;
            proxy_busy_buffers_size 128k;
            proxy_hide_header X-Powered-By;
        }
}
EOF

检查并重启 Nginx

sudo nginx -t && sudo nginx -s reload

好了,接下来访问 http://example.com/ghost/ 注册第一个账号即为管理员

6、升级 Ghost 0.11.0 到 Ghost 1.x

官方没有给任何升级工具,也就是说,就一个土办法,导出再导入

首先在新装好的博客后台 Labs > Delete all content 清空所有的新装博客的文章数据

然后在你旧的博客后台 Labs > Export your content 点击 Export 导出所有的数据,得到一个类似 sb.ghost.2017-07-27.json 的文件,记得下载到本地

然后在 Labs > Import content 导入刚才那个文件

然后在 Team 页面里把那个默认叫 Ghost 的鬼家伙删除,然后检查 GeneralDesign 里的设置对不对

最后,把你之前用的主题目录打包成 .zip 文件,在后台的 Design > Themes 里上传并激活

SB Blog:以上步骤是不是很 2B ?
Ghost 官方:是的
SB Blog:为什么要这么做?
Ghost:你夏天过去了,到冬天要换新衣服了,还不得把旧衣服脱掉?难道我们直接在你旧衣服上加棉花?

好像很有道理的样子,反正我是不懂他们逻辑,你看 WordPress 这么多年了,还不是照样一键升级向下兼容,但愿 1.0 以后的版本不要再这么折腾了吧

7、其他问题

本站制作的 Affinity 主题也在这两天更新到 1.3.0 版本并且兼容 Ghost 1.x 如果你担心的主题无法兼容,那么 Ghost 官方倒是有提供一个叫做 gscan 的在线工具帮你检测你的主题是否满足 Ghost 1.x 的要求,按提示慢慢改即可

另外千万不要在 root 用户下执行 ghost 相关命令,否则 /var/www/ghost/.ghostpid 这个文件的权限就变成 root 用户的了,导致后续会遇到 Message: 'EACCES: permission denied, open '/var/www/ghost/.ghostpid'' 这样的傻逼问题

如在 Debian 9.x 安装中遇到错误,请随时在下方留言,其他操作系统的问题按心情回复

文章来源:

Author:Showfom
link:https://sb.sb/debian-ubuntu-install-upgrade-ghost/