0%

Hexo博客搭建并部署到GitHub

引言

Hexo属于静态博客,因此一大优势就是可以不用专门的服务器作为支撑。经过一番折腾终于将这个博客运行起来了,中间踩了不少坑,写这一篇来简明记录安装过程

配置环境

Node.js

Ubuntu运行以下命令安装Nodejs与npm

sudo apt install nodejs npm

Windows可在官网下载

Git

Ubuntu运行以下命令安装

sudo apt install git-all

Windows可在[https://git-scm.com/download/win]下载

安装Hexo

安装hexo

npm install hexo-cli g

一般建议安装hexo-cli,最小版hexo在切换主题时可能有bug

初始化名为“blog”的博客文件夹

hexo init blog

安装主题

切换到主题文件夹

cd blog/themes

获取NexT主题

git clone https://github.com/theme-next/hexo-theme-next next

打开blog/_config.yml 文件,修改为

title: 博客标题
subtitle: ''
description: ''
keywords:
author: 作者名
language: zh-CN
timezone: ''

修改 theme 后面的名字为你刚才安装的主题,比如 next。

回到原文件夹

cd ..

新建一篇名为“my_site”的空白文章

hexo new my_site

部署

hexo g

本地预览

hexo s

打开浏览器,访问http://localhost:4000,能正常显示即可

部署到GitHub

创建仓库

登录后点击GitHub首页左栏的New创建新仓库,Repository name填用户名.github.io,Description可留空,类型建议选Private,最后点Create Repository

连接仓库

git config --global user.name "用户名"
git config --global user.email 邮箱

创建密钥文件

ssh-keygen -t rsa -C "你的github账号邮箱"

将公钥添加到Github

~/.ssh/id_rsa.pub中的内容全部复制
登陆到GitHub上,右上角小头像->Setting->SSH and GPG keys中,点击new SSH key
title随便填,将复制的内容粘到Key中,点Add保存

测试

ssh -T git@github.com

如果结果为 “ …You’ve successfully authenticated, but GitHub does not provide shell access”,则说明成功。

修改配置文件

打开blog/_config.yml 文件,修改为

deploy:
  type: git
  repo: https://github.com/用户名/用户名.github.io.git
  branch: master

获取令牌

Github在2021年移除了密码授权方式,因此还需创建令牌
Setting-> Developer settings->Personal access tokens中,选Generate New token,Note随便填,Select scopes中将repo全选,然后将生成的token妥善保存,以后将无法再次查看,如果遗忘只能重设。

部署

hexo clean
hexo g -d #构建并部署

填入Username,Password填之前的token
稍后即可在https://用户名.github.io看到你的博客了!


下一篇将会记录内容编写与个性化设置