用Hexo搭建静态博客(二), 将网页部署至github与gitcafe并使用SSH认证
在上一篇文章中介绍了如何在本地搭建Hexo以及用Hexo书写博客的基本操作。在这篇文章中将介绍如何将创建的博客发布至github及gitcafe的静态页面。通过为github及gitcafe添加ssh验证来跳过推送页面时输入用户名及密码。
Table of Contents
注册github及gitcafe账号
github及gitcafe都支持pages网页托管服务,首先选择要托管的服务注册账号。当然也可以同时将博客发布至github和gitcafe,对于国内及国外不同用户分别访问不同的服务上的博客。
将博客发布至github
创建github pages的详细方法可以参考官方帮助页面。
创建新的代码仓库
新代码仓库(repository)名必须为username.github.io
,其中username
为注册github的用户名。
设置用户名及邮箱
Hexo目录下的.deploy_git
文件夹即为需要发布的文件,这是一个git目录。在发布至github之前需要先设置git的用户名及邮箱地址。
1
2git config --global user.name "your name"
git config --global user.eamil "user@email.com"
这个命令设置了计算机中全局默认的用户名及邮箱,如果计算机中只有一个git账号的代码,这样设置该账号下的其他仓库时可以不用再进行设置。
但计算机中有多个账号的仓库代码要通过git同步时,就应该只设置该目录下git仓库的用户名及邮箱:
1
2git config user.name "your name"
git config user.eamil "your@email"
设置远程仓库地址
将远程仓库地址设置为新建的代码仓库,然后将网页推送至远程仓库:
1
2git remote add origin git@github.com:username.github.io
git pull origin master
一段时间后,直接访问网址username.github.io
就可以看到刚发布的博客了。
设置Hexo配置文件
配置Hexo的站点配置文件,加入远程仓库的信息,以后更新博客时只用执行 hexo d
就可以将博客发布到远端了。
1
deploy:
type: git
repository: git@github.com:username.github.io #你的GitHub Pages仓库
branch: master #要推送的分支,github pages默认为master
将博客发布至gitcafe
由于github 的连接速度并不理想,如果只是面向国内用户,可以将博客发布至gitcafe的pages服务。详细内容可以查看gitcafe的官方帮助文档,具体操作流程与github大同小异。
需要注意的是:gitcafe pages 的默认分支为gitcafe-pages
,因此推送时要使用gitcafe-pages
作为远端分支:
1
2
3git remote add origin git@gitcafe.com:username.gitcafe.io
git checkout -b gitcafe-pages
git pull origin gitcafe-pages
同时Hexo配置文件中的branch
也要改为gitcafe-pages
。
1
deploy:
type: git
repository: git@gitcafe.com:username.gitcafe.io #你的GitCafe Pages仓库
branch: gitcafe-pages #要推送的分支,gitcafe pages默认为gitcafe-pages
发布后稍等片刻,直接访问username.gitcafe.io
就可以看到刚发布的博客了。
同时将博客发布至github与gitcafe
如果同时设置了github及gitcafe,也可以将博客同时发布至两个服务。但是本地仓库的信息相同,因此需要使用相同的邮箱注册两个服务。在设置好远端服务后,如下修改Hexo站点配置文件:
1
deploy:
type: git
repo:
github: git@github.com:username.github.io, master
gitcafe: git@gitcafe.com:username.gitcafe.io, gitcafe-pages
然后hexo d
发布博客后,分别打开对应服务的网址就可以看到发布的博客了。
使用ssh密钥登陆
使用ssh密钥登陆github和gitcafe可以跳过密码输入,如果同时拥有两个服务也可以设置不同的密钥登陆,提升安全性。
生成ssh密钥
切换到ssh默认的密匙保存目录C:\Users\username\.ssh
,如果不存在则手动创建。
用以下命令生成与邮箱user@email.com
(即注册github及gitcafe的邮箱)对应的密钥:
1
ssh-keygen -t rsa -C "user@email.com"
首先会提示输入密钥名称
1
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/username/.ssh/id_rsa):
如果没有设置过ssh密钥,使用默认的id_rsa
即可。同时如果想在github及gitcafe上启用不同的密钥,在生成密钥时应采用不同的名称便于区分。
之后会提示输入密钥密码,该密码用来加密密钥,使用此密钥时需要输入该密码,留空则表示不使用密码。
1
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
然后会提示你成功生成了密钥,假设此时生成的密钥对为github_rsa
与github_rsa.pub
。相应的也可以为gitcafe网站生成不同的密钥对。
将公钥上传至github及gitcafe
登陆github,打开Settings
中的SSH keys
点击Add SSH key
,输入SSH key的名称,将.pub文件中的内容全部粘贴至Key
中,点击Add key
保存这个密钥。
gitcafe 中的设置完全相同,只是其SSH key管理的设置在账户设置
–SSH 公钥管理
中,相同的方式添加.pub公钥的信息即可。
设置ssh用户配置文件
在ssh用户配置文件C:\Users\username\.ssh\config
中指定对应服务所使用的私钥,如果该配置文件不存在则直接新建一个。
1
#github
Host github.com #昵称
HostName github.com #指代的网址
User git
IdentityFile ~/.ssh/github_rsa #认证私钥路径
PreferredAuthentications publickey
#gitcafe
Host gitcafe.com
HostName gitcafe.com
User git
IdentityFile ~/.ssh/gitcafe_rsa
PreferredAuthentications publickey
注意其中昵称是对应条目的代号,可以随便设置,在git中如果地址中出现该代号将被替换为对应的HostName
并用指定的私钥进行认证。因此若计算机中有两个github账户需要用不同的ssh key进行认证,可以如下设置:
1
#github 01
Host github01 #昵称
HostName github.com #指代的网址
User git
IdentityFile ~/.ssh/01_rsa #认证私钥路径
PreferredAuthentications publickey
#github 02
Host github02 #昵称
HostName github.com #指代的网址
User git
IdentityFile ~/.ssh/02_rsa #认证私钥路径
PreferredAuthentications publickey
在设置git远端地址或登陆时,用对应的昵称代替地址中的github.com
,比如:
1
2git remote add origin git@github01:username.github.io
git remote add origin git@github02:username.github.io
测试 ssh 配置是否正确
经过以上设置后应该可以用ssh登陆github及gitcafe,用以下命令可以测试配置是否正确。
1
2
3
4
5# 测试github
ssh -T git@github.com
# 测试gitcafe
ssh -T git@gitcafe.com
如果连接成功,会提示以下信息:
1
# github
Hi USERNAME! You've successfully authenticated, but GitHub does not provide shell access.
# gitcafe
Hi USERNAME! You've successfully authenticated, but GitCafe does not provide shell access.
测试通过后,就可以使用ssh key来发布博客及提交代码而无需输入密码了。
本文主要介绍了如何将Hexo博客发布至github及gitcafe,以及配置多对ssh公私钥进行git认证。下一篇文章中将介绍如何购买域名及为托管在github及gitcafe上的博客设置自定义域名。