写在前面:
一直想建一个自己的博客,但苦于对前端没有什么研究,也没钱买域名和服务器.
于是搜到了利用github page和hexo建立自己的blog的想法。
这样建站的好处在于:
1. 对代码要求很低,基本不需要会前端就可以建立自己的博客。
2. hexo有很多非常好看的主题完全满足一个blog的要求。
3. 没有钱买域名和服务器也可以,将代码和内容托管在github上,完全免费。
4. hexo使用markdown,理论上来说浏览速度应该是极快。
但是,在建站的过程中,我也遇到了一些问题。
其中一个就是,由于国内网站加载github特别慢,我又没有买域名和服务器,
所以博客国内访问速度特别慢。
针对这个问题,我目前想到的方法就是:
使用coding.net和github共同托管我的hexo代码.
但由于coding.net最近在更新,不让个人注册,我就得等一等了(感觉世界在针对我这个小可爱.jpg).github
- github创建个人库
- 登陆/注册github
- 创建新仓库:New repository。
- Repository name: 用户名.github.io(e.g. wangmazi.github.io)
- 安装git
- Linux:
1
sudo apt-get install git
- Mac OS:
1
brew install git
- Windows: https://gitforwindows.org/
- Linux:
- 配置git
- git bash:
1
2git config --global user.name 用户名
git config --global user.email github邮箱 - 设置ssh key文件: 我直接回车没设置密码
1
ssh-keygen -t rsa -C github邮箱
复制生成的id_rsa.pub文件中的内容(rsa public key)
进入github > settings > SSH and GPG keys >new ssh key, 起个名字,在key中粘贴复制的id_rsa.pub文件中的内容。 - 检测公私钥是否匹配成功,公钥在github上,私钥在本机。 若出现“You’ve successfully authenticated,…” 说明成功匹配。
1
ssh git@github.com
- git bash:
node.js
安装
1
brew install node
检查
1
2node -v
npm -vhexo
安装hexo
1
npm install -g hexo-cli
新建一个文件夹 blog/
初始化blog(在刚刚创建的blog文件夹下)
1
hexo init blog
测试网站:
建立新文章:
1
hexo new "My New Post"
启动服务器
1
hexo server
建立静态文件
1
hexo generate
部署到remote site
1
hexo deploy
在浏览器输入:http://localhost:4000 ,就可以看到一个初始的站点啦!
关联github和hexo:
site的配置文件:blog文件夹根目录下的 _config.yml
themes的配置文件:blog/themes/ 目录下的 _config.yml- 修改site的配置文件:
1
2
3
4
5
6# Deployment
## Docs: https://hexo.io/docs/deployment.html
deploy:
type: git
repo: https://github.com/你的用户名/你的用户名.github.io.git
branch: master - 执行: 完成部署
1
hexo d
- 修改site的配置文件:
一些有用的hexo命令
可以参考官方文档:https://hexo.io/zh-cn/docs/commands