/ ghost

如何愉快地搭建一个 Ghost 博客

如果你是新手,那么非常建议你去看 Ghost 的官方搭建文档,或者民间的中文版,里面详细的搭建步骤,基于 Windows/Linux 的都有。当然如果你碰到一些问题,那么可以找找本文章有没有能帮助到你的东西——因为本文正是基于我的 Case 写的:Linux + Apache + Ghost 还有绑定域名,当然里面很多内容都是参考官方的文档。

还有一点就是我已经假设你有基本的 Linux 知识。

安装 Apache

sudo apt-get install apache2

Apache 的配置文件位于 /etc/apache2 的 apache2.conf。

另外这是一些常用的命令:

重启 Apache2 服务:

sudo service apache2 restart

启用模块:

sudo a2enmod [mod to be enabled]

安装 Nodejs

这里是官方的安装文档

这里就不阐述。

安装 Ghost

使用下面命令下载 Ghost:

curl -L https://ghost.org/zip/ghost-latest.zip -o ghost.zip

解压:

unzip -uo ghost.zip -d ghost

切换到解压的目录(可以根据需要 Move 位置):

cd /your directory.

安装 Ghost:

npm install --production

然后在当前目录下启动 Ghost:

npm start

这时候,如果你能启动 Ubunto 的浏览器,那么输入

127.0.0.1:2368 就能访问 Ghost 博客了。

配置 Ghost

进入你的 Ghost 目录,找到 config.js 这个文件,把相应的模式(production/development/testing 等 )下的 server: host 修改为你服务器的 IP 的地址,就能从这个 IP 的2368 端口进入 Ghost 博客。

初始使用 Ghost 的话,可以进入 [ip address]/ghost 进入管理员模式,注册一个邮箱激活,就能进入你自己 Ghost 的后台了。

有一点值得注意:

在改动 Apache 和 Ghost 的配置文件后,要重启 Apache 和 Ghost 才能生效。

要让 Ghost 一直处于运行状态(npm start 这种启动方式,在断开与终端的连接和 ctrl+c 后 Ghost 就会关闭)的话,先安装 node 的 forever 模块:

npm install forever -g

然后以 forever 的形式运行 Ghost:

NODE_ENV=production forever start index.js

可以通过这样停止 Ghost

forever stop index.js

可以通过这样来查看 forever 形式运行的列表

forever list

设置反向代理

先确保你的 apache 启动了代理模块:

sudo a2enmod proxy

sudo a2enmod proxy_http

进入配置目录:

cd /etc/apache2/sites-available

新建配置文件 ghost.conf 然后把以下内容拷贝到新文件保存:

<VirtualHost *:80>
     ServerName example.net
     ProxyRequests off
     ProxyPreserveHost On

     ProxyPass /example2 !
     ProxyPass / http://localhost:2368/
     
</VirtualHost>

注意,example.net 是你服务器 IP 绑定的域名。

ProxyPass / http://localhost:2368/ 的意思是,example.net/ 下的所有都会被转发到 localhost:2368 ,所以如果你像我一样,在 apache 设置的站点根目录 /var/www 下有需要访问的内容,可以通过加上 **ProxyPass /example2 ! ** 去禁止 /example2 下的被反向代理。

设置代码高亮

Haighlight.js 是一个自动代码高亮的小脚本,使用起来也非常简单。

打开 ghost/content/themes/casper/default.hbs 文件,在 <body> 之前嵌入 css :

<link href="http://cdn.bootcss.com/highlight.js/8.0/styles/monokai_sublime.min.css" rel="stylesheet"> 

<body> 结束之前嵌入脚本:

<script src="http://cdn.bootcss.com/highlight.js/8.0/highlight.min.js"></script>
<script>
    $(document).ready(function() {
        $('pre code').each(function(i, block) {
            hljs.highlightBlock(block);
        });
    });
</script>

OK,ALL DONE.