用hexo+github搭建个人博客

环境配置

  1. 安装Git Bash
    https://git-for-windows.github.io/
    查看版本
    1
    2
    $ git version
    git version 2.12.0.windows.1
  2. 安装NodeJs
    https://nodejs.org/en/
    LTS为长期支持版,Current为当前最新版
    查看版本
    1
    2
    $ node -v
    v8.9.1
  3. 安装hexo
    全局设置下载源然后再安装
    1
    2
    3
    $ npm config set registry https://registry.npm.taobao.org 
    $ npm config set disturl https://npm.taobao.org/mirrors/node
    $ npm install -g hexo-cli
    安装完成后查看版本
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    $ hexo -v
    hexo-cli: 1.0.4
    os: Windows_NT 6.1.7601 win32 x64
    http_parser: 2.7.0
    node: 8.9.1
    v8: 6.1.534.47
    uv: 1.15.0
    zlib: 1.2.11
    ares: 1.10.1-DEV
    modules: 57
    nghttp2: 1.25.0
    openssl: 1.0.2m
    icu: 59.1
    unicode: 9.0
    cldr: 31.0.1
    tz: 2017b
    创建一个文件夹放blog文件进入该文件夹初始化
    1
    2
    3
    4
    $ cd Y:\hexoblog
    $ hexo init
    $ npm install
    $ hexo generate
    初始化完成之后打开所在的文件夹可以看到以下文件
    node_modules:是依赖包
    public:存放的是生成的页面
    scaffolds:命令生成文章等的模板
    source:用命令创建的各种文章
    themes:主题
    _config.yml:整个博客的配置
    db.json:source解析所得到的
    package.json:项目所需模块项目的配置信息
    回到gitbash中配置github账户信息(YourName和YourEail都替换成你自己的)
    1
    2
    $ git config --global user.name "mengjingxuan" //你的github用户名,非昵称
    $ git config --global user.email "mengyanxu@qq.com" //填写你的github注册邮箱
    创建SSH
    用git bash执行如下命令:
    $ cd ~/. ssh #检查本机已存在的ssh密钥
    如果提示:No such file or directory 说明你是第一次使用git
    1
    $ ssh-keygen -t rsa -C "mengyanxu@qq.com"
    然后连续3次回车,最终会生成一个文件在用户目录下,打开用户目录,找到.ssh\id_rsa.pub文件,记事本打开并复制里面的内容,打开你的github主页,进入个人设置 -> SSH and GPG keys -> New SSH key:
    测试是否成功
    1
    2
    3
    $ ssh -T git@github.com # 注意邮箱地址不用改
    #如果提示Are you sure you want to continue connecting (yes/no)?,输入yes,然后会看到:
    Hi mengjingxuan! You've successfully authenticated, but GitHub does not provide shell access.

用编辑器打开你的blog项目,修改_config.yml文件的一些配置(冒号之后都是有一个半角空格的)

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

回到gitbash中,进入你的blog目录,分别执行以下命令:
1
2
3
hexo clean
hexo generate
hexo server

注:hexo 3.0把服务器独立成个别模块,需要单独安装:npm i hexo-server。
打开浏览器输入:http://localhost:4000

  1. 上传到github
    先安装一波:
    1
    $ npm install hexo-deployer-git --save #这样才能将你写好的文章部署到github服务器上并让别人浏览到
    执行命令(建议每次都按照如下步骤部署):
    1
    2
    3
    hexo clean
    hexo generate
    hexo deploy 或者 hexo d -g
    7.主题更换后
    1
    2
    $ npm install hexo-deployer-git --save
    $ hexo clean && hexo generate && hexo deploy
    添加RSS订阅
    1
    2
    3
    $ npm i hexo-generator-feed --save
    $ npm install hexo-generator-sitemap --save
    $ npm install hexo-generator-baidu-sitemap --save
    修改hexo 主目录(主目录,主目录,非theme的配置文件哈)里面的配置文件_config.yml,添加如下内容。
    1
    2
    3
    4
    5
    # sitemap
    sitemap:
    path: sitemap.xml
    baidusitemap:
    path: baidusitemap.xml