[关闭]
@WillireamAngel 2018-04-24T12:21:22.000000Z 字数 6297 阅读 1542

Hexo+GitHub搭建个人博客

Blog


前言

在网上找了很多教程,也尝试了很多,最后利用Hexo+GitHub搭建了个人博客,以此记录。

准备

系统环境:Windows/Linux/macOS(推荐Windows)
注册准备:GitHub
GitHub:https://github.com/
软件安装:Git + Node.js
Git:https://git-scm.com/
Node.js:http://nodejs.cn/

环境部署

  1. 下载安装Git
    网址:https://git-scm.com/downloads
    注意:以Windows环境为例,一直”下一步”到安装完成,可以右键看到Git Bash Here和Git GUI Here。

  2. 下载安装Node.js
    网址:http://nodejs.cn/download/

  3. 注册GitHub
    网址:https://github.com/join?source=header-home
    注意:用户名和邮箱是常用的,需要以后各种链接都会建立在用户名和邮箱上,所以确定好后尽量不要修改。

  4. 新建项目库
    网址:https://github.com/new
    注意:每个帐号只能有一个仓库来存放个人主页,而且仓库的名字必须是[username]/[username].github.io(大小写变化无影响),这是特殊的命名约定,你可以通过http://[username].github.io 来访问你的个人主页。

  5. 生成SSH key
    上传文件需要配置ssh key,不然无法上传,需要先查看本地是否含有SSH Key,若含有,则需要将本地的Key删除。
    检查本地SSH key:

  1. ls -al ~/.ssh

设置user name和email(设置常用即可,无需与GitHub信息相同):

  1. git config --global user.name "<your name>"
  2. git config --global user.email "<your email>"

生成SSH key(一直Enter,可不设置密码,设置密码需记住):

  1. ssh-keygen -t rsa -C "<your GitHub email>"

生成后,可在~/.ssh/下生成了两个文件id_rsa 和 id_rsa.pub。

6.链接Git和GitHub
本地Git和远程GitHub通过SSH key链接,需要在GitHub上添加SSH key。
网址:https://github.com/settings/keys
查看本地SSH key:

  1. cat ~/.ssh/id_rsa.pub

添加SSH Key(命名随意,语义化即可)

SSH Key

测试连接:

  1. ssh git@github.com

结果大致如下格式:

  1. $ ssh git@github.com
  2. PTY allocation request failed on channel 0
  3. Hi WillireamAngel! You've successfully authenticated, but GitHub does not provide shell access.
  4. Connection to github.com closed.

搭建博客

1.安装Hexo

  1. npm install -g hexo-cli

2.初始化建站

  1. hexo init <your blog folder>
  2. cd <your blog folder>
  3. npm install

新建完成后,指定文件夹的目录如下:

  1. .
  2. ├── _config.yml
  3. ├── package.json
  4. ├── scaffolds
  5. ├── source
  6. | ├── _drafts
  7. | └── _posts
  8. └── themes

查看应用程序如下(可按需移除,一般可不动):

  1. $ cat package.json
  2. {
  3. "name": "hexo-site",
  4. "version": "0.0.0",
  5. "private": true,
  6. "hexo": {
  7. "version": "3.4.2"
  8. },
  9. "dependencies": {
  10. "hexo": "^3.2.0",
  11. "hexo-deployer-git": "^0.3.1",
  12. "hexo-generator-archive": "^0.1.4",
  13. "hexo-generator-category": "^0.1.3",
  14. "hexo-generator-index": "^0.2.0",
  15. "hexo-generator-tag": "^0.2.0",
  16. "hexo-renderer-ejs": "^0.3.0",
  17. "hexo-renderer-marked": "^0.3.0",
  18. "hexo-renderer-stylus": "^0.3.1",
  19. "hexo-server": "^0.2.0"
  20. }
  21. }

3.配置_config.yml
网址:https://hexo.io/zh-cn/docs/configuration.html
编辑_config.yml如下:

  1. # Hexo Configuration
  2. ## Docs: http://hexo.io/docs/configuration.html
  3. ## Source: https://github.com/hexojs/hexo/
  4. # Site 这下面的几项配置都很简单,你看我的博客就知道分别是什么意思
  5. title: blog name #博客名
  6. subtitle: ********** #副标题
  7. description: ******************* #用于搜索,没有直观表现
  8. author: authorname #作者
  9. language: zh-Hans #语言
  10. timezone: #时区,此处不填写,hexo会以你目前电脑的时区为默认值
  11. # URL 暂不配置,使用默认值(用于绑定网址)
  12. ## If your site is put in a subdirectory, set url as 'http://yoursite.com/child' and root as '/child/'
  13. url: http://yoursite.com
  14. root: /
  15. permalink: :year/:month/:day/:title/
  16. permalink_defaults:
  17. # Directory 暂不配置,使用默认值
  18. source_dir: source
  19. public_dir: public
  20. tag_dir: tags
  21. archive_dir: archives
  22. category_dir: categories
  23. code_dir: downloads/code
  24. i18n_dir: :lang
  25. skip_render:
  26. # Writing 文章布局等,使用默认值
  27. new_post_name: :title.md # File name of new posts
  28. default_layout: post
  29. titlecase: false # Transform title into titlecase
  30. external_link: true # Open external links in new tab
  31. filename_case: 0
  32. render_drafts: false
  33. post_asset_folder: false
  34. relative_link: false
  35. future: true
  36. highlight:
  37. enable: true
  38. line_number: true
  39. tab_replace:
  40. # Category & Tag 用于文章标识,按需配置
  41. default_category: uncategorized
  42. category_map:
  43. tag_map:
  44. # Date / Time format 时间格式,使用默认值
  45. ## Hexo uses Moment.js to parse and display date
  46. ## You can customize the date format as defined in
  47. ## http://momentjs.com/docs/#/displaying/format/
  48. date_format: YYYY-MM-DD
  49. time_format: HH:mm:ss
  50. # Pagination
  51. ## Set per_page to 0 to disable pagination
  52. per_page: 10 #每页显示的文章数,0表示不分页
  53. pagination_dir: page
  54. # Extensions 插件配置,暂时不配置
  55. ## Plugins: http://hexo.io/plugins/
  56. ## Themes: http://hexo.io/themes/
  57. plugins:
  58. - hexo-generator-feed
  59. theme: next #使用的主题
  60. feed: #之后配置rss会用,此处先不配置这个
  61. type: atom
  62. path: atom.xml
  63. limit: 20
  64. # Deployment 用于部署到github,
  65. ## Docs: http://hexo.io/docs/deployment.html
  66. deploy:
  67. type: git
  68. repo:ssh://git@github.com/[githubname]/[库名].github.io
  69. branch: master

4.调试
网址:https://hexo.io/zh-cn/docs/commands.html

  1. hexo s --debug

命令行输出:

  1. ...
  2. INFO Hexo is running at http://0.0.0.0:4000/. Press Ctrl+C to stop.

此时浏览器输入http://localhost:4000/可预览界面,按Ctrl+C可停止调试。

配置主题

HEXO开发者有很多主题可供选择,也有很多特效可以实现,具体可查看网址(本教程以安装next主题为例)。
网址:
- https://hexo.io/themes/
- http://theme-next.iissnan.com/
注:站点配置文件为./_config.yml,主题配置文件为./themes/next/_config.yml。
1.安装next

  1. cd your-hexo-site #进入Blog文件夹
  2. git clone https://github.com/iissnan/hexo-theme-next themes/next

2.站点配置文件启用主题(参见上述配置)

  1. theme: next

3.调试验证

  1. hexo s --debug

4.4.主题设定
Scheme 是 NexT 提供的一种特性,借助于 Scheme,NexT 为你提供多种不同的外观。同时,几乎所有的配置都可以 在 Scheme 之间共用。目前 NexT 支持三种 Scheme:
- Muse - 默认 Scheme,这是 NexT 最初的版本,黑白主调,大量留白
- Mist - Muse 的紧凑版本,整洁有序的单栏外观
- Pisces - 双栏 Scheme,小家碧玉似的清新
以选择 Pisces Scheme为例,修改如下:

  1. #scheme: Muse
  2. #scheme: Mist
  3. scheme: Pisces

5.站点配置文件设定语言

  1. language: zh-Hans

目前Hexo支持语言可参照官网:
http://theme-next.iissnan.com/getting-started.html
6.设置菜单
启用即可,更多说明参见官网。
示例:

  1. menu:
  2. home: /
  3. archives: /archives
  4. #about: /about
  5. #categories: /categories
  6. tags: /tags
  7. #commonweal: /404.html

7.设置头像
将图片防置在主题目录下,或使用网络图片链接。修改avator,为站点内图片,如:

  1. avatar: /images/avatar.png

也可为网络图片,如:

  1. avatar: http://example.com/avatar.png

8.设置favicon
按要求上传图片到主题文件夹即可。

  1. favicon:
  2. small: /images/favicon-16x16-next.png
  3. medium: /images/favicon-32x32-next.png
  4. apple_touch_icon: /images/apple-touch-icon-next.png
  5. safari_pinned_tab: /images/logo.svg

9.其他功能
设置评论区:next内置数个评论站点,按要求设置即可。
设置首页文章预览:

  1. auto_excerpt:
  2. enable: true

其它参见官网和谷歌百度。
10.bug留意
- 在社区账号设置后会产生main.css生成错误,Social Links栏目按需开启(需处理bug)。

  1. # Social Links.
  2. # Usage: `Key: permalink || icon`
  3. # Key is the link label showing to end users.
  4. # Value before `||` delimeter is the target permalink.
  5. # Value after `||` delimeter is the name of FontAwesome icon. If icon (with or without delimeter) is not specified, globe icon will be loaded.
  6. #social:
  7. #GitHub: https://github.com/yourname || github
  8. #E-Mail: youremail || envelope
  9. #Google: https://plus.google.com/yourname || google
  10. #Twitter: https://twitter.com/yourname || twitter
  11. #FB Page: https://www.facebook.com/yourname || facebook
  12. #VK Group: https://vk.com/yourname || vk
  13. #StackOverflow: https://stackoverflow.com/yourname || stack-overflow
  14. #YouTube: https://youtube.com/yourname || youtube
  15. #Instagram: https://instagram.com/yourname || instagram
  16. #Skype: skype:yourname?call|chat || skype
  17. social_icons:
  18. enable: false
  19. icons_only: false
  20. transition: false

常规使用

1.新建文章

  1. hexo new [title]

此时会有新文章显示在[blog-path]\source\_posts
文章前缀信息如下:

  1. ---
  2. title: HEXO
  3. date: 2018-04-24 19:52:25
  4. tags:
  5. - Hexo
  6. - Github
  7. categories: Blog
  8. ---

hexo文章均采用markdown文件,可用atom等编辑器修改。
2.新建目录
博客中关于、标签和分类等栏目无文件,需要个人手动创建。
新建目录:

  1. hexo new page categories[tags\about]

设置目录:
categories:

  1. ---
  2. title: categories
  3. date: 2018-04-23 20:23:17
  4. type: "categories"
  5. layout: "categories"
  6. ---

tags:

  1. ---
  2. title: categories
  3. date: 2018-04-23 20:23:17
  4. type: "tags"
  5. layout: "tags"
  6. ---

3.生成静态页面并push

  1. hexo d -g

此时前台博客就可以看到文章信息更新了。

参考文章

· https://xuhaoblog.com/2016/05/06/hexo-github-pages/
· https://hexo.io/
· http://theme-next.iissnan.com/

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注