在 Ubuntu 上搭建 Ghost 博客平台

Ghost 是一款开源的博客平台,基于 Node.js,由前 WordPress UI 主管 John O’Nolan 和 WordPress 开发人员 Hannah Wolfe 创立。网上的开源博客程序众多,去年刚上线的 Ghost 来势汹汹正迅速捕获用户,0.4.1 这么早期的版本就让 Coding Horror 拥抱了 Ghost。Ghost 主题/模版越来越多,一些优秀的 WordPress 主题商,比如 WooThemes 都开始提供 Ghost 主题了

安装 Ghost 非常容易,甚至比 WordPress 还简单。下面的安装步骤在 Ubuntu 12.04.4 LTS Server 版本上测试通过。

切换到 root 账号升级和更新整个系统:

$ sudo -i

# apt-get update
# apt-get upgrade

安装 Node.js 运行环境:

# apt-get install g++ make python python-software-properties
# add-apt-repository ppa:chris-lea/node.js
# apt-get update
# apt-get install nodejs

下载、解压 Ghost 后安装:

# cd
# wget https://ghost.org/zip/ghost-0.4.1.zip
# unzip ghost-0.4.1.zip -d ghost
# cd ghost
# npm install --production

配置 Ghost 使其监听本机上的所有 IP,修改 ‘127.0.0.1’ 为 ‘0.0.0.0’:

# vi config.js
...
       server: {
            // Host to be passed to node's `net.Server#listen()`
            host: '0.0.0.0',
            // Port to be passed to node's `net.Server#listen()`, for iisnode set this to `process.env.PORT`
            port: '2368'
        }
...

使用 npm 启动 Ghost 程序:

# npm start

> ghost@0.4.1 start /root/ghost
> node index

Ghost is running in development...
Listening on 0.0.0.0:2368
Url configured as: http://my-ghost-blog.com

Ghost 的默认端口是 2368,打开浏览器访问 http://192.168.2.178:2368 就可以看到界面了:

Ghost

登录后台访问 http://192.168.2.178:2368/admin 地址:

Ghost

Ghost 是独立程序,在 nodejs 环境下可以直接运行,在 config.js 文件里修改 Ghost 的监听端口 2366 为 80 就可以了,不过在生产环境我们一般在前端加个 Nginx.

安装和配置 Nginx:

# apt-get install nginx
# rm /etc/nginx/sites-enabled/default

# vi /etc/nginx/sites-available/ghost
server {
    listen 0.0.0.0:80;
    server_name vpsee.com;
    access_log /var/log/nginx/vpsee.com.log;

    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header HOST $http_host;
        proxy_set_header X-NginX-Proxy true;

        proxy_pass http://127.0.0.1:2368;
        proxy_redirect off;
    }
}

# ln -s /etc/nginx/sites-available/ghost /etc/nginx/sites-enabled/ghost

# /etc/init.d/nginx restart

Nginx 接到请求后会 pass 给(proxy_pass)Ghost 服务程序,这时候最好把刚才测试用的 Ghost 配置修改还原成 ‘127.0.0.1’,修改后记得重启 Ghost:

# vi config.js
...
            // Host to be passed to node's `net.Server#listen()`
            host: '127.0.0.1',
...

每次 npm start 太麻烦,为了 Ghost 程序在系统启动后能自动运行,需要加入脚本到 Upstart 里:

# vi /etc/init/ghost.conf
start on startup

script
    cd /root/ghost
    npm start
end script

以后需要启动、重启或者停止 Ghost 就可以用 service ghost start/restart/stop 了:

# service ghost restart
ghost stop/waiting
ghost start/running, process 11619

相比 WordPress 的臃肿 Ghost 要轻爽得多。通过 Markdown 格式、Node.js 的实时和漂亮的界面,Ghost 给用户提供了一种更简单、更纯粹的内容写作发布方式。左边是编辑文章,右边是实时预览:

Ghost

评论 (10 Comments)

  1. 真心好啊。。
    我一直想改端口号。
    现在终于好了

  2. 请问有什么可能性导致下面这种情况?

    root@demo:/var/www/ghost# npm start

    > ghost@0.4.1 start /var/www/ghost
    > node index

    ERROR: Cannot find module ‘sqlite3’

    Error: Cannot find module ‘sqlite3’
    at Function.Module._resolveFilename (module.js:338:15)
    at Function.Module._load (module.js:280:25)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object. (/var/www/ghost/node_modules/knex/clients/server/sqlite3.js:8:15)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:364:17)

  3. 再尝试了一下,发现使用root用户安装原来失败了,所以启动不了,用普通用户做此操作就没有问题。
    请问node.js这边有什么要注意的吗?

  4. @bgsm
    貌似你系统上没有安装 sqlite,ghost 默认使用 sqlite 数据库。

  5. 我安装的时候提示了 express express-hbs sqlite3 bookshelf 分别先安装了一遍…

  6. @bgsm 你安装 sqlite3 失败了,应该是没有用国内 npm 源安装的缘故,应该是跟我一样的问题 http://www.bokeyy.com/post/ghost-install-in-vps-solution-in-china.html

  7. 请问LZ,ghost有要求vps的配置吗,我学习用的内存128M的跑不起来?

  8. @Yonlin
    貌似 ghost 没有特别要求配置,不过 256MB 内存应该够了。如果你用的是 128mb 的 openvz vps 可能会有问题,如果运行 ghost 后某个时候超出 128MB,openvz 可能会把进程杀掉~

  9. 请问lz,怎么把备案号放到ghost首页底部中间呢

  10. @大脚
    在 theme 里面,修改 ghost-0.5.6/content/themes/casper/default.hbs 文件里面的 footer 相关部分。

发表评论