目录
  1. 1. 安装NodeJS
  2. 2. 安装hexo
  3. 3. 初始化hexo
  4. 4. 生成静态页面
  5. 5. 本地启动
  6. 6. hexo主题
  7. 7. 配置RSS订阅
  8. 8. 添加categories配置项
  9. 9. Github配置
  10. 10. 创建博客Github项目
  11. 11. hexo发布博客配置
  12. 12. 写博客流程

 零碎记录一下此博客的建立过程,基础说明如下:

  • 系统:Ubuntu 14.04 32位
  • 博客框架:hexo
  • 主题:Nadya
  • 代码仓库:Github

 基本情况就这样,以下是详细建站过程记录。。。

安装NodeJS

 因为hexo是基于NodeJS开发的,所以需要先安装NodeJS相关的环境。最方便快捷的方法是直接使用apt-get安装,如果安装失败那很可能是源的问题需要在网上找到一个合适可用的源。直接执行以下命令进行安装:

1
2
sudo apt-get install nodejs
sudo apt-get install npm

安装hexo

 如果你是采用apt-get安装的NodeJS,那么必须先要建立一个软链接。因为使用apt-get方式安装的NodeJS命令为/usr/bin/nodejs,但在安装hexo的过程中hexo是寻找/usr/bin/node命令。若不事先建立软链接,那么在安装hexo的过程会报错无法找到node命令

1
2
sudo ln -s /usr/bin/nodejs /usr/bin/node
sudo npm install -g hexo@3.0.0

初始化hexo

 安装完hexo后,建立目录用于存放博客程序并在此目录对hexo进行初始化操作

1
2
cd blog_dir && hexo init
sudo npm install

生成静态页面

 初始化hexo后,需要使用hexo g命令生成相关的静态页面文件。hexo g命令是hexo generate的简写

1
hexo g	or	hexo generate

本地启动

 使用hexo g后会在目录下生成许多博客相关的目录及文件,成功生成文件后一个默认的hexo博客就已经建好了,只需要在本地启动hexo即可浏览博客

1
hexo s	or	hexo server

hexo主题

 hexo默认使用landscape主题,可以到这里选着自己喜欢的主题并根据自己喜好对主题进行修改(需要懂点前端知识)。安装主题和后续需要使用到Github,所以先安装Github

1
sudo apt-get install git-core

 使用Github安装主题,主题将会被安装在blog_dir/themes

1
git clone <theme-gitgub-url> themes/<theme-name>

 在配置文件_config.yml中指定主题

1
theme: <theme-name>

配置RSS订阅

 hexo默认是没有RSS订阅功能的,需要自己配置。先安装hexo的RSS订阅插件hexo-generator-feed

1
sudo npm install hexo-generator-feed --save

 在<blog_dir>/_config.yml配置文件的最后添加RSS订阅的相关配置项,注意-后有一个空格

1
2
plugins:
- hexo-generator-feed

 完成后使用hexo g生成RSS订阅文件atom.xml,生成的xml文件会存放在<blog_dir>/public/atom.xml,如果存在此文件则说明成功。直接访问http://域名/atom.xml或将其嵌入到博客代码中即可

添加categories配置项

 一般使用Markdown写文章可以用hexo n "article title"命令生成一个md文件。生成的md文件会存放在<blog_dir/source/_posts目录下,默认会有如下内容:

1
2
3
4
5
title: "article title"
date: xxxx-xx-xx xx:xx:xx
tags:
toc: true
---

title即文件的标题;tags即标签,如果需要写多个标签[tag1, tag2]注意冒号后必须有一个空格;但我想要默认添加一个分类categories的配置

1
2
3
4
5
6
7
vim <blog_dir>/scaffolds/post.md
添加一行categories:,添加后:
title: {{ title }}
date: {{ date }}
categories:
tags:
---

Github配置

 写好的博客内容最终是要上传到Github的,又不想每次发布的时候都需要输入Github相关的账号及密码。所以要对Github做一些配置。首先需要有一个Github账号,没有的需要注册
配置ssh免密码登录

1
ssh-keygen -t rsa -C "注册邮箱"

 添加SSH keys

1
2
登录Github——>setting——>SSH keys——>Add SSH key
将~/.ssh/id_rsa.pub的内容复制到Github上

 测试ssh无密码连接Github

1
2
3
4
5
6
7
ssh -T git@github.com
第一次手动输入"yes"后显示如下内容则设置成功:
The authenticity of host 'github.com ' can't be established.
RSA key fingerprint is xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com' (RSA) to the list of known hosts.
Hi username! You've successfully authenticated, but GitHub does not provide shell access.

 Github账号相关配置

1
2
git config --global user.name "Github注册用户名"
git config --global user.email "Github注册邮箱"

创建博客Github项目

 需要在Github上创建一个项目用于存放博客的内容.Repository name必须是用户名.github.io

1
登录Github——>New Repository——>Repository name(必须是:用户名.github.io)

hexo发布博客配置

 想要通过hexo发布博客到Github上需要安装hexo-deployer-git插件

1
sudo npm install hexo-deployer-git --save

 配置_config.yml。repository必须写成`git@github.com:用户名/用户名.github.io.git这种形式,如果写成url形式https://github.com/用户名/用户名.github.io.git`则每次发布时都要输入Github的用户名及密码!

1
2
3
4
deploy:
type: git
repository: git@github.com:用户名/用户名.github.io.git
branch: master

 写完博客后通过命令hexo d进行发布,通过域名https://用户名.github.io/进行访问博客

写博客流程

1
2
3
4
hexo n "article name"			生成Markdown文件并用Markdown格式写博客
hexo s 本地启动hexo查看博客
hexo g 生成博客页面
hexo d 发布博客

Powered: Hexo, Theme: Nadya remastered from NadyMain