Octopress搭建小记

偶然看到了Octopress的介绍,我便被它惊人的载入速度和独特的离线写作模式吸引了。虽然之前的Wordpress使用的也挺不错的,但细想之下其实我还真用不到那么多功能,顿时让我有了把博客迁移到Octopress上的冲动。

网络上关于Octopress的部署教程已经有很多了,一种不错的方案是使用Github Pages服务。不过考虑到数据的安全性(比如_config.yml、一些第三方服务的API Key等),我决定同时使用Bitbucket和Github,即使用Bitbucket的私有Repo管理source,使用Github管理生成的页面。

Ruby环境的搭建以及Octopress的安装方法可以在Octopress的文档中找到,这里主要记录部署过程(Ubuntu环境)。

Octopress Documentation

首先登陆Bitbucket,选择Import repository(亦可依次按下ir),填写Github上Octopress的项目地址:

1
2
URL: [email protected]:imathis/octopress.git
Name: octopress

接下来登陆Github,以github_username.github.io为Repository name建立一个新的repo,这个将用来存放博客页面。

详见 User, Organization and Project Pages

接下来clone刚建立的repo到本地。

1
2
3
4
$ cd ~
$ git clone [email protected]:bitbucket_username/octopress.git octopress
$ cd octopress
$ git remote add octopress https://github.com/imathis/octopress

最后一条命令将Github上的官方repo添加到当前git配置中,以后可以用来升级Octopress系统。

按官方文档的方法安装好Octopress,执行以下命令。

1
2
$ rake generate
$ rake deploy

Octopress会自动生成一个_deploy文件夹,里面存放的就是用来发布到Github上的静态页面。不过因为还没有配置,所以此时是发布到了Bitbucket的gh-pages分支中。

打开Rakefile,修改deploy_branch的值为master,然后修改_deploy目录中的git配置。

1
2
$ cd _deploy
$ nano .git/config

修改[remote "origin"]中的url,将其修改为

1
git@github.com:github_username/github_username.github.io.git

保存git配置后,切换分支为master,上传文件。

1
2
3
$ git branch master
$ git checkout master
$ git push -u origin master

大约过10分钟后,访问github_username.github.io就可以看到你的博客了。

Github Pages支持绑定域名,首先在你的域名管理中,添加一个CNAME记录,指向github_username.github.io,然后执行以下命令:

1
2
3
4
$ cd ~/octopress
$ echo your_domain > source/CNAME
$ rake generate
$ rake deploy

稍候片刻,待域名信息更新后就可以通过绑定的域名访问博客了。

详见 Setting up a custom domain with Pages

至此Octopress的安装部署就完成了。以后写完日志,只需要执行:

1
2
3
4
# pwd is ~/octopress
$ rake generate
$ rake deploy
$ git push origin master

即可实现博客的更新以及Bitbucket的数据备份。