在 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