hexo

对于很多小伙伴来说,看到别人写的漂亮的博客,自己肯定时特别也想搞一搞,但是又想到还要自己去买服务器,去部署就特别麻烦,而且还要自己去写页面样式,总之很烦。但是,令人兴奋的事,现在我们有了hexo,有了github和码市,这让我们搭建一个属于自己的博客小站不再是个梦想。

安装前准备

首先,我们需要安装nodejs环境,需要安装git,还需要你在码市和github上注册一个账号。

正式安装

先在目录下创建blog目录,然后安装hexo。

1
sudo npm install -g hexo

初始化hexo

1
hexo init

生成页面

1
hexo generate

启动本地服务

1
hexo server

然后启动浏览器,输入localhost:4000,我们就可以看到我们的博客服务在本地运行起来了,默认用的主题使用的是landscape。

关联github

首先我们到我们的github上创建一个repository,创建的时候注意Repository的名字。比如我的Github账号是angelen10,那么我应该创建的Repository的名字是:angelen10.github.io。
然后,我们再到码市上创建一个repository。最后,在博客根目录下找到_config.yml文件关联这两个仓库。

1
2
3
4
5
6
7
# Deployment
## Docs: https://hexo.io/docs/deployment.html
deploy:
type: git
repo:
coding: git@git.coding.net:tinylin/blog.git,master
github: git@github.com:Samlin901211/Samlin901211.github.io.git,master

添加SSH key

为了避免每次更新博客都要输入github密码,我们可以添加SSH key

1
2
3
4
root@tinylin:/usr/blog ssh-keygen -t rsa -C "your_email@example.com"
root@tinylin:/usr/blog# cd /root/.ssh
root@tinylin:~/.ssh# ls
id_rsa id_rsa.pub known_hosts

复制id_rsa.pub到github,title随便起。

复制id_rsa.pub到码市。

测试是否生效

1
2
3
4
5
6
7
$ ssh -T git@github.com 或者 git@git.coding.net
The authenticity of host 'github.com (192.30.252.129)' can't be established.
RSA key fingerprint is 16:27:xx:xx:xx:xx:xx:4d:eb:df:a6:48.
Are you sure you want to continue connecting (yes/no)? yes #确认你是否继续联系,输入yes
Warning: Permanently added 'github.com,192.30.252.129' (RSA) to the list of known hosts.
Enter passphrase for key '/c/Users/xxxx_000/.ssh/id_rsa': #生成ssh kye是密码为空则无此项,若设置有密码则有此项且,输入生成ssh key时设置的密码即可。
Hi xxx! You've successfully authenticated, but GitHub does not provide shell access. #出现词句话,说明设置成功。

如果发现配置无效,仍然要输密码,修改根目录下的.deploy_git下的.git下的配置config的remote为ssh访问

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
老的配置
 [core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[branch "master"]
remote = https://github.com/Samlin901211/Samlin901211.github.io.git
merge = refs/heads/master
新的配置
 [core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[branch "master"]
remote = git@github.com:Samlin901211/Samlin901211.github.io.git
merge = refs/heads/master

更换主题

我们选用next主题,这个主题文档比较全,github地址文档地址
配置根目录下的全局配置_comfig.yml,一些主题相关的配置请查阅文档

1
theme: next

添加评论

我选用的是多说评论,之所以选他,主要还是因为他是我们国内的,访问速度会快些,当然它还不收费,是我们这些草根一族的首选。
我们先进入多说首页,点击我要“安装”,最后获取到duoshuo_shortname,找到我们主题根目录下的配置文件_config.yml,修改duoshuo_shortname配置

1
2
# Duoshuo ShortName
duoshuo_shortname: tinylin

添加百度统计

登陆百度统计,点击进入“网站中心”,的代码获取。

在主题根目录的配置文件_config.yml中添加baidu_tongji

1
baidu_tongji: true

找到head文件,我是用的是next主题,故目录在layout/_partial下,把下面这段代码复制进去,百度统计就配置好了。

1
2
3
4
5
6
7
8
9
10
11
{% if theme.baidu_tongji %}
<script>
var _hmt = _hmt || [];
(function() {
var hm = document.createElement("script");
hm.src = "//hm.baidu.com/hm.js?*********";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
})();
</script>
{% endif %}

添加阅读统计

注册leanCloud,登陆之后创建一个应用,点击应用边上的“存储”,创建名为Counter的Class。


进入“设置”应用key,获取到App ID App Key Master Key

在主题根目录的配置文件_config.yml中添加leanCloud信息:

1
2
3
4
5
6
# Show number of visitors to each article.
# You can visit https://leancloud.cn get AppID and AppKey.
leancloud_visitors:
enable: true
app_id: ***************
app_key: ***************

设置安全Web安全域名,保证了统计数据只在我们设置的域名下才能被访问