综述
本文主要介绍如何利用github以及hexo搭建自己的博客,以及一些第三方插件的使用。
安装Nodejs和hexo
由于我使用的是ubuntu 16.04,debian系的系统可以采用如下方式安装:1
2wget -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
sudo apt-get install -y nodejs
然后,将npm的源换成淘宝的源:1
npm config set registry https://registry.npm.taobao.org
hexo的安装方式如下:1
npm install hexo-cli -g
创建并部署到github pages
首先创建一个github repository, 默认即可,名字可命名为xxx.github.io,名字可以根据自己的心意变更,不拘泥于形式。然后,在家目录(根据自己的习惯)下,执行以下命令:1
2
3
4mkdir xxx.github.io
cd xxx.github.io
hexo init
npm install
这样就可以初始化完毕了你的博客文件夹,此时可以执行以下命令:1
hexo clean && hexo g && hexo s
在浏览器中输入 http://localhost:4000/ 即可看到你的博客页面。那么,如何部署到github上呢?首先,需要在wxx.github.io文件夹下执行以下命令:1
npm install hexo-deployer-git --save
请确保你的电脑上安装有git,如果没有,debian系下可执行以下命令:1
sudo apt-get install git
之后,在xxx.github.io文件下打开_config.yml,进行如下配置:1
2
3
4
5
6
7
8
9
10
11
12
13
14 5 # site
6 title: your blog title # 你的网页title
7 subtitle:
8 description: your description # 你的描述
9 author: your name # 你的名字或者昵称
10 language: zh-Hans # 你的语言设置
11 timezone: Asia/Shanghai # 你的时区设置
......
77 # Deployment
78 ## Docs: https://hexo.io/docs/deployment.html
79 deploy:
80 type: git
81 repository: https://github.com/xxx/xxx.github.io.git
82 bracnch: master
将your xxx 设置成whatever you want, 然后deploy下面的type设置成git, 将repository设置成在刚才github上新建的repository,branch设置成master。
然后在xxx.github.io目录下执行1
hexo clean && hexo g && hexo d
在浏览器中输入 xxx.github.io,即可访问你的网站。
更换hexo主题
安装next主题
链接https://hexo.io/themes/列出了hexo的一些主题,你可在上面寻找自己喜欢的主题,然后更换。本文选择了应用较广的主题next,以下是其配置方法。在此之前,需要先明确两个文件,一个称之为主题配置文件,另一个称之为站点配置文件。以下xxx.github.io的文件结构:1
2
3
4
5
6
7
8
9
10xxx.github.io
├── _config.yml
├── db.json
├── node_modules
├── package.json
├── package-lock.json
├── public
├── scaffolds
├── source
└── themes
在xxx.github.io下的_config.yml称之为站点配置文件,在themes/next/文件夹下的_config.yml称之为主题配置文件夹。不用纠结自己的themes文件夹下没有next,我们现在就开始安装。打开终端,执行以下命令:1
2cd xxx.github.io
git clone https://github.com/iissnan/hexo-theme-next themes/next
同样也可以https://github.com/iissnan/hexo-theme-next下载zip文件,然后将其解压到themes文件夹下,并将其重命名为next。接着对站点配置文件,作如下配置:1
75 theme: next
此时在xxx.github.io目录下运行以下命令1
hexo clean && hexo g && hexo s
即可在浏览器下看到next主题的页面
next主题设置
选择scheme
在主题配置文件,即themes/next/_config.yml中,将Scheme进行如下设置:1
108 scheme: Pisces
设置菜单
在主题配置文件中,将menu进行如下设置:1
2
3
4
5
6menu:
home: / || home
# about: /about/ || user
tags: /tags/ || tags
categories: /categories/ || th
archives: /archives/ || archive
此时还需在xxx.github.io文件夹下执行以下命令:1
2hexo new page tags
hexo new page categories
并在xxx.github.io/source/categories下的index.md中添加以下信息:1
2type: "categories
comments: false
同理,在xxx.github.io/source/tags下的index.md中添加以下信息:1
2type: "tags"
comments: false
设置comments为false是为了让以后添加的评论功能在这两个page下不显示。
配置头像及站点标志
在主题配置文件中,修改字段avatar如下所示:1
avatar: /path/to/your/avatar
将/path/to/your/avatar设置成你的图片所在的路径,如我在xxx.github.io/source/下新建了images文件夹,并将我的图片放在其中,路径设置为/images/avatar.jpg,注意不要加source。
同样在主题配置文件中,设置favicon字段如下所示:1
2
3
4
5favicon:
small: /images/avatar.jpg
medium: /images/avatar.jpg
apple_touch_icon: /images/avatar.jpg
safari_pinned_tab: /images/avatar.jpg
集成第三方服务
关于更多主题配置和第三方服务设置,请参考链接[next主题配置]http://theme-next.iissnan.com/theme-settings.html以及[next第三方服务集成]http://theme-next.iissnan.com/third-party-services.html
Reference
本文主要参考了以下链接: