在 vps 安装 Ghost 博客系统记录

610 查看

0. 前期准备

我的 VPS 系统比较差,很多必备的东西都没安装。

apt-get update
apt-get -y upgrade
apt-get install -y build-essential vim curl unzip

1. 安装 nodejs 和 npm

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

2. 安装 ghost

进入你想要安装的目录(一般我放在 /home/wwwroot/domain_name/ 下)执行:

curl -L https://ghost.org/zip/ghost-latest.zip -o ghost.zip
unzip -uo ghost.zip
npm install --production
npm start

这样装好后,外网是访问不到的,因为生产环境只监听了127.0.0.1的请求。你可以修改 config.js 来修改设定,但是我们一般在前端再用一层 nginx 做中转。

3. 安装并配置 nginx

apt-get install -y nginx
rm /etc/nginx/sites-enabled/default
vi /etc/nginx/sites-available/ghost.conf

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;
    }
}

创建链接:

ln -s /etc/nginx/sites-available/ghost.conf /etc/nginx/sites-enabled/ghost.conf

启动nginx:

service nginx start

这个时候你使用域名就可访问到了。

4. 配置后台运行

后台运行有很多方式,比如 forever 、Supervisor 等等。这里我们使用 Supervisor 。

apt-get install -y supervisor
service supervisor start
vi /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"

执行以下代码启动 ghost :

service supervisor start
supervisorctl reload

如果遇到问题或修改了配置,可使用以下命令检查或重载:

supervisorctl tail -f ghost stderr
supervisorctl restart ghost
supervisorctl update

5. 优化 ghost 配置

经过之前的配置,现在ghost是可以访问到了,但是还会有一些问题,配置文件是 ghost 程序目录的 config.js,我们来修改下 production 字段的配置。

  • 根目录网址:配置url为你自己的url,不然会导致路径出错;
  • 增加 privacy字段,禁用后台的 google fonts 和 Gravatar (这两个国内被封):
privacy: {
    useGoogleFonts: false,
    useGravatar: false
}
  • 要禁用前台的 Google Fonts,只能手动修改模版中的 css 文件引用了。对于默认的 capser 主题,在 content/themes/capser/ 目录的 default.hbs 中。

  • 配置邮件在 mail 字段中。官方文档 中只介绍了使用 mailgun 等服务商的方式,如果是已经有了自己的邮件服务,可采用以下配置(以QQ域名邮箱为例,注意替换邮箱和密码):

mail:{
    transport: 'SMTP',
    from: 'Your Name <yourname@yourhost.com>',
    options: {
        host:'smtp.qq.com',
        secureConnection: false,
        port:25,
        auth: {
            user: 'your_email_addr',
            pass: 'your_email_password'
        }
    }
},