Linux ·

Debian 8.x / Ubuntu 16.04.x 搭建 Ghost博客教程

Ghost 是一款使用 Node.js 开发的博客系统,相对于使用 PHP 开发的 WordPress 更轻巧友好,所以本站已经从 WordPress 切换至 Ghost,本文介绍在 Debian 8.x 和 Ubuntu 16.04 下搭建 Ghost 的教程

本文所有操作均在 root 用户下进行,请自行切换

首先,更新系统

apt-get update && apt-get upgrade

如果您用的 Debian 8.x 开启了 backports 也可以更新下

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

1、安装 Node.js 6.x LTS

由于系统自带的 Node.js 较老,这里我们采用 NodeSource 编译的 Node.js 源

Ubuntu 下

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

Debian 下

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

2、安装 Nginx

Ubuntu 下可以用 PPA

Debian 下可以用 Dotdeb

然后安装 Nginx 以及一些必要的软件

apt-get install nginx unzip wget curl sudo sqlite3

3、下载 Ghost

这里我们演示把 Ghost 下载并解压在 /var/www/ghost 目录

cd /var/www/ && wget https://ghost.org/zip/ghost-latest.zip && unzip ghost-latest.zip -d ghost && rm -rf ghost-latest.zip

4、添加 ghost 用户并修改权限

useradd ghost
chown -R ghost:ghost /var/www/ghost

5、修改 config.js

cd /var/www/ghost
cp -r config.example.js config.js

接着就可以修改 config.js 按照实际情况,修改 config 段,举例如下

config = {
    // ### Production
    // When running Ghost in the wild, use the production environment.
    // Configure your URL and mail settings here
    production: {
        url: 'http://example.com', //修改为你博客的域名,如需要启动 SSL 则改为 https
        mail: {},
        database: {
            client: 'sqlite3',
            connection: {
                filename: path.join(__dirname, '/content/data/ghost.db')
            },
            debug: false
        },

        server: {
            host: '127.0.0.1',
            port: '2368'
        }
    },

如果需要后台发送邮件给用户,您可以配置 mail 参数,例如:

mail: {
      transport: 'SMTP',
      options: {
          service: 'Mailgun',
          auth: {
              user: '', // mailgun username
              pass: ''  // mailgun password
          }
      }
  },

Ghost 用的是 Nodemailer,如果您需要其他服务商的 service 名字,可以在官网文档里查看。

6、安装并启动 Ghost

cd /var/www/ghost
npm install --production
npm start --production

如果在 Ubuntu 16.04 下提示

Error: Cannot find module '/var/www/ghost/node_modules/sqlite3/lib/binding/node-v48-linux-x64/node_sqlite3.node'

则需要安装 nodejs 的 sqlite3

npm install sqlite3 --save

可以在本机上打开 http://127.0.0.1:2368/ 查看在线版本

7、配置 Nginx

按 ctrl + c 停止 Ghost ,然后我们可以修改 Nginx 配置文件,举例如下

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

检查并重新加载 nginx

nginx -t
nginx -s reload

8、安装 PM2 使 Ghost 保持后台运行

PM2 是一款很流行的 Node.js 进程管理器,可以做到开机启动和重新关闭等操作

首先我们通过 npm 安装 PM2

cd /var/www/ghost
npm install pm2 -g

配置当前环境,并设置开启启动,然后保存

NODE_ENV=production pm2 start index.js --name ghost
pm2 startup
pm2 save

后续可能需要用到的命令有

启动 Ghost

pm2 start ghost

停止 Ghost

pm2 stop ghost

重启 Ghost

一般用于修改了主题文件或进程隔屁的情况

pm2 restart ghost

好了,大功告成,打开浏览器访问 http://www.linuxidc.com/ghost/ 注册的第一个账号就是全站管理员,接着就可以开始 Ghost 的博客之旅了。

参与评论