使用Hexo + Github搭建博客

1. 在 github 上创建项目

  • 在 github 上创建一个仓库 liyuan-meng.github.io(用户名.github.io)
  • 在搭建好的项目页点击 settings-GitHub Pages,你会发现项目已经部署到网络上: Your site is published at https://liyuan-meng.github.io/

2. 本地安装 Hexo 并更新配置

  • 安装 Hexo

    script
    1
    npm install hexo -g
  • 检查 Hexo 是否安装成功

    script
    1
    hexo -v
  • 创建一个文件夹用来写博客

    script
    1
    mkdir blog
  • 进入到 blog 目录下,初始化该文件夹,看到后面的“Start blogging with Hexo!”表示初始化成功!

    script
    1
    hexo init
  • 安装依赖

    script
    1
    npm install
  • 启动 hexo 服务并访问 localhost:4000

    script
    1
    hexo s

3. 关联 Github 和 Hexo

  • 设置 github 的 username 和 email(如果之前设置过则跳过此步骤)

    script
    1
    2
    3
    git config --global user.name "liyuan-meng"

    git config --global user.email "****@163.com"
  • 进入到 .ssh 目录下

    script
    1
    cd ~/.ssh
  • 生成秘钥和公钥(一路回车即可),得到两个文件: id_rsa、id_rsa.pub

    script
    1
    ssh-keygen -t rsa -C "****@163.com"
  • 将秘钥添加到 ssh-agent

    script
    1
    ssh-add ~/.ssh/id_rsa
  • 登录 github,点击头像下的 settings,选择 SSH and GPG keys,添加一个 ssh key,然后将公钥 id_rsa.pub 文件中的内容复制进去

  • 测试 ssh key 是否添加成功

    script
    1
    ssh -T git@github.com
  • 配置 deploy,打开 blog/_config.yml 文件,修改 deploy

    script
    1
    2
    3
    4
    deploy:
    type: git
    repository: git@github.com:liyuan-meng/liyuan-meng.github.io.git
    branch: master

4. 创建博客并提交

  • 安装扩展

    script
    1
    npm install hexo-deployer-git --save
  • 在 _posts 目录下创建一个 markdown 文件

    script
    1
    hexo new 'hello,hexo'
  • 生成以及部署

    script
    1
    hexo d -g

另一种写法

script
1
2
hexo generate
hexo deploy

访问 http://liyuan-meng.github.io