Hexo+Git=Blog 安装流程


  • hexo 环境配置流程图 hexo 环境配置

1. 安装 Git

  • 安装过 Xcode 可以直接跳过这步,因为 Xcode 自带 Git
1
2
3
4
 // 通过 ruby 安装 HomeBrew (已安装过HomeBrew的可以直接执行下一行)
 $ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

 $ brew install git   // 通过 HomeBrew 安装Git

也可以通过下载Git安装程序来安装

1.1 注册账户

首先需要注册一个Github的账户,已有的可以直接跳过去下一步了。注册github

1.2 创建仓库

登入进去之后再界面的右上角点那个加号,创建一个仓库(new repository)来存放我们的博客网站。Respository name 中 填写 username.github.io ,这里的 username.github.io 就是你以后的博客域名,这个 username 可以认真想一个了。创建github仓库

2. 安装 nvm

1
git clone https://github.com/creationix/nvm.git ~/.nvm
  • 配置终端启动时自动执行 source ~/.nvm/nvm.sh 否则每次打开终端都要配置一下nvm环境。在 ~/.bashrc, ~/.bash_profile, ~/.profile, 或 ~/.zshrc 随便哪一个文件内添加一行配置命令就可以。vim .profile打开配置文件,在配置文件内添加下面这一行代码,保存退出。
1
source ~/.nvm/nvm.sh
  • 查看本地是否安装成功,终端输入nvm显示如下信息说明安装成功。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
Node Version Manager

Note: <version> refers to any version-like string nvm understands. This includes:
  - full or partial version numbers, starting with an optional "v" (0.10, v0.1.2, v1)
  - default (built-in) aliases: node, stable, unstable, iojs, system
  - custom aliases you define with `nvm alias foo`

******

Note:
  to remove, delete, or uninstall nvm - just remove the `$NVM_DIR` folder (usually `~/.nvm`)

3. 安装 node.js

  • 输入nvm ls看到如下信息说明还没有安装任何 node.js
1
2
3
	N/A
node -> stable (-> N/A) (default)
iojs -> N/A (default)
  • 输入nvm ls-remote查看可供安装的 node.js 和 iojs的版本
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
	......
    iojs-v3.0.0
    iojs-v3.1.0
    iojs-v3.2.0
    iojs-v3.3.0
    iojs-v3.3.1
	......
         v5.9.1
        v5.10.0
        v5.10.1
        v5.11.0
        v5.11.1
         v6.0.0
         v6.1.0
         v6.2.0
  • 选择需要安装的版本执行安装nvm install v6.2.0 这里可以安装多个版本
1
2
3
4
5
6
7
8
Rango-MBP:nvm rango$ nvm install v6.2.0
Downloading https://nodejs.org/dist/v6.2.0/node-v6.2.0-darwin-x64.tar.gz...
#
#####################
#######################################
######################################################################## 100.0%
Now using node v6.2.0 (npm v3.8.9)
Creating default alias: default -> v6.2.0
  • 查看一下,显示如下信息,安装成功
1
2
3
4
5
6
Rango-MBP:nvm rango$ nvm ls
->       v6.2.0
default -> v6.2.0
node -> stable (-> v6.2.0) (default)
stable -> 6.2 (-> v6.2.0) (default)
iojs -> N/A (default)
  • 顺便安装一下 iojs 不安装也不影响使用
1
2
3
4
5
6
7
Rango-MBP:nvm rango$ nvm install iojs-v3.3.1
Downloading https://iojs.org/dist/v3.3.1/iojs-v3.3.1-darwin-x64.tar.gz...
#
#####################
#######################################
######################################################################## 100.0%
Now using io.js v3.3.1 (npm v2.14.3)
  • 查看一下,是否安装成功
1
2
3
4
5
6
7
Rango-MBP:nvm rango$ nvm ls
->  iojs-v3.3.1
         v6.2.0
default -> v6.2.0
node -> stable (-> v6.2.0) (default)
stable -> 6.2 (-> v6.2.0) (default)
iojs -> iojs-v3.3 (-> iojs-v3.3.1) (default)
  • 更改需要使用的 nvm 版本(如果没有安装 iojs 则不需要,其实 iojs 和 node.js 安任何一个应该都是可以的)
1
2
Rango-MBP:nvm rango$ nvm use v6.2.0
Now using node v6.2.0 (npm v3.8.9)
  • 查看一下,更改成功
1
2
3
4
5
6
7
Rango-MBP:nvm rango$ nvm ls
    iojs-v3.3.1
->       v6.2.0
default -> v6.2.0
node -> stable (-> v6.2.0) (default)
stable -> 6.2 (-> v6.2.0) (default)
iojs -> iojs-v3.3 (-> iojs-v3.3.1) (default)

安装了 node.js 之后 npm 也就安装好了。(它们之间的关系我还不太清晰)

4. 安装 Hexo 了!!!

  • 输入sudo npm install -g hexo-cli开始[-g 全局]安装 hexo(中间有几个警告,好像是因为我之前系统里安装过)(前方高能…)
  • 如果遇到 npm err 可以试试切换 node.js 版本
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
Rango-MBP:nvm rango$ sudo npm install hexo -g
loadRequestedDeps → netwo ▐ ╢██████░░░░░░░░░░░░░░░░░░░░░░░░╟
Rango-MBP:nvm rango$ npm install hexo -g
npm WARN deprecated cross-spawn-async@2.2.4: cross-spawn no longer requires a build toolchain, use it instead!

**********

  └─┬ warehouse@2.2.0
    ├─┬ cuid@1.3.8
    │ ├── browser-fingerprint@0.0.1
    │ ├── core-js@1.2.6
    │ └── node-fingerprint@0.0.2
    ├─┬ is-plain-object@2.0.1
    │ └── isobject@1.0.2
    └─┬ JSONStream@1.1.1
      ├── jsonparse@1.2.0
      └── through@2.3.8

看到上面这一大坨应该就是安装好了吧。。。 经过部落测试发现直接用npm安装话经常出现卡住而导致无法正常安装,解决办法就是修改npm的安装源,以下三种方法任选一个即可(正常的朋友不用执行这一步)。 1.通过config命令: npm config set registry https://registry.cnpmjs.org npm info underscore (如果上面配置正确这个命令会有字符串response) 2.命令行指定 npm --registry https://registry.cnpmjs.org info underscore 3.编辑 ~/.npmrc 加入下面内容 registry = https://registry.cnpmjs.org

  • 查看一下,嗯,有了,说明安装好了
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
Rango-MBP:nvm rango$  hexo
Usage: hexo <command>

Commands:
  help     Get help on a command.
  init     Create a new Hexo folder.
  version  Display version information.

Global Options:
  --config  Specify config file instead of using _config.yml
  --cwd     Specify the CWD
  --debug   Display all verbose messages in the terminal
  --draft   Display draft posts
  --safe    Disable all plugins and scripts
  --silent  Hide output on console

For more help, you can use 'hexo help [command]' for the detailed information
or you can check the docs: http://hexo.io/docs/
  • 查看安装的 hexo 版本
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
Rango-MBP:nvm rango$ hexo version
hexo-cli: 1.0.1
os: Darwin 15.5.0 darwin x64
http_parser: 2.7.0
node: 6.2.0
v8: 5.0.71.47
uv: 1.9.1
zlib: 1.2.8
ares: 1.10.1-DEV
icu: 57.1
modules: 48
openssl: 1.0.2h
  • 更新 hexo npm update hexo -g 这一步看心情,更不更都可以,以后更新hexo可以用这个,也可以用上面的安装方法,安装最新的版本,完全看心情看需要了。

进行到这里 hexo 和 github 仓库都有了,下面就可以生成博客,部署到 github 仓库发布了。

5. 初始化本地文件夹

  • 输入 hexo init username会在主目录下(如果不想在主目录下,也可以 cd 到你希望的文件夹下再执行这个初始化操作)创建一个 username 文件夹,并初始化(这里的 username 就是想要初始化的文件夹名称,需要什么名称就写什么名称),。输入下面的命令之后又是一大坨信息,最后看到INFO Start blogging with Hexo!就说明初始化成功了。
1
2
3
4
5
6
Rango-MBP:~ rango$ hexo init username
INFO  Cloning hexo-starter to ~/username
Cloning into '/Users/rango/username'...
......
......
INFO  Start blogging with Hexo!

6. 更改配置

为了使博客不太难看,这里选择安装一个主题,切换到刚刚生成的目录下cd username,安装主题。

1
git clone https://github.com/iissnan/hexo-theme-next themes/next

这里安装的是一个极简主题next,当然你也可以安装其他主题,Hexo也有其他主题可供选择。 安装好了之后,需要配置一下,找到username/_config.yml 修改几个键值对,下面列出必须要改的几个值,其他的配置请参考官方设置文档

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# Site
title: CodeNight //博客的名字
author: Rango_Lee //你的名字
language: zh-Hans //语言 中文

theme: next   //刚刚安装的主题名称

deploy:
	type: git    //使用 Git 发布
	repo: https://github.com/username/username.github.io.git    // 刚创建的Github仓库

接下来就是主题配置了,配置文件在 username/themes/next/_config.yml 中修改,这里就不一一配置了,详细配置可以参考官方配置文档

7. 本地测试及发布

  • 到了这里所有的基础框架都已经创建完成了,下面来发布我们的博客吧。 输入命令cd username/source/_posts跳转到这个文件夹_posts这个文件就是以后存放.md文件的地方。(用 Markdown 写.md 博客文档) 默认在 username/sources/_posts/下会有一篇 hellWorld.md 博文。
  • 启动本地测试,你可以在浏览器中输入 https://localhost:4000 来访问,( 在终端状态 control+c 结束测试 )
1
hexo s --debug 
  • 安装 Git 自动部署工具
1
npm install hexo-deployer-git --save
  • 发布测试没有问题之后,就可以生成静态网页,然后发步到我们的 github pages 中了。
1
hexo clean && hexo g && hexo d
  • 如果这是你第一次使用 Git 部署,终端会需要你输入 Github 的邮箱和密码,正确输入后,稍等一会会看到刷刷的进度,最后看到下面的信息INFO Deploy done: git,就说明你的博客已经上传至Github 了。
1
INFO  Deploy done: git

以后在 _posts 文件夹下写好.md文件也只需要 在终端 切换到这个文件夹下然后执行上面这行命令就可以发布博客了。 到这里,你的博客已经完成,在浏览器中输入 http://rangolee.github.io 就能够访问了。


生成SSH秘钥
1.设置Git的user name和email:
1
2
$ git config --global user.name "yourname"
$ git config --global user.email "youremailaddress"
2.生成SSH密钥过程:
  • 查看是否已经有了ssh密钥 ~cd ~/.ssh 如果没有密钥则不会有此文件夹,有则备份删除(如果里面没有id_rsa和id_rsa.pub不删除也无所谓)
  • 生成密钥:
1
Rango-MBP:~ Rango$ ssh-keygen -t rsa -C "848***256@qq.com"
  • 按3个回车,密码为空。
1
2
3
4
Your identification has been saved in /Users/Rango/.ssh/id_rsa.
Your public key has been saved in /Users/Rango/.ssh/id_rsa.pub.
The key fingerprint is:
......
  • 最后得到了两个文件:id_rsaid_rsa.pub
  • 在github上添加ssh密钥,这要添加的id_rsa.pub里面的公钥。打开https://github.com/ 登录,添加
  • 测试:ssh git@github.com