使用hexo向github发布自己的blog

  • 准备:使用hexo在github上搭建博客需要以下东西:
  1. git,下载安装即可

  2. node,下载安装即可,如果自己用压缩包安装,注意配置好环境变量。

  3. 通过node安装好hexo。

  • 安装hexo
    在装好node才可以安装hexo
    1
    npm install -g hexo

创建hexo文件夹即自己的blog项目文件夹,切换到目录下,按住shirt键并右击,可以看到《在此处打开命令窗口》,点击进入输入下面命令:

1
hexo init

运行完后,可以看到建立网站所需要的所有文件,至此,全部安装工作已经完成!blog就是你的博客根目录,所有的操作都在里面进行。
生成静态页面

1
hexo generate

本地启动:
启动本地服务,进行文章预览调试,命令:

1
hexo server

浏览器输入http://localhost:4000,这样本地博客已经建立好了,如何才能发布到github上呢,首先我们的配置下我们githu的信息,hexo配置github的信息在文件夹的_config.yml这个文件里面,打开文件我们可以看到以下内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# Hexo Configuration
## Docs: https://hexo.io/docs/configuration.html
## Source: https://github.com/hexojs/hexo/
# Site

title: CallteFoot's blog

subtitle:

description:

author: John Doe

language:

timezone:

# URL

## If your site is put in a subdirectory, set url as 'http://yoursite.com/child' and root as '/child/'

url: https://cattlefoot.github.io/

root: /

permalink: :year/:month/:day/:title/

permalink_defaults:

# Directory

source_dir: source

public_dir: public

tag_dir: tags

archive_dir: archives

category_dir: categories

code_dir: downloads/code

i18n_dir: :lang

skip_render:

# Writing

new_post_name: :title.md # File name of new posts

default_layout: post

titlecase: false # Transform title into titlecase

external_link: true # Open external links in new tab

filename_case: 0

render_drafts: false

post_asset_folder: false

relative_link: false

future: true

highlight:

enable: true

line_number: true

auto_detect: false

tab_replace:

# Category & Tag

default_category: uncategorized

category_map:

tag_map:

# Date / Time format

## Hexo uses Moment.js to parse and display date

## You can customize the date format as defined in

## http://momentjs.com/docs/#/displaying/format/

date_format: YYYY-MM-DD

time_format: HH:mm:ss

# Pagination

## Set per_page to 0 to disable pagination

per_page: 10

pagination_dir: page

theme: landscape

deploy:

type: git

#我的github项目地址:https://github.com/CattleFoot/CattleFoot.github.io.git

repo: https://{username}:{password}@github.com/CattleFoot/CattleFoot.github.io.git

#注意上面这个,要不发布到github时会要输账号、密码的,反正我是一直输错的,哭死。

branch: master

message: hexo build blog

接下来就的配置hexo发布github服务依赖

1
npm install hexo-deployer-git --save

这个安装好了,基本就可以发布自己的博客到github上了,但是还缺少了ssh登陆认证。

生成ssh密码,主要目的是使github和发布机器建立信任关系,在生产密钥前先运行下ssh-keygen是否全局命令,如果不是需要在环境变量中配置下,其所在的目录是Git\usr\bin,即git安装目录下的usr下的bin文件夹下。

ssh-keygen -t rsa -C “你的邮箱地址”,按3个回车,密码为空。

在C:\Users\Administrator.ssh下,得到两个文件id_rsa和id_rsa.pub。

生成好了之后到https://github.com/settings/ssh点击Add SSH key,并将id_rsa.pub内容粘贴进去。

  • 发布博客
    如果上面一切都搞定,接着就可以发布自己的博客到github啦:
    1
    hexo deploy

最后到github就可以看到自己的博客文件资源啦,然后可以进入自己的blog看看,如我的blog地址:https://cattlefoot.github.io/。

参考文献: