[关闭]
@mritd 2015-12-30T09:32:42.000000Z 字数 4718 阅读 2513

Github 搭建 Hexo 静态博客

Linux CentOS Hexo 博客


操作环境为虚拟机中 CentOS_6.7 , 阿里镜像源

一、安装 Node.js

  1. tar -zxvf node-v4.2.4-linux-x64.tar.gz /usr/local/node
  1. # 编辑 /etc/profile (使用vim)
  2. vim /etc/profile
  3. # 在底部添加 PATH 变量
  4. export PATH=$PATH:/usr/local/node/bin
  5. # 最后保存并使其生效即可
  6. source /etc/profile

二、安装 Hexo

  1. mkdir hexo
  1. # 切换目录
  2. cd hexo
  3. # 安装Git(已安装可跳过)
  4. yum install git-core
  5. # 安装 Hexo
  6. npm install -g hexo-cli
  7. # 初始化 hexo
  8. hexo init
  1. # 启动测试
  2. hexo server
  3. # 此时控制台应该打印 如下语句
  4. INFO Hexo is running at http://0.0.0.0:4000/. Press Ctrl+C to stop.
  5. # 测试访问 http://IP:4000 ,效果如下

Hexo Start

Hexo 提供了其他许多扩展插件,比如 git插件、快捷命令插件等

  1. npm install hexo-generator-index --save
  2. npm install hexo-generator-archive --save
  3. npm install hexo-generator-category --save
  4. npm install hexo-generator-tag --save
  5. npm install hexo-server --save
  6. npm install hexo-deployer-git --save
  7. npm install hexo-deployer-heroku --save
  8. npm install hexo-deployer-rsync --save
  9. npm install hexo-deployer-openshift --save
  10. npm install hexo-renderer-marked@0.2 --save
  11. npm install hexo-renderer-stylus@0.2 --save
  12. npm install hexo-generator-feed@1 --save
  13. npm install hexo-generator-sitemap@1 --save

三、Hexo 相关命令

  1. hexo n “文章名称”
  1. hexo g
  1. hexo s
  1. hexo d

四、Hexo相关设置

直接上配置文件 (hexo/_config.yml),里面包括主题、github等参数设置,主题、Github配置请看下面

  1. # Hexo Configuration
  2. ## Docs: http://hexo.io/docs/configuration.html
  3. ## Source: https://github.com/hexojs/hexo/
  4. # Site 站点相关配置
  5. title: 烟雨平生
  6. subtitle: 一蓑烟雨任平生,也无风雨也无晴。
  7. description: 记录生活点滴,不断学习,每走一步,在这里印下足迹。
  8. author: mritd
  9. language: zh-CN
  10. timezone:
  11. # URL 网站URL
  12. ## If your site is put in a subdirectory, set url as 'http://yoursite.com/child' and root as '/child/'
  13. url: http://www.mritd.me
  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-:year-:month-:day.md # 新文章(post)生成为文件名
  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. auto_detect: true
  40. tab_replace:
  41. # Category & Tag
  42. default_category: uncategorized
  43. category_map:
  44. tag_map:
  45. # Date / Time format
  46. ## Hexo uses Moment.js to parse and display date
  47. ## You can customize the date format as defined in
  48. ## http://momentjs.com/docs/#/displaying/format/
  49. date_format: YYYY-MM-DD
  50. time_format: HH:mm:ss
  51. # Pagination
  52. ## Set per_page to 0 to disable pagination
  53. per_page: 10
  54. pagination_dir: page
  55. # Extensions
  56. ## Plugins: http://hexo.io/plugins/
  57. ## Themes: http://hexo.io/themes/
  58. theme: yilia #主题设置
  59. stylus:
  60. compress: true
  61. # Deployment
  62. ## Docs: http://hexo.io/docs/deployment.html
  63. deploy: #部署插件设置(目前只自动部署到github)
  64. type: git
  65. repo: git@github.com:mritd/mritd.github.io.git
  66. branch: master
  67. message: '站点更新: {{ now("YYYY-MM-DD HH:mm:ss") }}'

五、主题设置

hexo_yilia

1、 无论什么主题,先 Download 到本地,拿 yilia 为例,由于其托管于github,直接git clone即可

  1. # 任意目录下执行会生成 yilia 文件夹
  2. git clone https://github.com/litten/hexo-theme-yilia.git yilia

2、 根据作者教程更改一些参数,主要更改 主题目录 下的 _config.yml文件 (yilia/_config.yml),我的配置样例如下

  1. # Header
  2. menu:
  3. 主页: /
  4. 所有文章: /archives
  5. 随笔: /categories/随笔
  6. IT: /categories/IT
  7. 相册: /tags/相册
  8. # SubNav
  9. subnav:
  10. github: "https://github.com/mritd/"
  11. #weibo: "#"
  12. #rss: "#"
  13. #zhihu: "#"
  14. #douban: "#"
  15. mail: "mailto:mritd1234@gmail.com"
  16. #facebook: "#"
  17. google: "https://plus.google.com/u/0/111440014763883747104"
  18. #twitter: "#"
  19. #linkedin: "#"
  20. rss: /atom.xml
  21. # Content
  22. excerpt_link: more
  23. fancybox: true
  24. mathjax: true
  25. # 是否开启动画效果
  26. animate: true
  27. # 是否在新窗口打开链接
  28. open_in_new: false
  29. # Miscellaneous
  30. google_analytics: ''
  31. favicon: /favicon.ico
  32. #你的头像url
  33. avatar: /logo.jpeg
  34. #是否开启分享
  35. share: true
  36. share_addthis: false
  37. #是否开启多说评论,填写你在多说申请的项目名称 duoshuo: duoshuo-key
  38. #若使用disqus,请在博客config文件中填写disqus_shortname,并关闭多说评论
  39. duoshuo: 'mritd4hexo'
  40. #是否开启云标签
  41. tagcloud: true
  42. #是否开启友情链接
  43. #不开启——
  44. #friends: false
  45. #开启——
  46. friends:
  47. HelloDog: http://wsgzao.github.io/
  48. 咚门: http://www.dearzd.com/DBlog/
  49. Coding云: http://www.codingyun.com/
  50. 伯乐在线: http://blog.jobbole.com/
  51. #是否开启“关于我”。
  52. #不开启——
  53. #aboutme: false
  54. #开启——
  55. aboutme: 社会三好青年,祖国未来栋梁,世界未来领袖......前面都是吹牛逼的,我就是个逗比......

3、配置完成后将主题目录复制到 hexo/themes 目录下

  1. cp -r yilia hexo/themes

4、修改hexo主配置文件为对应的主题

  1. ## Themes: http://hexo.io/themes/
  2. theme: yilia #主题名称
  3. stylus:
  4. compress: true

5、重新生成静态文件 访问测试

  1. hexo clean # 清除缓存
  2. hexo g # 生成静态文件
  3. hexo s # 启动服务器预览

hexo_mritd

六、部署到Github

首先注册 Github 账户,这里掠过,然后创建一个新项目,项目名称为 用户名.github.io ,比如我的Github用户名是mritd,则创建的项目名为 mritd.github.io

hexo_githubproject

服务器生成秘钥;如果在 $HOME/.ssh 下有 id_rsa、id_rsa.pub 则可忽略创建过程

  1. # 执行以下命令然后一路回车 创建秘钥
  2. ssh-keygen
  3. # 复制 公钥内容 稍后加入Github 账户的 sshkey中
  4. less ~/.ssh/id_rsa.pub

登录 Github后访问 https://github.com/settings/ssh,选择 Add SSH key ,取个名字然后把内容粘进去,保存即可。

hexo_githubaddsshkey

修改 Hexo 配置文件即可 如下

  1. # Deployment
  2. ## Docs: http://hexo.io/docs/deployment.html
  3. deploy: #部署插件设置(目前只自动部署到github)
  4. type: git
  5. repo: git@github.com:mritd/mritd.github.io.git # 你新建的Github项目地址(用户名.github.io)
  6. branch: master
  7. message: '站点更新: {{ now("YYYY-MM-DD HH:mm:ss") }}' #每次部署后更新信息
  1. # 执行以下命令将自动更新到Github
  2. hexo d

示例站点

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