使用Hexo和GitHub搭建博客

环境:Windows7、GitHub、Hexo3

GitHub Pages

GitHub Pages are public webpages hosted and easily published through GitHub.

         github Pages可以被认为是用户编写的、托管在github上的静态网页。

Hexo

A fast, simple & powerful blog framework

         Hexo 是一个快速、简洁且高效的博客框架。Hexo 使用 Markdown(或其他渲染引擎)解析文章,在几秒内,即可利用靓丽的主题生成静态网页。
         我们主要是用hexo在本地写博客,然后上传到github page,就像你commit项目代码到github一样.

创建GitHub Pages

  1. 登录github并创建一个库(new repository)
  2. 仓库名必须为【username.github.io】
  3. 详情请看官方教程

安装Hexo

  • 安装 git
  • 安装 Node.js
  • 使用Git Shell,输入下面的命令安装Hexo

    1
    npm install -g hexo-cli
  • 初始化:执行init命令初始化hexo到你指定的目录

    1
    2
    hexo init <folder>
    ##也可以cd到目标目录,执行hexo init
  • 安装插件,运行下面的命令即可 (由于hexo3.0很多功能都插件化了,包括git上传,server功能)

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

用Hexo管理博客

  • hexo new "title"
    新建一篇博客,然后对生成的md文件进行编辑(markdown语法,md文件的默认路径:目标路径\source\_posts)
    详见hexo writing
  • hexo clean
    清理缓存
  • hexo generate
    根据博客内容和配置项生成网页
    生成博客在public文件夹中,上传到github上的就是里面的内容
    详见hexo generating
  • hexo server
    本地查看效果:访问 http://localhost:4000 查看效果

部署

  • 使用Hexo 命令

    • 编辑_config.yml文件

      1
      2
      3
      4
      deploy:
      type: git
      repo: https://github.com/xx/xx.github.io ###<repository url> 需要使用 "Git Bash" 工具执行 deploy;否则不能成功??
      branch: master
    • hexo deploy
      上传到GitHub
      详见hexo deployment
      注意:有些新用户需要设置 ssh,否则上述命令会失败。详见GitHub ssh官方教程

  • 使用GitHub Windows
    • 拷贝 hexo目录\public 下的内容
    • 复制到仓库的本地副本中
    • 运行GitHub确认修改信息后执行右上角的sync 同步
  • 访问username.github.io,查看内容

Hexo主题

  1. Hexo有丰富的主题支持,本博客使用的是Next
  2. 更多主题 点击主题

注意

  1. 每次使用hexo命令时,都要在 hexo init 指定的 目录下
  2. 修改配置文件时注意YAML语法,参数冒号:后一定要留空格
  3. hexo是用markdown来写作
  4. 运行hexo命令使用Git Bash工具?

参考

  1. 使用GitHub和Hexo搭建免费静态Blog
  2. 用hexo搭建github博客
  3. 搭建hexo博客并部署到github上
  4. hexo你的博客
  5. 使用GitHub + Hexo搭建个人博客(九)- 切换 Hexo 主题