前言:

  • 此教程适用于小白群体
  • 此教程属于基础类教程
  • 本人小菜鸟一个,如有错漏,欢迎批评指正
  • 好了往下看吧

0x0关于Hexo和github Page

什么是github page,以下是引用自官网的介绍 https://help.github.com/articles/what-is-github-pages/

What is GitHub Pages?
GitHub Pages is a static site hosting service.
GitHub Pages is designed to host your personal, organization, or project pages directly from a GitHub repository. To learn more about the different types of GitHub Pages sites, see “User, organization, and project pages.”
You can create and publish GitHub Pages online using the Jekyll Theme Chooser. If you prefer to work locally, you can use GitHub Desktop or the command line.

什么是Hexo?引用自官网的介绍 https://hexo.io/docs/

What is Hexo?
Hexo is a fast, simple and powerful blog framework. You write posts in Markdown (or other languages) and Hexo generates static files with a beautiful theme in seconds.

0x1 创建github page

  • 首先要有一个github账号,请到https://github.com 注册一个github账号
  • 注册完登录之后新建一个repository
    击右上角的“+”号,选择“New repository”菜单,创建仓库.
  • 在Create a new repository页面填写相关信息
    1.Repository name 填写:username.github.io,把 Initialize this repository with a README 勾选上,然后点击绿色的按钮 Create repository
    2.点击repository右边的 Settings,往下拉到 GitHub Pages 部分,点击 Change theme ,然后再点击右边的绿色按钮 select theme
    3.这时候在浏览器访问你的github page博客地址 https://yourname.github.io 就可以看到你的博客了

0x2 安装Node.js和Hexo

  • 安装git
    打开终端 alt+ctrl+T 在终端输入命令:

    1
    sudo apt-get install git
  • 安装node.js
    安装node.js前需要安装g++编译器,如果你已经安装过此处可以跳过
    在shell终端执行命令

    1
    sudo apt-get install build-essential

node.js安装方式有几种,我是用源码编译的方式安装的,源码下载https://nodejs.org/en/download/ 当然你可以选择其他的安装方式。
下载完源码解压到任意目录,然后进入到解压目录执行以下命令:

1
2
3
./configure
make
sudo make install

make编译的时间有点长,取决于你的电脑配置,像我的电脑就编译了半个钟….
安装完之后测试node.js是否安装成功,在shell终端输入:

1
node -v

安装成功了会输出node.js的版本信息,我的是6.9.2:

1
v6.9.2

  • 安装npm
    在shell终端执行命令:
    1
    curl http://npmjs.org/install.sh | sudo sh

如果使用上面的命令一直卡主不动可以换一种方法,node.js的github可以看文档

1
2
3
4
sudo apt-get install python-software-properties
sudo add-apt-repository ppa:gias-kay-lee/npm
sudo apt-get update
sudo apt-get install npm

  • 安装hexo
    命令:

    1
    sudo npm install -g hexo
  • 配置hexo
    创建一个存放博客的文件夹,然后进入到该目录

    1
    2
    mkdir myBlog
    cd myBlog

执行命令初始博客的根目录

1
2
hexo init
npm install

  • 让hexo关联到github.io
    安装hexo-deployer-git:
    1
    npm install hexo-deployer-git --save

配置hexo博客根目录下的_config.yml文件:
找到# deploy节点编辑:
type: 填写 git
repo: 填写你的github.io地址
branch: 填写 master
注意:”: “ 后面有一个空格,一定要细心

1
2
3
4
5
6
# Deployment
## Docs: http://hexo.io/docs/deployment.html
deploy:
type: git
repo: git@github.com:yourname/.github.io.git
branch: master

  • 配置ssh
    首先配置一下git
    “youremail”是你个人的具体邮箱地址

    1
    2
    git config --global user.email "youremail"
    git config --global user.name "youname"
  • 生成秘钥:
    输入命令

    1
    ssh-keygen -t rsa -C "yourmail"

之后提示Generating public/private rsa key pair. Enter file in which to save the key (/your_home_path/.ssh/id_rsa):
按enter键之后提示:
Enter passphrase (empty for no passphrase): [Type a passphrase] Enter same passphrase again: [Type passphrase again]
可以不输入直接enter
最后的生成信息:

1
2
3
4
Your identification has been saved in /your_home_path/.ssh/id_rsa.
Your public key has been saved in /your_home_path/.ssh/id_rsa.pub.
The key fingerprint is:
#01:0f:f4:3b:ca:85:d6:17:a1:7d:f0:68:9d:f0:a2:db yourmail #指纹每个人的都不一样的

在相应的目录会生成两个文件 id_rsa 和 id_rsa.pub

  • 将ssh key添加到GitHub中
    打开id_rsa.pub把内容复制,粘贴到github->setting->SSH and GPG keys->New SSH Key
    title自己写一个,key就是复制的内容,然后Add SSH Key
  • 验证一下SSH是否配置成功了
    输入命令:
    1
    ssh -T git@github.com

如果成功了你会看到类似于Hi j2u! You've successfully authenticated, but GitHub does not provide shell access. 的提示信息。

到这里安装和配置都完成了啦,那么让我们来开始写第一篇博客吧!

  • 第一步:创建一个新页面(名字随意,此处叫“helloHexo”)

    1
    hexo new post "helloHexo"
  • 第二部:生成html

    1
    hexo generate
  • 第三步:发布到github.io

    1
    hexo deploy

好了赶紧打开自己博客看看效果吧,在浏览器输入博客地址,例如我的地址是:https://j2u.github.io ,是不是看到了自己的博客啊!

0x3 给博客换主题

按照文档说的去安装一些依赖包,不同的主题依赖包不一样,详细的信息请看主题安装的doc

  • 主题安装完之后编辑本地博客的根目录下的 _config.yml,找到Extensions节点,修改theme: themenName 保存即可
    1
    2
    3
    4
    # Extensions
    ## Plugins: https://hexo.io/plugins/
    ## Themes: https://hexo.io/themes/
    theme: indigo #修改此项

0x4 安装工程中遇到的错误和解决办法

  • 类似于这样的错误(部分关键词)“……node.js:810var cwd = process.cwd();”
    搜索了一下,我的解决办法事重启shell终端。。。
  • 没有配置ssh
    一开始没有配置ssh,导致hexo连接不上github,把ssh配上即可