静态博客

发布于
用 Github 页面 搭建 静态博客(Hexo)全教程。

终于下定决心好好折腾一下 github 了,说实话,真的很废时间,一开始还各种报错。重装又重启了N次,总算弄好了。。。其实N久之前研究过了两三次,均以失败告终,这次总算没白费精力。。


首先下载:

本人现在是 Windows 10 Build 10049 64位的环境。

官网文件安装路径
Node.jsnode-v0.12.2-x64.msiD:\Program Files\Nodejs
GitGit-1.9.5-preview20150319.exeD:\Program Files (x86)\Git

生成SSH key

打开 Git Bash

ls

查看有没有 .ssh 文件夹,即是否为第一次运行 git

ssh-keygen -t rsa -C "[email protected]"

邮箱随便填,会生成 id_rsaid_rsa.pub 两个文件然后输入密码,密码要大于 4字节,也可以为空。

Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/username/.ssh/id_rsa):

输入保存密匙的位置,直接回车

Created directory '/c/Users/username/.ssh'.

创建目录

Enter passphrase (empty for no passphrase):

然后输入密码,密码要大于4字节,也可以为空

Enter same passphrase again:

重复一次密码。

Your identification has been saved in /c/Users/username/.ssh/id_rsa.
Your public key has been saved in /c/Users/username/.ssh/id_rsa.pub.

得到 id_rsaid_rsa.pub 两个文件。

The key fingerprint is:
The key's randomart image is:

然后还有 密钥的指纹(finger print) 和随机字符画(randomart image)。

再登陆到 github 设置(setting),添加 SSH keys把刚才得到的 id_rsa.pub 用文本编辑器打开复制,粘贴到 key 里边,标题随便。

之后会被要求输入一遍 github 网站 的密码。


测试连接

$ ssh -T [email protected]
The authenticity of host 'github.com (192.30.252.128)' can't be established.
RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
Are you sure you want to continue connecting (yes/no)?

输入 yes

Warning: Permanently added 'github.com,192.30.252.128' (RSA) to the list of known hosts.
Enter passphrase for key '/c/Users/username/.ssh/id_rsa':

输入密码

Hi huching! You've successfully authenticated, but GitHub does not provide shell access.

就会看到 successfully

然后需要设置以一个全局的用户名和邮箱:

git config --global user.name "username"
git config --global user.email  "[email protected]"

也可以在 Git GUI 选项里边填写。


创建页面

这里需要用到 常用的 git 命令,当然也可以试试 GUI 界面。在工作目录打开 Gib bash

git init

初始化

git checkout --orphan gh-pages

切换到新分支(Github-pages 分支)

git add index.html

添加 index.html 文件

git commit -m "add page"

提交

git remote add origin https://github.com/huching/huc2.git

远程添加源,即你自己在 github 网站创建的。其中 huchinggithub用户名huc2项目名。下同。

git push origin gh-pages

推送给远程源,即可把本地页面提交到 Github。


博客程序

npm几乎被墙,速度太慢,切换到淘宝的镜像:

npm config set registry https://registry.npm.taobao.org

然后建立一个工作目录,里边

npm install -g hexo

安装

hexo init

初始化

npm install

安装依赖包

hexo g
hexo s

之后就可以在本地预览了 http://localhost:4000/

编辑 _config.yml 最后改为

deploy:
    type: git
    repo: https://github.com/huching/huc2.git

然后看官方文档,需要装一个 hexo-deployer-git

npm install hexo-deployer-git --save

最后

hexo d -g

把本地编辑好的,部署到网络上。


绑定域名

现在域名解析的管理页面 比如 huching.net 添加

@ CNAME huching.github.io

解析裸域名,例如 http://huching.net

www CNAME huching.github.io

解析 http://www.huching.net 或其他前缀

注意:示例的 huching.net 是解析到 http://huching.github.com/huc2

创建 CNAME 文件( 以 UTF-8 无 BOM 格式编码)然后再里边 填写要绑定的域名,放在 Hexo\source 目录,然后部署上去就好了。

查阅了官方文档,只准绑定一个域名,多打几行都是没有用的。


git push origin gh-pages
fatal: unable to access 'https://github.com/huching/huc2.git/': Failed to connect to github.com port 443: Timed out

如果不幸碰到 github 被墙,比如上面,连接超时。可以搭梯子,在 Git bash 里设置代理。(没实测,懒得弄。我不断重试直到能成功 push )

http.proxy
git config --global http.proxy 127.0.0.1:8087
git config --global http.sslVerify false

出错

如果出现 sh: hexo: command not found 等,是环境变量的问题,比如 自己乱改环境变量重装系统后直接拿以前安装好的程序运行安装时不是直接 Next下一步,而是自己取消勾选了环境变量 ……等 会造成这种结果。

需要自己到 控制面板 - 系统 - 高级系统设置 - 高级 - 环境变量 里边添加路径。上面的用户变量,添加:

变量名PATH
变量值C:\Users\username\AppData\Roaming\npm

下面的系统变量,修改 Path,最后面添加(有一个半角分号与之前的项分隔):

;D:\Program Files\Nodejs\

一般是彻底卸载后重启再重装 Node.js 就能解决。

参考