github+hexo搭建简易博客

依托第三方github,免费搭建hexo博客。

1. github上创建仓库

在github上创建个人仓库,仓库名为:[username].github.io

2. 软件以及包安装

  • 在本地主机安装nodejs:https://nodejs.org/zh-cn/
  • 安装git:https://git-scm.com/
  • 打开命令行或git bash,安装hexo:
    1
    npm install hexo-cli -g
    安装完成之后,可以使用hexo -v 命令检查是否安装成功
  • 由于hexo官方源在国外,下载速度较慢,可以先安装cnpm,然后用cnpm命令进行安装:
    1
    npm install -g cnpm --registry=https://registry.npm.taobao.org

3. 初始化hexo

新建一个文件夹,该文件夹将作为博客专用文件夹。cd到新建的文件夹目录下,输入命令:

1
hexo init

接着输入下面的指令,自动安装需要的组件

1
cnpm install

初始化完成后,会有一个默认主题以及一个hello-word的默认文章。可以先生成博客来看一下效果,运行命令:

1
2
hexo generate #可以简写成 hexo g
hexo server #可以简写成 hexo s

然后打开浏览器,在地址栏中输入http://localhost:4000 查看博客

4. 安装新的主题

输入命令:

1
2
hexo clean #清理缓存
git clone https://github.com/litten/hexo-theme-yilia.git themes/yilia

安装完成之后,修改文件_config.yml的theme为新的主题名称:

1
theme: yilia

然后hexo s启动服务器,可以查看新主题长啥样。

5. 配置ssh

git bash中输入:

1
ssh-keygen -t rsa -C "your github email address"

回车3次,生成密钥之后打开C:\Users\用户名.ssh\id_rsa.pub,复制,然后打开github,打开github的settings界面,点击SSH and GPG keys,然后添加SSH key,将刚才复制的内容粘贴到输入框中。

测试一下设置是否成功。在git bash中输入:
1
ssh -T git@github.com

然后输入yes,显示成功信息:

1
2
Warning: Permanently added 'github.com,52.74.223.119' (RSA) to the list of known hosts.
Hi scarletmjy! You've successfully authenticated, but GitHub does not provide shell access.

6. 远程部署配置

打开_config.yml,找到deploy,并在该位置键入:

1
2
3
4
deploy:
type: git
repo: git@github.com:username/username.github.io.git
branch: master

安装远程deploy的工具:

1
npm install hexo-deployer-git --save

使用命令

1
2
hexo generate #hexo g
hexo deploy #hexo d

将项目push到github上。
或者直接使用复合命令:

1
hexo d -g

然后打开链接:https://your_username.github.io,就可以看到刚刚部署的博客啦。

参考
在windows下安装hexo

【超简单】Windows下使用GitHub + Hexo搭建技术博客

如何搭建一个独立博客——简明Github Pages与Hexo教程

hexo官方文档


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!