前言
本篇详细记录了通过Hexo+github搭建个人blog的全过程,因为之前看了不少人搭建的个人blog,自己就是想亲自尝试一下,并把实操的过程记录下来,以作日后备查。
使用Github Pages可以为你提供一个免费的服务器,免去了自己搭建服务器和写数据库的麻烦。此外还可以绑定自己的域名。
Hexo 是一个快速、简洁且高效的博客框架。Hexo 使用 Markdown(或其他渲染引擎)解析文章,在几秒内,即可利用靓丽的主题生成静态网页。
准备工作
安装Git
安装Node.js
安装Hexo
首先,新建一个文件,比如:在D盘下新建文件夹名为:Hexo,将博客的相关文件都存储在这个文件夹下,建好文件夹后,在该目录里面右键的弹出菜单中选择
git bash here
菜单即可。
接着在命令行窗口做如下操作:
命令行输入:
$ npm install hexo-cli -g
$ npm install hexo-deployer-git –save
初始化:
$ hexo init
本地查看效果:
$ hexo g
$ hexo s
打开浏览器输入: http://localhost:4000/ 回车即可查看效果。
部署博客
注册帐号:
首先要注册一个github账号,登录github.com网站即可完成注册。
找到New repository,新建一个仓库,比如:xxx.github.io,注意:xxx同用户名一样,如:
fivexxxxx.github.io
注意上面的的fivexxxxx就是和用户名一样。
配置SSH密钥:
$ ssh-keygen -t rsa -C “your email@example.com“
说明:引号里面填写你的邮箱地址,接着直接都默认回车也可,这样生成密钥简单的方法。
复制公钥到粘帖板:
$ clip < ~/.ssh/id_rsa.pub
Github中添加公钥
登录后,找到Settings–>SSH and GPG keys–>New SSH key
复制过来即可,因为上面已经将公钥复制到了粘帖板了。
测试:
$ ssh -T git@github.com
验证时报错:
FATAL: Unable to connect to relay host, errno=10061
kex_exchange_identification: Connection closed by remote host
Connection closed by UNKNOWN port 65535
修改C:\Users\sin.ssh目录下的config文件
修改后:
Host github.com
User git
Hostname ssh.github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa
Port 443
原来的:
#Host github.com
#ProxyCommand connect -S 127.0.0.1:1081 -a none %h %p
再验证就通过了
$ ssh -T git@github.com
The authenticity of host '[ssh.github.com]:443 ([20.205.243.160]:443)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '[ssh.github.com]:443' (RSA) to the list of known hosts.
Hi fivexxxxx! You've successfully authenticated, but GitHub does not provide shell access.
配置git个人信息:
$ git config –global user.name “此处填你的用户名”
$ git config –global user.email “此处填你的邮箱”
将本地Hexo文件更新到github仓库:
http方式时,报了个错误,导致不能更新到服务器,错误忘记记录下来。
修改为git方式:
先修改d盘下Hexo的 _config.yml文件,找到deploy项并作如下修改:
deploy:
type: git
repository: git@github.com:fivexxxxx/fivexxxxx.github.io.git
branch: master
保存文件后,再执行如下命令:
$ hexo g
$ hexo d
访问博客:
https://你的用户名.github.io
比如我的:
博客上发表文章
首先进入D:\Hexo\source_posts目录下,新建文件且后缀名.md。编辑md文件时,推荐 Windows 上使用 MarkdownPad2编辑器,macOS 上使用 Mou 编辑器,Linux 上使用 Remarkable编辑器。
md文件编辑OK后,即可推送到服务器:
$ hexo g
$ hexo d
部署后,报404错误,修改Hexo目录下_config.yml配置文件,如下:
url: https://fivexxxxx.github.io –开始写的路径有误导致
root: /
Next主题安装配置:
克隆时报如下错误:
git clone https://github.com/theme-next/hexo-theme-next themes/next
Cloning into ‘themes/next’…
fatal: unable to access ‘https://github.com/theme-next/hexo-theme-next/': Failed to connect to 127.0.0.1 port 1081: Connection refused
查看代理的命令:
env|grep -i proxy
取消代理:
unset http_proxy
上面操作不行。
启动代理VPN(科学工具):
git clone https://github.com/theme-next/hexo-theme-next themes/next
Cloning into ‘themes/next’…
remote: Enumerating objects: 12582, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 12582 (delta 0), reused 3 (delta 0), pack-reused 12578
Receiving objects: 100% (12582/12582), 8.03 MiB | 3.76 MiB/s, done.
Resolving deltas: 100% (7995/7995), done.
OK了
配置_config.yml文件:
//theme: landscape
theme: next
将原来的屏蔽掉,增加next主题,这样将我们的Hexo博客主题替换为Next主题了。
主题简单配置
Next提供了四中主题,可以在主题配置文件Hexo/themes/next/_config.yml文件中进行选择,分别是Muse、Mist、Pisces、Gemini
参考:https://blog.csdn.net/nightmare_dimple/article/details/86661502
经过上面的修改后,只有首页和归档两个连接OK,关于,标签,分类的连接都报404错误,修正错误参考下面链接
https://www.jianshu.com/p/3a05351a37dc
添加分类模块:
$ hexo new page categories
此时,你会发现source文件夹下有了categories/index.md,打开index.md文件将title设置为title:分类,同时,打开next目录内的_config.yml文件,找到menu,将原来被注释掉的categorcies。
例子:
title: 分类测试文章标题
categorices: 分类名
其他两个添加方法类推,参考上述的步骤重复一遍即可。
…
结束语
至此,尝鲜之旅基本完成了,本着简单的尝试即可的原则,不想整顿花里胡哨,大道至简嘛!总之,能把笔记记录成文,同时能给看到的人哪怕一点点参考和帮助就有了本笔记存在的意义…